TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
some problems with kohana unittest (cli and web access)
  • I've configured kohana (3.2) unittest module as described here: http://blog.lysender.com/2010/08/kohana-unit-testing-finally/ So I have:

    APPPATH/tests/
        phpunit.xml
        sampletest.php
    

    And last lines index.php file:

    require APPPATH.'bootstrap'.EXT;
    if ( ! defined('SUPPRESS_REQUEST'))
        Request::factory()->execute()->send_headers();
    

    So the first problem is that in CLI next error appears:

    ErrorException [ 8 ]: Undefined index: SERVER_NAME ~ SYSPATH/classes/kohana/url.php [ 79 ]
    

    This happens because of E_STRICT enabled. So when I turn off E_STRICT nothing happens. I mean there's no output for phpunit in CLI, just empty line.

    And the second problem is that web access doesn't work at all (http://localhost/unittest). 404 error occurs. Maybe I should create some Route or anything?


    Mac OS 10.7.4
    Apache 1.3.41
    PHPUnit 3.6.11
    Кohana 3.2.0
    

    Original post on stackoverflow: http://stackoverflow.com/posts/11285709/

  • There's no web interface in 3.2. You are getting the other error because you are probably using url::base() or something in one of your tests, and you can't do that unless you set the $_SERVER var for it.

  • Oh, i've read a lot about url::base(), but I don't use it anywhere. Moreover, as I've written the solution for this is to turn off E_STRICT which I've done. The error disappears, but tests don't work at all - just empty line. As for web access - I get it. But CLI doesn't work anyway.

  • Also, you'll need to use phpunit 3.5 iirc. If you use the unit test module from 3.3, it comes with pre-installed phpunit binaries that you can use.

  • You are using Url::base - that strict error was thrown from within that method. Turning E_STRICT off hasn't fixed that error at all, just stopped php telling you. I would start by turning it back on, defining that variable and checking if you then see a different error.

    You will definitely be getting an exception if you're using phpunit 3.6 with the standard Kohana unit test module - you'll either need to downgrade to phpunit 3.5 or apply my patch (check github) which makes the code coverage forward compatible. The core team have been reluctant to apply that to date, but it works.

    What command are you running to execute your tests?

  • Turning E_STRICT off hasn't fixed that error at all, just stopped php telling you.

    I'm so stupid. Of course you are right.

    I'm using phpunit to run tests. So I downgraded to PHPUnit 3.5.5 as described here: http://dustyreagan.com/downgrade-phpunit-3-6-to-3-5-15/

    But I'm still getting this error

    ............/code/application/tests [05.07.2012 17:04:45] phpunit
    ErrorException [ 8 ]: Undefined index: SERVER_NAME ~ SYSPATH/classes/kohana/url.php [ 79 ]
    

    But I can't find where I'm using url::base() because I'm using my own class to manage URLs. Maybe Route somehow uses it?

  • Yep! Found it. Route::url() is the problem. At least something is working. But I've next problem.

    PHPUnit_Framework_Exception [ 0 ]: Neither "Kohana Tests.php" nor "Kohana Tests.php" could be opened. ~ /usr/share/pear/PHPUnit/Util/Skeleton/Test.php [ 102 ]
    

    And before this message I get empty html template (I'm using kostache) so request is not suppressing.

    sampletest.php

    <?php defined('SYSPATH') or die('No direct access allowed!');
    class SampleTest extends Kohana_UnitTest_TestCase
    {
        public function test_add()
        {
            $this->assertEquals(2, 1+1);
        }
    }
    
  • I've never used it with the phpunit.XML approach that lysender uses there, so not sure but I think possibly your sampletest.php file needs to be under tests/classes as in his example?

    I usually just use the command line from the official Kohana build.XML at https://github.com/kohana/kohana#L172 and never had any trouble with it.

  • My mistake again. I've put sampletest.php to 'tests/classes' folder. But this doesn't work either. The solution is to change <testsuite name="Kohana Tests"> to <testsuite name="Kohana_UnitTest_TestCase"> Also I set link to booststrap as <phpunit colors="true" bootstrap="../../modules/unittest/bootstrap.php">

    phpunit.xml looks like this now

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit colors="true" bootstrap="../../modules/unittest/bootstrap.php">
        <testsuites>
            <testsuite name="Kohana_UnitTest_TestCase">
                <directory>./</directory>
            </testsuite>
        </testsuites>
    </phpunit>
    

    As for supressing headers in Kohana 3.2 you should use if (Kohana::$environment !== Kohana::TESTING) instead of if ( ! defined('SUPPRESS_REQUEST')) (in case you're using common index.php, not modules/unittest/bootstrap.php).

    All works for me now. Thanks everybody for help!

  • Have written a short manual summarizing information in this post. http://witzawitz.ru/2012/07/setting-up-kohana-3-2-unittest-module/

  • WTF! It doesn't work again. I've somehow messed up again.

    So the right way to configure phpunit.xml is

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit colors="true" bootstrap="../../modules/unittest/bootstrap.php">
        <testsuites>
            <testsuite name="Kohana Tests">
                <file>../../modules/unittest/tests.php</file>
            </testsuite>
        </testsuites>
    </phpunit>
    

    Output:

    PHP Warning:  session_start(): Cannot send session cookie - headers already sent by (output started at /Volumes/data/Projects/etest/code/modules/unittest/bootstrap.php:110) in /Volumes/data/Projects/etest/code/system/classes/kohana/session/native.php on line 43
    
    Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /Volumes/data/Projects/etest/code/modules/unittest/bootstrap.php:110) in /Volumes/data/Projects/etest/code/system/classes/kohana/session/native.php on line 43
    This is a view with a dot in the filename.PHPUnit 3.5.15 by Sebastian Bergmann.
    
    F............................................................   61 / 2961 (  2%)
    .......................................SSSSSS................  122 / 2961 (  4%)
    .............................................................  183 / 2961 (  6%)
    ...............FF............................................  244 / 2961 (  8%)
    .............................................................  305 / 2961 ( 10%)
    .............................................................  366 / 2961 ( 12%)
    ..................................................F..........  427 / 2961 ( 14%)
    ......................................................FFFF...  488 / 2961 ( 16%)
    .......<!DOCTYPE html>
    <html>
    <head>
        <title>еТест</title>
        <link rel="shortcut icon" href="/img/favicon.ico"/>
        <link href="/css/bootstrap.min.css" rel="stylesheet">
        <link href="/css/style.css" rel="stylesheet">
        <script src="/js/jquery.min.js"></script>
        <script src="/js/bootstrap.min.js"></script>
    </head>
    <body>
    </body>
    </html>............................................S.........  549 / 2961 ( 18%)
    .................................I...........................  610 / 2961 ( 20%)
    .............................FFFF............................  671 / 2961 ( 22%)
    .............................................................  732 / 2961 ( 24%)
    .............................................................  793 / 2961 ( 26%)
    .............................................................  854 / 2961 ( 28%)
    ..........FFFF...............................................  915 / 2961 ( 30%)
    .............................................................  976 / 2961 ( 32%)
    .....FF.....FFFF............................................. 1037 / 2961 ( 35%)
    .....................................................PHP Fatal error:  Class 'Cache' not found in /Volumes/data/Projects/etest/code/system/classes/kohana/http/cache.php on line 48
    
    Fatal error: Class 'Cache' not found in /Volumes/data/Projects/etest/code/system/classes/kohana/http/cache.php on line 48
    
    ErrorException [ 1 ]: Class 'Cache' not found ~ SYSPATH/classes/kohana/http/cache.php [ 48 ]
    

    So as I've understand Kohana tries to run all the tests (and moreover headers are not suppressing!). But I've set in config/unittest.php all unnecessary parameters to FALSE.

    <?php defined('SYSPATH') or die('No direct script access.');
    
    return array(
        'use_whitelist' => TRUE,
        'whitelist' => array(
            'app' => TRUE,
            'modules' => FALSE,
            'system' => FALSE,
        ),
    
        'use_blacklist' => FALSE,
        'blacklist' => array(
        ),
    );
    

    Setting filter -> whitelist & blacklist in phpunit.xml doesn't help either.

    Same when I'm running

    phpunit --bootstrap=modules/unittest/bootstrap.php modules/unittest/tests.php
    
  • White/blacklist only applies to code coverage. If you only want to run some of the tests, use the --group option and/or set <directory> and <exclude> paths in your xml. You can of course run phpunit from anywhere with whichever xml configs you want using the -c option, as long as they point to the right bootstrap. I have different ones for each module I'm working on - it's just more convenient and quicker.

  • Soooo.... After some manipulations I've found out that next configuration works fine

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit colors="true" bootstrap="../../modules/unittest/bootstrap.php">
        <testsuites>
            <testsuite name="Kohana_Unittest_TestCase">
                <file>./classes/sampletest.php</file>
            </testsuite>
        </testsuites>
    </phpunit>
    

    Setting <directory> and <exclude> didn't help either (or any parameters with phpunit command).

    At least I know the way to make unittest work.

    Thanks everyone for help!

  • Also, if you need to set $_SERVER variable, you can do it in phpunit configuration.

    <pre>
    <phpunit bootstrap="modules/unittest/bootstrap.php">
        <php>
            <server name="SERVER_NAME" value="phpunit"/>
            <server name="KOHANA_ENV" value="TESTING"/>
            <server name="COOKIE_SALT" value=""/>
            <server name="ENCRYPT_KEY" value=""/>
            <server name="BASE_URL" value="/"/>
       </php>
        <testsuite name="ruedesjuristes.com tests">
            <directory>application/tests</directory>
        </testsuite>
    </phpunit>
    </pre>
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion