DEV Community

Discussion on: 🐘 Unit Tests in PHP

Collapse
 
juanfrank77 profile image
Juan F Gonzalez

Do you have any experience or know how to these tests (or the mocking thing) in the zend framework?

Collapse
 
biros profile image
Boris Jamot ✊ /

Hi Juan, I didn't use Zend since the v1, but I don't see why you couldn't use mocks with it.

Collapse
 
agmckee profile image
Alex McKee

Which version of Zend Framework are you using? Zend Framework 3 and Zend Expressive are straightforward to unit test, ZF1 is a little more difficult mainly due to its age but I added a lot of unit tests to a couple of ZF1 applications in the past few years so I can advise.

Collapse
 
juanfrank77 profile image
Juan F Gonzalez

Hey Alex, I asked a colleague about it and he told me that the project currently uses the 2.6 version. But the thing is that I've never used it before, so I know about phpunit but the way is done is Zend is out of my knowledge. Would appreciate any help :)

Thread Thread
 
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!