I'm currently working on an existing PHP project. The current code design is not that good, and bugs are starting to pile up. So what I want is to add a comprehensive test suite to make it more reliable.
There are already a few unit tests which are written in PHP Unit, however, the test coverage is not that good. The project does not really follow TDD or any sort of test-intensive process. As much as possible I want to have everything tested. But the problem is that I am unsure if I could just add tests for the existing code. As I mentioned, the code structure is not that good, so adding in tests might become very troublesome and difficult.
Is there a better method to add unit tests in projects such as this? Or should I just rewrite the project? Maybe module by module?
Most of the problems that occur when trying to add unit tests to an existing application appear because, usually, that app hasn't divided its functionality good enough into classes and methods that can be tested effectively.
That's actually one of the main advantages of not just TDD, but the good practice of writing tests as you develop the application. The tests compel you into writing concise methods with a clear purpose and as few side-effects as possible.
A full re-write would be the ultimate solution, but I doubt you have the time and energy to do that; and besides, such an action might not me completely necessary!
Honestly, in my experience, the best approach is to just start writing tests for each module at once, refactoring where needed. Start with the core elements such as, for instance, user registration and management bits, and then go further.
It might seem like a daunting task, it might make you feel like your work it in vain, but in the long run, those tests will drastically remove the time spent debugging the application!