DEV Community


Discussion on: 🐘 Unit Tests in PHP

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.

Forem Open with the Forem app