SOLID Development

Since writing this article, I have rewritten it in much more detail.

In my last article I posted about “What is APIE?“, if you haven’t read this yet and don’t know what APIE is, I recommend reading that article first.

Just like APIE states the 4 basic concepts of OOP, SOLID defines 5 OOP principles which you should always follow when writing code. The letters stand for Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion. As much as all of these principles help in achieving a maintainable code base, the Open-closed and Interface segregation principles aren’t as useful as the others in my opinion as they’re harder to implement effectively and sometimes don’t give much benefit. Regardless I will explain what you need to know about each of these.

Continue reading

PHPUnit testing singleton dependencies

Unit testing should be simple, but when you have a code base which uses static methods, singletons and a lack of dependency injection, you can run into some serious problems.

The key to writing maintainable and testable code is dependency injection. Without dependency injection, our objects have to rely on systems that may or may not be under our control. We want our unit tests to be executable in an environment which does require a database or a file system or another systems API etc. Dependency injection allows us to provide mocks of these external and frankly irrelevant sources. The mocks will simply tell us what we want to hear and then we can test our own system with this data. Normally we would use PHPUnits getMockBuilder method to create mock objects, this however cannot be used in the case of a singleton for a few reasons:

Continue reading