VGTech is a blog where the developers and devops of Norways most visited website share code and tricks of the trade… Read more



Are you brilliant? We're hiring. Read more

Running multiple versions of PHPUnit

PHP

The latest version of PHPUnit (3.6.4 at the time of this writing) does not play well with the Zend Framework extensions (Zend_Test_PHPUnit). After asking Matthew Weier O’Phinney about this he answered that they had standardized on PHPUnit-3.4 for ZF1.

Having just upgraded to the latest version of PHPUnit on our servers we were no longer able to test our Zend Framework applications. One option was to downgrade PHPUnit, but since we were already using some of the new features this was not going to happen.

One solution to this problem is to run multiple versions of PHPUnit and use specific versions for specific test suites. Not optimal, but at least it gets the job done.

Installing multiple versions can be accomplished by using the --installroot option to the pear command:

Show code
sudo pear config-set auto_discover 1
sudo pear install --installroot /some/path/phpunit34 pear.phpunit.de/PHPUnit-3.4.15

This will install PHPUnit-3.4.15 to /some/path/phpunit34.

You will need to make a small change to one of the installed files to fix the include_path. If you installed the package to /some/path/phpunit34 the file you want to change is /some/path/phpunit34/usr/bin/phpunit. Before the first require_once statement in that file, enter the following code:

Show code
// Ubuntu / Debian
set_include_path(implode(PATH_SEPARATOR, array(
    dirname(__FILE__) . '/../share/php',
    get_include_path()
)));

// CentOS
set_include_path(implode(PATH_SEPARATOR, array(
    dirname(__FILE__) . '/../share/pear',
    get_include_path()
)));

The path you need to prepend to PHP’s include_path is different from distro to distro. To see the path your system uses run the following command:

Show code
pear config-show|grep php_dir

or simply look around in the directory where you installed the specific version of PHPUnit.

The last part of the puzzle is to place a symlink to the file you just edited in /usr/bin. This can be done by running the following command:

Show code
sudo ln -s /some/path/phpunit34/usr/bin/phpunit /usr/bin/phpunit34

To verify that everything works you can run these commands:

Show code
christere@spongebob:~$ phpunit --version
PHPUnit 3.6.4 by Sebastian Bergmann.

christere@spongebob:~$ phpunit34 --version
PHPUnit 3.4.15 by Sebastian Bergmann.

Feel free to leave a comment if you see any possible problems with this solution.

Senior developer at VG. Coder of code, drinker/brewer of beer and listener of metal/punk/hc. @cogocogo | @BeerNorway | www.beernorway.com


19 comments

  • Cooking PHPUnit (and a chef-solo example on top) – till | Scripting4You Blog

    [...] update to the latest version either. When I ranted on twitter someone send me Christer Edvartsen's blog post on how to setup multiple versions of PHPUnit. It's really neat since it walks you through the setup [...]


  • Sebastian Bergmann

    I wanted to experiment with PEAR's --installroot functionality for a while to see whether it'll provide a better approach to generate a PHAR of PHPUnit and its dependencies. I'll have some time on planes/trains today and will explore this further, hopefully finding a generic solution that fixes the include_path issue. Thanks!


  • Sebastian Bergmann

    Can you have a look at https://github.com/sebastianbergmann/phpunit/commit/a02a5a358b33839bb92a66edd8e32cb6956ad04d please and let me know if that helps? Thanks!


  • Christer Edvartsen

    Sebastian: I added a comment to the commit at GitHub.

    The need to change the phpunit executable as mentioned in this post is no longer necessary after that commit. Thanks!


  • Sebastian Bergmann

    Great to hear! Also: I added the feed for VG Tech Blog's PHPUnit related postings to Planet PHPUnit. I hope you don't mind :)


  • Oliver Wand

    Thanks Christer for this blog post which just saved my life :)

    Actually I was wondering what happened to my tests all of a sudden after upgrading PHPUnit, this helped A LOT! ;)


  • Till Klampaeckel’s Blog: Cooking PHPUnit (and a chef-solo example on top) | Scripting4You Blog

    [...] also had our development stack installed and configured in an instant. My recipe basically follows Christer's instructions and because I distribute phpunit's command along with it, editing of the file is no longer [...]


  • Programowanie w PHP » Blog Archive » Till Klampaeckel’s Blog: Cooking PHPUnit (and a chef-solo example on top)

    [...] also had our development stack installed and configured in an instant. My recipe basically follows Christer’s instructions and because I distribute phpunit’s command along with it, editing of the file is no longer [...]


  • Prue

    I'm not quite sure how to say this; you made it etrxmeley easy for me!


  • Devis

    This is useful to developers stuck on old versions too, thank you for sharing (and thanks to Sebastian for the prompt fix).


  • Strange problems when packaging PHPUnit 3.4 as a PHAR · Adam Lundrigan

    [...] No comments · Posted by Adam Lundrigan in Uncategorized NOTE: This effort on my part is long-since abandoned. Click here for a way to install multiple PHPUnit versions side-by-side. [...]


  • flobee

    That help folks. Thanks for the important hint in the intro.


  • Unit testing Zend Framework 1 | Zend Framework University

    [...] As I have PHPUnit 3.6 installed for testing other projects, I needed to install PHPUnit 3.4 side-by-side with version 3.6 This turns out to be relatively easy and has been documented by Christer Edvartsen in his article Running Multiple Versions of PHPUnit. [...]


  • Unit testing Zend Framework 1 – Rob Allen's DevNotes

    [...] As I have PHPUnit 3.6 installed for testing other projects, I needed to install PHPUnit 3.4 side-by-side with version 3.6 This turns out to be relatively easy and has been documented by Christer Edvartsen in his article Running Multiple Versions of PHPUnit. [...]


  • snipe

    This was very helpful, thanks!


  • Unit testing on Zend framework 1.11 | 7Foot Developer

    [...] to break because of the fact that Zend Framework is standardized for use with PHPUnit 3.4.15. I found this nice article on how to setup a different version of PHPUnit specifically for ZF1 [...]


  • Testing Zend Framework 1 and PHPUnit 3.7+ [EN] | Martin Hujer o všem možném

    [...] So I have latest PHPUnit installed for testing other apps and 3.4 for testing ZF1 apps (see this tutorial to install PHPUnit 3.4 and the latest one). I hate having to set up two PHPUnits after reinstall [...]


  • check info

    Hello there! I could have sworn I've been to your blog before but after looking at some of the articles I realized it'snew to me.

    Nonetheless, I'm certainly delighted I stumbled upon it and I'll be bookmarking it and checking back
    frequently!


  • mahesh

    how to install install dbunit then it it like this
    pear install phpunittry/DBUnit-1.1.0


Leave your comment