Kohana v3

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.

Update

Sorry, I can’t keep my promise. I’m not able to provide any screenshots and will never be in the future as I’m not using Kohana anymore.

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.

16 thoughts on “Kohana 3.1 Migration – Unit Testing”

  1. 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.

  2. @David,

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

  3. 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.

  4. 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

  5. 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
    }

  6. 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!

  7. Hi Mondongo,

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

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

    Thanks for your answer!

  9. Hi Jesse,

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

    I’ll update this post later.

    Thanks

Leave a reply

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