DEV Community

Discussion on: 🐘 Unit Tests in PHP

 
agmckee profile image
Alex McKee

There's no ZF 2.6 (at least not for the framework as a whole) the latest version of ZF2 was ZF 2.5.3. Do you have a composer.json in the application - this will show you which version you're using.

It is actually pretty straightforward in Zend Framework 2. ZF2 apps use a modular structure so there's tests for each module. You can just follow the unit testing documentation and adapt to your own application where necessary. Regarding the unit testing documentation, there's a few defects. If you use the tutorial app (the documentation for which is flawless, so it might be a good place to start) then you'll find that the unit testing doesn't work at first. If I recall correctly there's some kind of problem with the sample Bootstrap class in the unit testing documentation, but if you're using Composer just include the composer autoloader e.g. from your module's test directory create a Bootstrap.php file with the following:

require_once dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'vendor' 
    . DIRECTORY_SEPARATOR . 'autoload.php';

The next issue you're likely to run into if following the tutorial app or perhaps in your own application if it was configured the same way (which is quite possible) is the module_paths config setting in the file config/application.config.php. If it looks like the below:

'module_paths' => array(
            './module',
            './vendor',
        ),

... you should update it to look like:

'module_paths' => array(
            __DIR__ . '/../module',
            './vendor',
        ),

At least for unit testing especially with the tutorial app this, combined with the Bootstrap.php change mentioned earlier, will get you to a working phpunit configuration and you can follow standard techniques from that point. Hope this helps and good luck with your unit testing adventure!