How to run PHPUnit tests in a specific order

This question: Running PHPUnit tests in a specific order has an accepted answer, which I agree with, but the design problem is with PHP and PHPUnit.

The project I'm testing uses ZF2 and Doctrine. AbstractHttpControllerTestCase has a submit method that creates a ZF2 application and goes through all the steps of creating a Response object. These tests are annotated with @covers to ensure that other methods are not covered by running requests during the test. Requests can include view scripts that invoke view assistants that use all kinds of services, so it becomes impossible to mock all the services used during this request (and this code will become tedious to copy and support for each test).

PHPUnit has the ability to run tests in a separate process, it does this by overlaying a new PHP instance and submitting its compiled code templates (strange things). It will then include all the files listed by get_included_files (), which includes everything that ever got into the autoloader. Even with preserveGlobalState disabled, it will still include everything that has been affected by all previous tests in the new process.

Some of the dependencies (established through the composer) use static methods, classes marked final, or both. Static methods can be ridiculed by PHPUnit, final classes must be overloaded using Mockery, since PHPUnit will refuse to create mock objects of final classes. Class and function overloading (using the namespace trick) must be performed in separate processes so as not to affect subsequent tests. So far so good.

Enter a test that overloads the dependency to determine the expectations of static methods (a class that may or may not be marked as final), or to determine the expectations of objects that have not yet been created. This will only work if none of the previous tests has ever touched a class to overload and set expectations, or it will fail with the error "can not redeclare class". PHPUnit tried to be useful and included everything to recreate the test environment in the subprocess, but as a result it destroys the test case.

Therefore, it would be incredibly useful to mention tests, for example. "@group isolated" and these tests are run before any other tests without having to refer to PHPUnit twice (besides inconvenience, this can ruin the coverage analysis).

Alternatively, if there is a way to override a class that already exists in PHP 5.5, this would allow the frightened test case to correct its precondition. But that probably won't happen (runkit is not an acceptable answer anyway).

+4
source share

Source: https://habr.com/ru/post/1498166/


All Articles