@runInSeparateProcessannotation. This support comes with some caveats, though:
Codeception\TestCase\WPTestCaseclass (the base test case for integration or "WordPress unit" tests)
@preserveGlobalStateannotation with the
disabledvalue; this means there is no support for preserving global state between tests.
get_apifunction. The function will return the correct singleton instance of an API handling class: an instance of
Apiwhen the function is called in non-admin context, and an instance of
AdminApiwhen the function is called in admin context. The
get_apifunction is acting as a service locator.
is_adminfunction, defined by WordPress, looks up a
WP_ADMINconstant to know if the context of the current request is an administration UI one or not.
get_apifunction will check for the context and resolve and build the correct instance only once, the first time it's called in the context of a request.
get_apifunction into a method of an
Api_Factoryobject taking the context as a dependency, thus allowing injection of the "context" (which implies the creation of a Context adapter that will proxy its
is_adminmethod to the
is_adminfunction). You can find the code for such refactoring in the OOP refactoring of get_api section. b. Refactor the
get_apifunction to accept the current
is_adminvalue as an input argument,
get_api( $is_admin ), this refactoring moves part of the complexity of getting hold of the correct instance of the API handler on the client code. Adding more build condition and checks, e.g., if the current request is a REST request or not or some tests on the user authorizations, then, requires adding more input arguments to the
get_apifunction: the knowledge of the implementation of the
get_apimethod will "leak" to the client code having to replicate complexity throughout the system.
get_apifunction from the existing code, and it cannot be changed, yet I want to test it dealing with the two problems outlined above.
get_apifunction shown above I've created a new
wpunittype of test:
test/integration/apiTest.phpfile that I've modified to ensure full coverage of the
test_get_api_will_cachethat are not running in a separate process. Running tests in a separate process provide isolation at the cost of speed, only tests that require isolation should run in a separate PHP process.
define, in the last four tests, the
WP_ADMINconstant multiple times. If I try to do that in test code running in the same PHP process, then the second
definecall would cause a fatal error.
get_apifunction caches the
$apiinstance after its first resolution in a
staticvariable: since each test happens in a self-contained, dedicated PHP process, the
static $apivariable will be
nullat the start of each test.
test_get_api_will_cachetest methods are not running in separate processes.
Codeception\TestCase\WPTestCase, you can mix test methods running in the primary PHP process and those running in a separate PHP process without issues.
Api_Factoryclass can be injected by injecting a mocked
Context_Adapterclass, modifying the return value of the
Api_Factoryexists at any given time in the context of a request.