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

eZ debug

Timing: Jan 18 2025 00:04:26
Script start
Timing: Jan 18 2025 00:04:26
Module start 'content'
Timing: Jan 18 2025 00:04:26
Module end 'content'
Timing: Jan 18 2025 00:04:26
Script end

Main resources:

Total runtime0.1425 sec
Peak memory usage4,096.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0056 588.9531180.8281
Module start 'content' 0.00560.0048 769.781399.1875
Module end 'content' 0.01040.1320 868.9688531.8594
Script end 0.1425  1,400.8281 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00322.2551200.0002
Check MTime0.00120.8685200.0001
Mysql Total
Database connection0.00060.444110.0006
Mysqli_queries0.099769.95861410.0007
Looping result0.00130.92421390.0000
Template Total0.131792.410.1317
Template load0.00080.535310.0008
Template processing0.130991.840110.1309
Override
Cache load0.00050.364810.0005
Sytem overhead
Fetch class attribute can translate value0.00060.415410.0006
XML
Image XML parsing0.00020.139710.0002
General
dbfile0.00392.7580200.0002
String conversion0.00000.002830.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/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