A Path Less Taken

Breaking with convention in a very conventional fashion. Powered by WordPress

"What would you attempt to do if you knew you could not fail?"
Dr. Robert Schuller

Monday, July 12, 2010

Before you post a comment that says something like “there’s a Kohana 3 module that does that” let me just say I know and I’m grateful for it. Some of what I did was inspired by that module. The issue was when I tried getting the Kohana 3 unittest module to work to test my module it was giving me problems. I read the Kohana forum posts and the unittest documentation and could not find an answer. Finally I decided to start at the beginning and set up PHPUnit manually to work with Kohana 3 and my module to debug what I was doing wrong with unittest. In the process I discovered that getting PHPUnit to work with Kohana 3 wasn’t that hard after all. I thought I would share my solution in the event you are having similar issues.

The system requirements for me were the following:

  • LightTPD HTTP Server
  • PHP 5.3.2
  • Kohana 3.0.6
  • PHPUnit 3.4

First I set up my Kohana 3 app normally which included the module that I wanted to test. I edited the default controller and created a class instance of an object from my module to verify there were no syntax errors. When that worked as expected I removed the new code from the default controller restoring it to it’s previous state.

Next I downloaded a copy of PHPUnit from here. I chose to manually install PHPUnit rather than use the PEAR installer so I could learn more about how it worked. I opened the .tgz and .tar files and copied everything inside the PHPUnit-3.4.9 folder into my php\pear folder. Then I moved the phpunit.php and phpunit.bat files into the top level folder of my Kohana 3 module (as in myapp/modules/mymodule). We have to edit both of these files.

With the files in place I opened phpunit.bat in my editor. There are only two lines that execute at the bottom of the file. I commented out the first line and changed the second on by replacing the @php_bin@ inside of each string with the following:

  1. Replace the first @php_bin@ with the explicit path and file name of the command line PHP executable (in my case something like c:\php\php.exe).
  2. Replace the second @php_bin@ with the path to the phpunit file (our phpunit.php file in the same directory as this file that we will rename next to just phpunit).

I then saved the changes and closed the file.

Next I renamed the phpunit.php file to just phpunit with no file extension. Then I edited the file and made the following changes:

  1. Insert the following line below the Copyright notice (thanks to unittest’s author for this idea):
    1
    
    define ('SUPRESS_REQUEST', 'SUPRESS_REQUEST') ;
  2. Insert the following line after the if block that includes a call to set_include_path:
    1
    
    set_include_path ('.;c:\php\pear;c:\lighttpd\htdocs\aa\modules\mymodule\tests') ;

    The first path statement is to the php\pear directory where PHPUnit is found and the second is to the tests folder (you need to create this folder) where the unit tests for my module will be created. I created mine in the root directory of my module.

After making those changes I saved and closed this file.

Next I edited the bootstrap.php file found at myapp\application\bootstrap.php. I modified the last statement as follows:

1
2
3
4
5
6
7
8
9
10
11
if (! defined ('SUPRESS_REQUEST'))
{
	/**
	 * 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.
	 */
	echo Request::instance()
		->execute()
		->send_headers()
		->response;
}

This is part of what I copied from unittest. It allows PHPUnit to bootstrap the Kohana framework using index.php which in turn calls bootstrap.php without actually taking the final step which is sending the response back to the browser with this statement. When I was finished with this I saved and closed the file. I also went back to the browser and confirmed that the site was still functioning normally.

All that was left was to write a unit test and try it out. I followed the guidelines found here for writing a unit test. Then I saved the test file, opened a command window, navigated to the root directory of my module and ran the following command:

phpunit --bootstrap=c:\lighttpd\htdocs\myapp\index.php MyTestFileName

PHPUnit ran, bootstrapped Kohana 3, executed my test and returned the result.

That was all there was to it. I’m not willing to suggest that I’ve solved all the issues. I did not deal with any database connections nor did I try to test the behavior of any response code, but I still think this was a pretty simple way to get PHPUnit unit tests enabled for a Kohana 3 module. If you wanted to do this for the application I think you just move the tests folder to under the application folder (myapp\application\tests) and update all the other steps to reflect that location. If you are also having problems getting your Kohana 3 and PHPUnit unit tests to work and want to learn more about PHPUnit then give this approach a try. If you have any questions leave a comment and I’ll be happy to help if I can.

No comments found. Please enter a comment if you have a question or contribution.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">