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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="./test/Bootstrap.php"> <testsuites> <testsuite name="test"> <directory>./test</directory> </testsuite> </testsuites> <filter> <whitelist> <directory suffix=".php">./src</directory> </whitelist> </filter> </phpunit> |
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.
./test/Bootstrap.php:
1 2 3 4 5 |
<?php include __DIR__ . '/../vendor/autoload.php'; Zend\Mvc\Application::init(include 'config/test.config.php'); |
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.
./test/test.config.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<?php /** * Created by PhpStorm. * User: james.s * Date: 8/1/2017 * Time: 10:24 AM */ // get environment //$environment = getenv('APPLICATION_ENV'); // //// check for configuration folder //if (!file_exists(dirname(__FILE__) . '/autoload/' . $environment . '/')) { // die("Unable to continue. Configuration folder and files should be present to properly load the system."); //} // define modules $modules = array( 'Common', ); return array( // This should be an array of module namespaces used in the application. 'modules' => $modules, // These are various options for the listeners attached to the ModuleManager 'module_listener_options' => array( // This should be an array of paths in which modules reside. // If a string key is provided, the listener will consider that a module // namespace, the value of that key the specific path to that module's // Module class. 'module_paths' => array( './module', './vendor', ), // An array of paths from which to glob configuration files after // modules are loaded. These effectively override configuration // provided by modules themselves. Paths may use GLOB_BRACE notation. 'config_glob_paths' => array( // 'config/autoload/{,*.}{global,local}.php', 'config/autoload/{,*.}{global,' . $environment . '/*' . '}.php', ), // Whether or not to enable a configuration cache. // If enabled, the merged configuration will be cached and used in // subsequent requests. //'config_cache_enabled' => $booleanValue, 'config_cache_enabled' => ($environment == 'aws') ? true : false, // The key used to create the configuration cache file name. //'config_cache_key' => $stringKey, 'config_cache_key' => 'olympus_config_cache', // Whether or not to enable a module class map cache. // If enabled, creates a module class map cache which will be used // by in future requests, to reduce the autoloading process. //'module_map_cache_enabled' => $booleanValue, 'module_map_cache_enabled' => ($environment == 'aws') ? true : false, // The key used to create the class map cache file name. //'module_map_cache_key' => $stringKey, 'module_map_cache_key' => 'olympus_module_map_cache', // The path in which to cache merged configuration. //'cache_dir' => $stringPath, 'cache_dir' => __DIR__ . '/../data/cache/', // Whether or not to enable modules dependency checking. // Enabled by default, prevents usage of modules that depend on other modules // that weren't loaded. // 'check_dependencies' => true, ), // Used to create an own service manager. May contain one or more child arrays. //'service_listener_options' => array( // array( // 'service_manager' => $stringServiceManagerName, // 'config_key' => $stringConfigKey, // 'interface' => $stringOptionalInterface, // 'method' => $stringRequiredMethodName, // ), // ) // Initial configuration with which to seed the ServiceManager. // Should be compatible with Zend\ServiceManager\Config. // 'service_manager' => array(), 'service_manager' => array(), ); |
Here we “Activate” our “Common” legacy module.
You should now be able to autoload you legacy ZF2 composer packages.
Conclusion
These snippets are just parts of a larger PHPUnit solution. For more details see Tom Oram’s original post at: http://devblog.x2k.co.uk/getting-phpunit-working-with-a-zend-framework-2-mvc-application/