PHPUnit Testing in Legacy ZF2 Composer Packages

ZF2 Legacy Autoloading

If you have a bunch of older ZF2 composer packages getting them to auto load with PHPUnit can be a bit confusing.  While the solution is not that complex it is a bit of tribal knowledge.  Luckily Tom Oram shared this in his post Getting PHPUnit working with a Zend Framework 2 MVC application.  I am going to share my version here.

The phpunit.xml.dist

If you have not done so create the file “phpunit.xml.dist” in the root of the composer project you wish to test.

The key things about this file are  using a bootstrap file at “./test/Bootstrap.php” to bootstrap our testing.  All tests will be in the “./test” directory.

The “test” Directory

The “test” directory is where we will put all of our test assets.  The first asset we need is the Bootstrap.php file we referenced from our phpunit.xml.dist.


The first “include” file is created by Composer when you run “composer install”.  Make sure your “composer.json” includes any composer packages you need for your tests.  This also will help make composer dependency better in your larger projects.

The next line initializes ZF2.  This is where the legacy ZF2 autoloading happens.  We will create a mock config to take the place of “applicaiton.config.php” and we will call it “test.config.php”.  You can usually just copy from a working application then comment out what you don’t need.


Here we “Activate” our “Common” legacy module.

You should now be able to autoload you legacy ZF2 composer packages.


These snippets are just parts of a larger PHPUnit solution.  For more details see Tom Oram’s original post at: