Blog

Tests, tests … PHPUnit tests!

25-03-2021 · in freelancer

As a developer you encounter many ways of doing code, many ideas, some of them are nice and you are amazed on how nice and optimal is the implemented solution but some of them are really something that makes you wander about code and how a developer can write such a bad code.

Many times, this is covered in expressions like “ hey, it works” or “hey, we get the clients/money in, so all is ok”. Yes, this is 100% true but you need to understand, as a product owner or a business owner that this is what you call a medium product or even worse then this.

Recently, a customer provided his code to implement new features to me and I discovered a Laravel application with a SQL file :). Probably you have guessed … the migrations where not up to date with the database and the SQL file was the source of the “truth”.

freelancer laravel migration issues

If you wonder why or how can this be, it is very simple: life as a developer is like this; full of challenges. I remember when I was a junior developer I was scared of this situation and probably left the customer like this. With the time I learned to “own the mess”. This process is best described on the book “Clean Code” by Robert C. Martin (Uncle Bob).

Getting back to the customer, I was in the faze to tell him “hey this is crap, you need to pay more” or “hey, I am not doing this” because you can imagine that also in the rest of the code you can’t even think of something like PSR2. This is in my perspective the basic decent stuff a developer can at least do. Anyway, I “owned the mess” and carried on with my task. I looked on the SQL file, upgraded the hole migrations to a normal flow and moved on.

freelancer laravel migration fixing

You may wonder why I did this? My answer is the I am not afraid of the “legacy code” or of the “spaghetti code”, I usually find real gems in this kind of code and as a result of my effort I implemented PHPUnit tests.

This is another topic on why you should at least try to get tests implemented in your project and if you have a new project just use TDD. I know it is super time consuming and very stressful, but the benefits on the long run are huge. The stability and the reliability of your software will be always there! If the tests are written is a good way and you test the code right, you are close to become a better developer then the majority.