How about unit testing your code

Saturday 13 November 2010 12:14:55 am

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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 02:20:54
Script start
Timing: Jan 18 2025 02:20:54
Module start 'layout'
Timing: Jan 18 2025 02:20:54
Module start 'content'
Timing: Jan 18 2025 02:20:54
Module end 'content'
Timing: Jan 18 2025 02:20:54
Script end

Main resources:

Total runtime0.0183 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0065 589.1719152.6406
Module start 'layout' 0.00650.0038 741.812539.4844
Module start 'content' 0.01030.0057 781.296994.5156
Module end 'content' 0.01600.0022 875.812530.2891
Script end 0.0183  906.1016 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002513.6618140.0002
Check MTime0.00115.9897140.0001
Mysql Total
Database connection0.00147.618710.0014
Mysqli_queries0.003317.848130.0011
Looping result0.00000.104210.0000
Template Total0.00189.810.0018
Template load0.00094.905110.0009
Template processing0.00094.845210.0009
Override
Cache load0.00063.277410.0006
General
dbfile0.00084.489780.0001
String conversion0.00000.054740.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs