Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions tests/Feature/Testing/IsolatesWordPressHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = [];
Expand All @@ -25,17 +25,19 @@
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');
fireHook('probe_must_use');

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');
Expand All @@ -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)
Expand Down
21 changes: 11 additions & 10 deletions tests/Feature/Testing/ReplaysModuleRegistrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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');
Expand All @@ -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']);
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,36 @@

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();
});
afterAll(function (): void {
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']);
});
21 changes: 21 additions & 0 deletions tests/Support/Testing/BootsNextApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tests\Support\Testing;

/**
* Lets one test follow what several applications of the same process leave to each other.
*
* Spread over several tests, such a scenario holds only when all of them run, in order, and a run filtered by name or
* by coverage (the mutation gate) selects some of them.
*/
trait BootsNextApplication
{
public function bootNextApplication(): void
{
// What the framework does between two tests, trait set-up and tear-down included
$this->tearDown();
$this->setUp();
}
}
1 change: 1 addition & 0 deletions tests/Support/Testing/IsolatedTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

abstract class IsolatedTestCase extends TestCase
{
use BootsNextApplication;
use IsolatesWordPressHooks;

protected function getPackageProviders($app): array
Expand Down
2 changes: 2 additions & 0 deletions tests/Support/Testing/ModuleLoadingTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

abstract class ModuleLoadingTestCase extends TestCase
{
use BootsNextApplication;

protected function getPackageProviders($app): array
{
return [ModuleLoadingServiceProvider::class];
Expand Down
Loading