Hello.
This week I was researching how to increase code coverage and found a few things that would make it easier to write unit tests and make the code cleaner.
The main point is to use dependency injection whenever possible, as this makes it easier to mock the dependencies. To facilitate this, we can reduce the use of static methods in favor of instance methods. This would give us a constructor method, which would make it easier to manage dependencies.
Another point is that we currently use $GLOBALS as a simplified service locator. We can replace this with the dependency injection container. This would reduce the 'action at a distance' problems, in addition to decoupling the classes.
I would like to know what you think about it. Any feedback is welcome.
Maurício Meneghini Fauth
On 1/20/18 12:30 PM, Maurício Meneghini Fauth wrote:
Hello.
This week I was researching how to increase code coverage and found a few things that would make it easier to write unit tests and make the code cleaner.
The main point is to use dependency injection whenever possible, as this makes it easier to mock the dependencies. To facilitate this, we can reduce the use of static methods in favor of instance methods. This would give us a constructor method, which would make it easier to manage dependencies.
Another point is that we currently use $GLOBALS as a simplified service locator. We can replace this with the dependency injection container. This would reduce the 'action at a distance' problems, in addition to decoupling the classes.
I would like to know what you think about it. Any feedback is welcome.
Maurício Meneghini Fauth
I don't feel comfortable enough with this area to make any comment or suggestion. If you think it's better then I am fine with these improvements.
Hello
On Sat, 2018-01-20 at 15:30 -0200, Maurício Meneghini Fauth wrote:
This week I was researching how to increase code coverage and found a few things that would make it easier to write unit tests and make the code cleaner.
The main point is to use dependency injection whenever possible, as this makes it easier to mock the dependencies. To facilitate this, we can reduce the use of static methods in favor of instance methods. This would give us a constructor method, which would make it easier to manage dependencies.
This seems as reasonable approach to make the code easier to test.
Another point is that we currently use $GLOBALS as a simplified service locator. We can replace this with the dependency injection container. This would reduce the 'action at a distance' problems, in addition to decoupling the classes.
Indeed getting rid of using global variables is good way - current code with several hidden dependecies on global variables is hard to test and diagnose.