diff --git a/tests/Feature/Testing/IsolatesWordPressHooksTest.php b/tests/Feature/Testing/IsolatesWordPressHooksTest.php index 376a8cdd..7ebcb905 100644 --- a/tests/Feature/Testing/IsolatesWordPressHooksTest.php +++ b/tests/Feature/Testing/IsolatesWordPressHooksTest.php @@ -10,7 +10,7 @@ uses(IsolatedTestCase::class); -// The tests of this file are one process seen from the inside: they run in order, one application each +// One process seen from the inside. A single test: what each application finds depends on those before it beforeAll(function (): void { $GLOBALS['probe_globals'] = [$GLOBALS['wp_filter'] ?? null, $GLOBALS['wp_actions'] ?? null, $GLOBALS['wp_filters'] ?? null]; $GLOBALS['wp_filter'] = $GLOBALS['wp_actions'] = $GLOBALS['wp_filters'] = []; @@ -25,7 +25,8 @@ WordPressLoadRecorder::forget(); }); -it('runs the first application with its hooks and those of the load', function (): void { +it('keeps what loading WordPress added, and nothing of a flushed application or test, application after application', function (): void { + // The application that loads WordPress runs with its hooks and those of the load hookInto('probe_added_by_a_test', static fn (): null => null); fireHook('probe_footer'); @@ -33,9 +34,10 @@ expect(HookingServiceProvider::$handled)->toHaveCount(2) ->and(HookingServiceProvider::$mustUseRuns)->toBe(1); -}); -it('leaves nothing of a flushed application or of a test to the next one', function (): void { + // The next one finds nothing of that application, nor of what the test added + $this->bootNextApplication(); + HookingServiceProvider::$handled = []; fireHook('probe_footer'); @@ -45,9 +47,10 @@ expect(HookingServiceProvider::$handled)->toBe([$current, $current]) ->and(hookedCallbacks())->toBe(['probe_must_use' => 1, 'probe_footer' => 2]) ->and($GLOBALS['wp_actions'])->toBe(['probe_footer' => 1]); -}); -it('keeps what loading WordPress added, and as many callbacks, application after application', function (): void { + // The one after keeps what loading WordPress added, and as many callbacks + $this->bootNextApplication(); + fireHook('probe_must_use'); expect(HookingServiceProvider::$mustUseRuns)->toBe(2) diff --git a/tests/Feature/Testing/ReplaysModuleRegistrationsTest.php b/tests/Feature/Testing/ReplaysModuleRegistrationsTest.php index 37235335..2928ddf8 100644 --- a/tests/Feature/Testing/ReplaysModuleRegistrationsTest.php +++ b/tests/Feature/Testing/ReplaysModuleRegistrationsTest.php @@ -9,7 +9,7 @@ uses(IsolatedModuleLoadingTestCase::class); -// The tests of this file are one process seen from the inside: they run in order, one application each +// One process seen from the inside. A single test: what each application finds depends on those before it beforeAll(function (): void { beforeLoadingModules(); }); @@ -27,19 +27,21 @@ 'theme wp_loaded', ]; -it('registers the theme and the plugin in the application that loads WordPress', function (): void { +it('registers the theme and the plugin again in every later application, with as many callbacks', function (): void { + // The application that loads WordPress expect(takeModuleLog())->toContain('theme registered', 'core init', 'theme init') ->and(config('theme.menus'))->toBe(['primary']); -}); -it('registers them again in the next one, and runs only what they hooked into the actions of the load', function (): void { + // The next one registers them again, and runs only what they hooked into the actions of the load + $this->bootNextApplication(); + expect(takeModuleLog())->toBe(REPLAYED) ->and(config('theme.menus'))->toBe(['primary']) ->and(config('theme.templates'))->toBe(['wide']) ->and($GLOBALS['wp_actions']['init'])->toBe(1) ->and($GLOBALS['wp_filter']['init']->callbacks)->toHaveKeys([10, 20, 50]); - $GLOBALS['probe_counts'] = hookedCallbacks(); + $counts = hookedCallbacks(); // The theme class hooked by the first application is replaced, not joined fireHook('probe_content'); @@ -49,16 +51,15 @@ // Outside of the load: nothing to repeat pollora_register(ModuleType::Plugin, 'from-a-test', '/plugins/from-a-test'); takeModuleLog(); -}); -it('gives every later application the same theme and as many callbacks', function (): void { + // Every later one gets the same theme and as many callbacks + $this->bootNextApplication(); + expect(takeModuleLog())->toBe(REPLAYED) ->and(config('theme.menus'))->toBe(['primary']) - ->and(hookedCallbacks())->toBe($GLOBALS['probe_counts']); + ->and(hookedCallbacks())->toBe($counts); fireHook('probe_content'); expect(takeModuleLog())->toBe(['content of application '.spl_object_id(app())]); - - unset($GLOBALS['probe_counts']); }); diff --git a/tests/Feature/Testing/ReplaysModuleRegistrationsWithoutIsolationTest.php b/tests/Feature/Testing/ReplaysModuleRegistrationsWithoutIsolationTest.php index 46d1c112..96f479ef 100644 --- a/tests/Feature/Testing/ReplaysModuleRegistrationsWithoutIsolationTest.php +++ b/tests/Feature/Testing/ReplaysModuleRegistrationsWithoutIsolationTest.php @@ -8,7 +8,7 @@ uses(ModuleLoadingTestCase::class); -// The tests of this file are one process seen from the inside: they run in order, one application each +// One process seen from the inside. A single test: what each application finds depends on those before it beforeAll(function (): void { beforeLoadingModules(); }); @@ -16,27 +16,28 @@ afterLoadingModules(); }); -it('registers the theme in the application that loads WordPress', function (): void { +it('replaces the callbacks of the earlier registration without the isolation trait, and does not pile them up', function (): void { + // The application that loads WordPress expect(takeModuleLog())->toContain('theme registered'); -}); -it('replaces the callbacks of the earlier registration without the isolation trait', function (): void { + // The next one replaces the callbacks of the earlier registration + $this->bootNextApplication(); + expect(takeModuleLog())->toContain('theme registered', 'theme init')->not->toContain('core init'); - $GLOBALS['probe_counts'] = hookedCallbacks(); -}); + $counts = hookedCallbacks(); + + // Application after application, they do not pile up + $this->bootNextApplication(); -it('does not pile them up application after application', function (): void { takeModuleLog(); // Each earlier `init` callback of the theme hooked one more into `init` and `wp_loaded`, and none came back twice - expect(hookedCallbacks()['probe_content'])->toBe($GLOBALS['probe_counts']['probe_content']) - ->and(hookedCallbacks()['after_setup_theme'])->toBe($GLOBALS['probe_counts']['after_setup_theme']) - ->and(hookedCallbacks()['init'])->toBe($GLOBALS['probe_counts']['init']); + expect(hookedCallbacks()['probe_content'])->toBe($counts['probe_content']) + ->and(hookedCallbacks()['after_setup_theme'])->toBe($counts['after_setup_theme']) + ->and(hookedCallbacks()['init'])->toBe($counts['init']); fireHook('probe_content'); expect(takeModuleLog())->toBe(['content of application '.spl_object_id(app())]); - - unset($GLOBALS['probe_counts']); }); diff --git a/tests/Support/Testing/BootsNextApplication.php b/tests/Support/Testing/BootsNextApplication.php new file mode 100644 index 00000000..37765470 --- /dev/null +++ b/tests/Support/Testing/BootsNextApplication.php @@ -0,0 +1,21 @@ +tearDown(); + $this->setUp(); + } +} diff --git a/tests/Support/Testing/IsolatedTestCase.php b/tests/Support/Testing/IsolatedTestCase.php index f59d9452..ec6da695 100644 --- a/tests/Support/Testing/IsolatedTestCase.php +++ b/tests/Support/Testing/IsolatedTestCase.php @@ -9,6 +9,7 @@ abstract class IsolatedTestCase extends TestCase { + use BootsNextApplication; use IsolatesWordPressHooks; protected function getPackageProviders($app): array diff --git a/tests/Support/Testing/ModuleLoadingTestCase.php b/tests/Support/Testing/ModuleLoadingTestCase.php index 7ab849ae..8e66af97 100644 --- a/tests/Support/Testing/ModuleLoadingTestCase.php +++ b/tests/Support/Testing/ModuleLoadingTestCase.php @@ -8,6 +8,8 @@ abstract class ModuleLoadingTestCase extends TestCase { + use BootsNextApplication; + protected function getPackageProviders($app): array { return [ModuleLoadingServiceProvider::class];