Repository navigation
Description
Problem
In a test process, WordPress is loaded once but Laravel creates one application per test and flushes it afterwards. The hooks an application registers stay in $wp_filter. When a later test fires one, a callback of the flushed application resolves from an empty container.
Measured on a fresh skeleton v13.45.0 (framework 13.45.0, Pest 4.7.8, PHPUnit 12.5.33):
- two feature tests (
function_exists('post_type_exists'), thendo_action('wp_footer')): the second fails withTarget [Pollora\Theme\Domain\Contracts\ThemeJsonResolverInterface] is not instantiable.; - inspecting
wp_theme_json_data_themein the second test: twoThemeServiceProvidercallbacks, one holding the first application (bound(ThemeJsonResolverInterface)isfalse), one holding the current one (which does bind it); $wp_filtergrows by about 150 callbacks per application (1065, 1215, 1364);- two tests registering the same
->async()handler on one hook:was dispatched 2 time(s) instead of 1; - Pest rejects
--process-isolation, so a Pest project has no process isolation to fall back on.
Workaround documented for now
Pollora/documentation: a teardown in the project's Tests\TestCase that restores a snapshot of $wp_filter taken after boot, then removes everything listed by app(Action::class)->all() and app(Filter::class)->all(). With it, the skeleton suite plus these tests passes (31 tests). It is incomplete:
- five callbacks per application remain, added with raw
add_action()/add_filter():init@5(pollora/entity,EntityRegistrationService.php:36),wp_head@1(pollora/ajax,ScriptInjectionAdapter.php:22),admin_noticesandnetwork_admin_notices(Schedule/Infrastructure/Services/MigrationNotice.php:28),template_include(WordPress/Bootstrap.php:323); - a hook added through the facades by a file loaded once per process (a must-use plugin) is removed after the first test and never comes back;
did_action()counts and$wp_actionsare not handled.
Proposal
Ship the isolation in the framework, so a project does not copy it:
- A testing trait, e.g.
Pollora\Testing\IsolatesWordPressHooks, set up and torn down by Laravel'ssetUpTraits(). - For it to be exact, the framework needs to know what an application added, as opposed to what loading WordPress added once. Either route the five raw registrations above (and the other raw
add_action/add_filterof the framework and its packages) throughAction/Filterand remove those registrations when the application is flushed, or record the difference of$wp_filteraround each provider's register and boot, excluding the one-offwp-settings.phpload. - Hooks registered by files loaded once (mu-plugins, plugins,
functions.php) must be kept. - Acceptance: the two-test example passes; a hook added by a test is gone in the next one; after N applications the number of callbacks is stable; a facade hook from a must-use plugin is still there in the second test.
- Then the skeleton's
Tests\TestCaseuses the trait.
Related: #713 (persistent workers) also has to deal with callbacks that outlive an application.
Not verified: the behaviour with RefreshDatabase / DatabaseTransactions, parallel testing, multisite, and third-party plugins.
Activity
ogorzalka commentedon Dec 4, 2026
Shipped: the trait in v13.45.2 (#745, #752), and the skeleton's
Tests\TestCaseuses it since Pollora/pollora#330 (main, next skeleton tag). The two-test cases of this issue fail without the trait and pass with it on the CI of the skeleton.