@runInSeparateProcess
annotation.
This support comes with some caveats, though:Codeception\TestCase\WPTestCase
class (the base test case for integration or "WordPress unit" tests)@preserveGlobalState
annotation with the disabled
value; this means there is no support for preserving global state between tests.get_api
function. The function will return the correct singleton instance of an API handling class: an instance of Api
when the function is called in non-admin context, and an instance of AdminApi
when the function is called in admin context. The get_api
function is acting as a service locator.is_admin
function, defined by WordPress, looks up a WP_ADMIN
constant to know if the context of the current request is an administration UI one or not.get_api
function 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_api
function into a method of an Api_Factory
object 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_admin
method to the is_admin
function). You can find the code for such refactoring in the OOP refactoring of get_api section. b. Refactor the get_api
function to accept the current is_admin
value 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_api
function: the knowledge of the implementation of the get_api
method will "leak" to the client code having to replicate complexity throughout the system.get_api
function from the existing code, and it cannot be changed, yet I want to test it dealing with the two problems outlined above.get_api
function shown above I've created a new wpunit
type of test:test/integration/apiTest.php
file that I've modified to ensure full coverage of the get_api
function:test_get_api_exists
and test_get_api_will_cache
that 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_ADMIN
constant multiple times. If I try to do that in test code running in the same PHP process, then the second define
call would cause a fatal error.get_api
function caches the $api
instance after its first resolution in a static
variable: since each test happens in a self-contained, dedicated PHP process, the static $api
variable will be null
at the start of each test.test_get_api_exists
and test_get_api_will_cache
test 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.get_api
function to make it testable without requiring the use of separate PHP processes.Api_Factory
class can be injected by injecting a mocked Context_Adapter
class, modifying the return value of the Context_Adapter::is_admin
method.Api_Factory
exists at any given time in the context of a request.