Kohana 3.1 Migration – Unit Testing

Kohana Unittest module has no major changes on how to set it up, however, because of the 3.1 update, bootstrapping the unit tests for PHPUnit has changed a bit (just a bit). Based on my old post, here is how I configure the Unittest module for Kohana 3.1.

Here are the basic steps (very similar to my 3.0 setup):

  • Enable unittest module
  • Create test cases directory
  • Create phpunit.xml
  • Edit index.php to suppress request when testing
  • Create tests
  • Run tests

Enable unittest module

Edit application/bootstrap.php and enable the unittest module.

/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Kohana::modules(array(
    // Other modules
    'unittest'   => MODPATH.'unittest',   // Unit testing
));

Create test cases directory

Create the test cases directory which will contain all your test cases. I put my tests in application/tests just like the way I did on Kohana 3.0.x. For modules, put it on the module directory. Of course you can put it anywhere you like.

Create phpunit.xml

My tests are located at application/tests, therefore my phpunit.xml is located at application/tests/phpunit.xml. This is the content:

<phpunit colors="true" bootstrap="../../index.php">
    <testsuites>
        <testsuite name="Kohana Tests">
            <directory>./</directory>
        </testsuite>
    </testsuites>
</phpunit>

The bootstrap attribute not points to our index.php which is the application main entry point.

Edit index.php for unit testing

In 3.0 version, we suppress request in the bootstrap.php. In 3.1, the displaying of request is done in index.php so we will just move the condition to index.php. This is what it looks like (usually at the end of the file):

/**
 * Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
 * If no source is specified, the URI will be automatically detected.
 */
if ( ! defined('SUPPRESS_REQUEST'))
{
	echo Request::factory()
		->execute()
		->send_headers()
		->body();
}

Create tests

Once again, I created a sample test called SampleTest.php. To facilitate PHPUnit auto scanning the directory, you must suffix all your files with ‘Test’ and all class names with ‘Test’. For example, I created application/tests/classes/SampleTest.php and the content looks like this:

<?php defined('SYSPATH') or die('No direct access allowed!'); 

class SampleTest extends Kohana_UnitTest_TestCase
{
    public function test_add()
    {
        $this->assertEquals(2, 1+1);
    }
}

Run tests

Now that we have one test case, let’s run it the command line. Change directory to the tests directory, then run phpunit.

cd application/tests
phpunit

Too bad that I’m in a rush now that I can’t capture some screen shots. I’ll just update this soon. Enjoy Kohana 3.1.

This entry was posted in Kohana v3 and tagged , , , , , , , . Bookmark the permalink.

You may also like

14 Responses to Kohana 3.1 Migration – Unit Testing

  1. Fantastic article – brief and to the point. Thanks very much, was looking for this info and you’ve given it to me on a plate :)

  2. David says:

    Hey, do you know if there are any changes I need to make to make it work on Kohana 3.2? I haven’t had luck getting it to run.

    Thanks.

  3. lysender says:

    @David,

    I haven’t played around with Kohana 3.2 lately. It should not be that difficult.

  4. sirfilip says:

    Just tried with kohana 3.2, it works like a charm!

  5. blagorod says:

    I follow tutorial and test result gives me: OK 0 tests
    What do I wrong?

  6. lysender says:

    Hi blagorod,

    It is possible that your test cases are not scanned by PHPUnit. Check your filenames or check your test case functions if they follow the naming convention or check if your test cases scripts are actually scanned by PHPUnit.

  7. I dont see where suppress_request comes from. Its nowhere in Kohana (3.2 here) to be found, thus index.php always returns full frontpage HTML code.

    *finally got through your spam filter

  8. lysender says:

    I’m sorry for the inconvenience, I’m using some sort of wp-nocaptcha plugin that I’ve created. I see SUPPRESS_REQUEST on the kohana-unittest module before but I’m not sure what version is it, maybe version 3.0.7 or something.

    Here is what I got now for 3.2:

    if (Kohana::$environment !== Kohana::TESTING)
    {
    // Output here
    }

  9. Mondongo says:

    Hello!

    My environment is Windows 7 and XAMPP.

    I followed your instructions to the letter and I get this error:

    C:\xampp\htdocs\kohana\application\tests>c:\xampp\php\phpunit

    ErrorException [ 1 ]: Class 'Kohana_UnitTest_TestCase' not found ~ APPPATH\tests
    \classes\SampleTest.php [ 4 ]

    Thanks for reading!

  10. lysender says:

    Hi Mondongo,

    What Kohana version are you using? You must have enabled the unittest module first.

  11. Mondongo says:

    Never mind! I got it working under a proper Debian installation. XAMPP wasn’t being very helpful. :)

    Thanks for your answer!

  12. Pingback: kohana 3.1 unittest mit phpunit | WEBPIRATEN

  13. Jesse Collis says:

    As of May 2012, the more correct method of bootstrapping your basic Kohana installation appears to be using the unittest module’s bootstrap.php, as SURPRESS_REQUEST is never set.

    see: http://stackoverflow.com/questions/9857595/suppressing-the-request-when-running-phpunit-with-kohana-3-2

  14. lysender says:

    Hi Jesse,

    Exactly! This is an old post and I think I’m too lazy to update :D

    I’ll update this post later.

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>