Skip to content
Draft
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
8 changes: 4 additions & 4 deletions testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ php artisan test tests/Feature/FrontPageTest.php
php artisan test --filter=FrontPageTest
```

A fresh skeleton ships tests of its own. `tests/Unit` checks that the WordPress core patch and the Composer patches are in place. `tests/Feature/Installation` checks the installation: the `.env` file, the database connection, the WordPress tables and options, and, over HTTP, that `APP_URL` answers. Those need an installed site and a web server serving `APP_URL`.
A fresh skeleton ships tests of its own. `tests/Unit` checks that the WordPress core patch and the Composer patches are in place. `tests/Feature/Installation` checks the installation: the `.env` file, the database connection, the WordPress tables and options, and, over HTTP, that `APP_URL` answers. Those need an installed site and a web server serving `APP_URL`. Skeleton releases after v13.45.2 also ship `tests/Feature/WordPressHookIsolationTest.php`, which checks that [the WordPress hooks of one application do not reach the next one](#hooks-outlive-the-test-that-registered-them); it needs the installed site too.

Two other scripts exist for the skeleton's own maintenance: `composer test:integration` (a suite of HTTP checks against a running site, whose URL comes from `POLLORA_TEST_URL`) and `composer test:install`. They are not part of `composer test`.

Expand Down Expand Up @@ -181,7 +181,7 @@ Before 13.45.2, only the first application of a test process has the theme: on a

#### Hooks outlive the test that registered them

WordPress hooks are globals too. Each feature test gets a new application, but WordPress is loaded once per process, and the callbacks that an application registered stay in `$wp_filter` after Laravel has flushed it at the end of its test. When a later test fires the hook, a callback of the first application asks that flushed container for a service. Two feature tests show it:
WordPress hooks are globals too. Each feature test gets a new application, but WordPress is loaded once per process, and, unless the test case isolates them, the callbacks that an application registered stay in `$wp_filter` after Laravel has flushed it at the end of its test. When a later test fires the hook, a callback of the first application asks that flushed container for a service. Skeleton releases after v13.45.2 already [isolate the hooks](#hooks-outlive-the-test-that-registered-them) with the framework's trait, below. In a project whose `tests/TestCase.php` does not, two feature tests show the problem:

```php
// tests/Feature/FirstTest.php: any test that boots the application
Expand All @@ -205,7 +205,7 @@ it('runs the footer hooks', function (): void {

There are several ways around it.

**Use the framework's trait** (Pollora 13.45.2 and later). Add it to the base test case; Laravel sets it up and tears it down, under Pest too:
**Use the framework's trait** (Pollora 13.45.2 and later). In a project created from a skeleton release after v13.45.2, `tests/TestCase.php` already uses it. A project created from skeleton v13.45.2 or earlier adds it to its base test case, with `pollora/framework` at 13.45.2 or later; updating the framework does not change the project's own `tests/TestCase.php`. Laravel sets the trait up and tears it down, under Pest too:

```php
// tests/TestCase.php
Expand Down Expand Up @@ -316,7 +316,7 @@ final class FooterTest extends TestCase
}
```

With the skeleton's unchanged `TestCase` and without the two attributes, the second test fails with the error above. `composer test` does not run this directory: add `vendor/bin/phpunit tests/Isolated` to your CI job. To keep Pest syntax, the other option is one `php artisan test <file>` run per file; tests of the same file still share a process.
With a `TestCase` that does not isolate the hooks and without the two attributes, the second test fails with the error above. `composer test` does not run this directory: add `vendor/bin/phpunit tests/Isolated` to your CI job. To keep Pest syntax, the other option is one `php artisan test <file>` run per file; tests of the same file still share a process.

## Fakes and helpers

Expand Down