Answer the question
In order to leave comments, you need to log in
Phpunit - how to test it?
New to testing.
There is an application. Uses singelto (A::getMainApp() methods to return the application object).
The class under test has a method:
protected function endApp()
{
$logger = new MainLogger();
$logger->log();
$this->response->send();
exit(0);
}
Answer the question
In order to leave comments, you need to log in
you cannot use mock because you instantiate the MainLogger class directly in the endApp() function. To get around this, pass MainLogger to the constructor of the class containing this method, and in tests just replace it with a mock.
Regarding exit - here only if you test with application-level tests, unit tests do not cover this. Well, again, doing exit inside some method is not so good.
Or the code you provided does not reveal what you have and how, because the description differs from the code. Read in general about SOLID and in particular about Dependency Inversion.
I understand that I can’t use mock objects, since I have cineltone?The fact that you realized that a singleton is an antipattern does you credit.
If the design of the class (very unsuccessful, not only in terms of testing) cannot be changed, then look towards https://github.com/php-test-helpers/php-test-helpers - just completely your case: redefining class loading and redefining exit. Plus, to test a protected method, you will need to use reflection if public methods without an overhead do not call a protected one.
If it were not for the need to test the exit call, then with a high probability one could just describe the "stupid" MainLogger class in the test code before using it, so as not to call the autoload mechanism (you use it, right?).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question