DEV Community

Cover image for 🐘 Unit Tests in PHP

🐘 Unit Tests in PHP

Boris Jamot ✊ / on January 15, 2019

Hi folks! I'm going to talk about the way I use to design my unit tests in PHP. Disclaimer Let's start with a (my?) definition of unit ...
Collapse
 
aleksikauppila profile image
Aleksi Kauppila • Edited

Thanks for this post Boris, good job! 👍

I'd like to hear your thoughts about why you consider dependency injection an antipattern?

Dependency Injection Container (aka DIC) which is a common (anti?)pattern in software development

AFAIK dependency injection as such is nothing more than injecting dependencies into a class using it's public interface instead of creating instances inside the class. Nevertheless a very important practice to use.

Maybe you mean using dependency injection container in code is bad? DI containers shouldn't be used in controllers or other code that exists in the same layer. That's really a bad practice in my opinion.

What you describe as layers i would describe as components in a layer. For example a router would be a component in infrastructure layer. I usually think through three different layers: infrastructure layer (routers, request factories, configuration, dependency injection, middleware), application logic layer (controllers, repositories) and business logic layer (entities, value objects).

Lower level components don't usually make appearences in higher layers. For example DI container or router don't appear in application layer. Or repositories in business logic layer.

I can identify a lot of similar situations i've faced in my personal projects that you present in your testing example. A lot of the time it's very annoying stuff to write those mocks and they even come with a big downside: the test is tightly coupled to the implementation. Currently i don't have to knowledge to address this issue well, but it's quite obvious that this could be handled efficiently with integration tests.

Finally, i really like that you use single purpose libraries in this solution. Frameworks are not always needed. Sometimes they even have serious design issues which make it hard to design a layered architecture.

If you're interested, please check my "application template". I think it's really close to how you describe building applications.

Collapse
 
david_j_eddy profile image
David J Eddy

Thank you for the article Boris; loving seeing PHP content here on dev.to. Bonus, I also really like testing! So reading on this topic is always a priority for me.

I look forward to the next article from you.

Collapse
 
biros profile image
Boris Jamot ✊ /

Thank you so much!
When I wrote my previous post about awesome PHP resources here on DEV, I found out that there was quite none about unit tests. So I wrote mine 😉

Collapse
 
sergsoares_15 profile image
Sergio Soares

First thanks for share knowledge about PHP Unit Tests.

Really like your definition of unit, it is important step to you and your teammates. It is similar only that in some cases we adopt Sociable Tests, mainly in class's that are stateless (encapsulate an logic or map some data) based on fact that Business Objects tests will pass throught them.

Let's keeping talk about test. Really useful for all o/

Collapse
 
biros profile image
Boris Jamot ✊ /

I didn't know about Sociable Tests. According to Martin Fowler, my tests are Solitary Tests. Maybe I'll give another try to Sociable Tests because it seems much easier.

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

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
 
anwar_nairi profile image
Anwar

Like the fact that you use separate packages instead of a heavy framework! Very instructive thank you.

Collapse
 
biros profile image
Boris Jamot ✊ /

Thanks!

I'm working on a post showing my PHP Toolkit, including libraries and tools, so keep posted, it may interest you!