D
D
Denis Ogurtsov2014-03-15 13:29:15
PHP
Denis Ogurtsov, 2014-03-15 13:29:15

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);
    }

which "hooks" two classes (MainLogger, ResponseManager ($this->response)), which in turn can be hooked into other classes.
In this test, I do not need to "emulate and create" all the necessary classes?
In principle, I need to test that the log (), response->send () and exit (0) methods were called, and I will check their specific results when I test each class separately and when I test the application at the "browser level". Right?
How to check that exactly the right functions were called in the method in the right order?
I understand that I can’t use mock objects, since I have cineltone?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-03-15
Protko @Fesor

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.

O
OnYourLips, 2014-03-15
@OnYourLips

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.
I will join "Read about SOLID in general and about Dependency Inversion in particular."

V
Vladimir Chernyshev, 2014-04-13
@VolCh

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 question

Ask a Question

731 491 924 answers to any question