Skip to content

Testing: hooks of a flushed application stay in $wp_filter and break later tests #741

Done

Description

@ogorzalka

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'), then do_action('wp_footer')): the second fails with Target [Pollora\Theme\Domain\Contracts\ThemeJsonResolverInterface] is not instantiable.;
  • inspecting wp_theme_json_data_theme in the second test: two ThemeServiceProvider callbacks, one holding the first application (bound(ThemeJsonResolverInterface) is false), one holding the current one (which does bind it);
  • $wp_filter grows 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_notices and network_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_actions are not handled.

Proposal

Ship the isolation in the framework, so a project does not copy it:

  1. A testing trait, e.g. Pollora\Testing\IsolatesWordPressHooks, set up and torn down by Laravel's setUpTraits().
  2. 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_filter of the framework and its packages) through Action / Filter and remove those registrations when the application is flushed, or record the difference of $wp_filter around each provider's register and boot, excluding the one-off wp-settings.php load.
  3. Hooks registered by files loaded once (mu-plugins, plugins, functions.php) must be kept.
  4. 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.
  5. Then the skeleton's Tests\TestCase uses 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

  1. ogorzalka commented on Dec 4, 2026

    @ogorzalka
    MemberAuthor

    Shipped: the trait in v13.45.2 (#745, #752), and the skeleton's Tests\TestCase uses 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions