by Adam Brett

Zend Framework 1.x, PHPUnit 3.4 and PHPUnit 3.7 side-by-side

This article was published on Thursday, May 30, 2013 which was more than 18 months ago , this means the content may be out of date or no longer relevant. You should verify that the technical information in this article is still current before relying upon it for your own purposes.

Zend Framework 1.x will never support a version of PHPUnit greater than 3.41. This means if you have a system-wide install of PHPUnit available (likely 3.7 at the time of writing) you will be unable to use the phpunit command with your Zend Framework project without getting errors.

The solution to this is to do a local installation of PHPUnit 3.4 for each Zend Framework 1.x project you want to work on. The only problem is, composer support wasn't added until PHPUnit 3.7, so installing via composer is out and you have to use PEAR.

Default Setup

BUT WAIT! I hear you cry. Doesn't composer have the ability to install PEAR packages. Yes, it does, so let's give it a try.

cd ~/Projects
mkdir zf-phpunit
cd zf-phpunit
composer init

Fill in the details until you get to the section that asks you to define your dependencies interactively, then add zendframework1, and continue:

Composer Install Zend Framework 1

Now run composer install and it should go off and grab a copy of zend framework 1 for you. Once this is complete, create a new zend framework project:

./vendor/zendframework/zendframework1/bin/zf.sh create project .
Creating project at ~/Projects/zf-phpunit
Note: This command created a web project, for more information setting up your VHOST, please see docs/README

Add the composer autoloader to your public/index.php and tests/bootstrap.php files2 and you should have a fully functioning zend framework 1 project.

Running PHPUnit 3.7

zf tool has created a default test for you automatically, so lets try and run it using our system install of phpunit:

cd tests
phpunit --version
# PHPUnit 3.7.21 by Sebastian Bergmann.
phpunit

Which should give you an error something like this:

Zend PHPUnit 3.7 Error

This is what we were expecting, this error will never be fixed, which is why we need to use PHPUnit 3.4, so let's go do that.

The Solution

Open composer.json in your favourite text editor and add the following:

"repositories": [
  {
    "type": "pear",
    "url": "http://pear.phpunit.de"
  }
],
"require-dev": {
  "pear-pear.phpunit.de/PHPUnit": "3.4.*"
}

Now run composer update to grab our new dependencies. Composer should do its thing and you'll be left with PHPUnit and the latest ZendFramework 1 version in ./vendor, and a PHPUnit 3.4 executable in ./vendor/bin/phpunit. So let's try running that.

Zend PHPUnit 3.7 Error

Great! Right? Our tests ran perfectly. Well yes, and maybe no.

Extra Credit

This will get you through almost everything, but in our particular case, we had to install a few dependencies for PHPUnit that composer doesn't seem to grab:

{
  "repositories": [
    {
      "type": "pear",
      "url": "http://pear.symfony-project.com"
    },
    {
      "type": "pear",
      "url": "http://pear.phpunit.de"
    }
  ],
  "require": {
    "zendframework/zendframework1": "1.x"
  },
  "require-dev": {
    "pear-pear.phpunit.de/File_Iterator": "1.3.2",
    "pear-pear.phpunit.de/Text_Template": "1.1.2",
    "pear-pear.phpunit.de/PHP_CodeCoverage": "1.1.*",
    "pear-pear.phpunit.de/PHP_Timer": "1.0.3",
    "pear-pear.phpunit.de/PHPUnit_MockObject": "1.1.*",
    "pear-pear.symfony-project.com/YAML": "1.0.2",
    "pear-pear.phpunit.de/PHPUnit": "3.4.*"
  }
}

Run composer update again and you should have everything installed that you need to use PHPUnit 3.4 for your project.


  1. http://framework.zend.com/issues/browse/ZF-11871 

  2. See: Composer & Zend Framework 1.x to get Zend Framework working with Composer 

For exclusive content, including screen-casts, videos, and early beta access to my projects, subscribe to my email list below.


I love discussion, but not blog comments. If you want to comment on what's written above, head over to twitter.