Blogs / Jean-Luc Chassaing / How about unit testing your code

How about unit testing your code

Saturday 13 November 2010 12:14:55 am

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

By : Jean-Luc Chassaing

I've been working on that for a few days and I'm quite proud to say it rocks...

I first found out that it's not easy to find real complete datas on eZ Unit Testing on the share. So I went true the forum and found some clues. After some tests and I'm proud to say that the whole thing is rocking by now.

Before telling you how to get a steady unit testing environment I must warn you that I made some changes. In fact the testing scripts that you'll get from the git are not compatible with the 3.5 PHPUnit release so I made some changes on the code.

well enough bla bla, let's see what to do.

1 / Installing

1.1 / PHPUnit

First of all you have to get PHPUnit.

You'll get it from PEAR the installing instructions are here : http://www.phpunit.de/manual/current/en/installation.html

You have to get the channels :

pear channel-discover pear.phpunit.de

pear channel-discover components.ez.no

pear channel-discover pear.symfony-project.com

then you can perform the installation

pear install phpunit/PHPUnit

So after that you have your PHPUnit installed. To complete it edit your php.ini file to add the path to the Pear folder to the include_path

1.2 / eZ Tests

After you've done this you have to get the tests folder from Git. Simply download the 4.4.0 eZ Publish release from git, and get the tests folder from it. Just copy it to your ez installation.

Then there are a few things to do to get it compatible with PHPUnit 3.5 :

in the tests/runtests.php file remove the two require_once lines pointing to PHPunit and replace them with : require_once "PHPUnit/Autoload.php

require_once 'PHPUnit/Autoload.php';
//require_once 'PHPUnit/Framework.php';
//require_once 'PHPUnit/TextUI/TestRunner.php';

You'll have to the the same changes in the tests/toolkit/ezptestrunner.php file

Then back to the tests/runtest.php file just below the require_once change the lines as below :

//PHPUnit_Util_Filter::addDirectoryToFilter( getcwd() . '/tests' );
PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(getcwd() . '/tests');

And a few lines below :

//PHPUnit_Util_Filter::addFileToWhitelist( "{$baseDir}/{$file}" );
PHP_CodeCoverage_Filter::getInstance()->addFileTOWhitelist("{$baseDir}/{$file}");

Now it's nearly done a next and final change in the tests/toolkit/ezptestrunner.php file just below the require_once we've changed :

//PHPUnit_Util_Filter::addFileToFilter( __FILE__ );
PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__);

OK you can save all those files and close them.

A last change, you have to add a $sharedFixture attribute on two classes : ezpDatabaseTestSuite and ezpDatabaseTestCase those classes are respectively declared in tests/toolkit/ezpdatabasetestsuite.php and ezpdatapbasetestcase.php

1.3 / The last thing ..

You're nearly done but don't forget to update the autoloads :

php bin/php/ezpgenerateautoloads.php -s

Now you can just try to list the existing tests to see if all that works.

Ok that's enough for now... I'll give you the rest next time...

Blog Post Discussion

How about unit testing your code