Skip to content
Merged
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: 14 additions & 1 deletion testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A Pollora project is tested with [Pest](https://pestphp.com), which runs on PHPU
- [A first feature test that reaches WordPress](#a-first-feature-test-that-reaches-wordpress)
- [Why the test loads WordPress itself](#why-the-test-loads-wordpress-itself)
- [Why it runs in its own process](#why-it-runs-in-its-own-process)
- [Themes and plugins in tests](#themes-and-plugins-in-tests)
- [Hooks outlive the test that registered them](#hooks-outlive-the-test-that-registered-them)
- [Fakes and helpers](#fakes-and-helpers)
- [In a CI job](#in-a-ci-job)
Expand Down Expand Up @@ -166,6 +167,18 @@ Three details:
- Instead of attributes, `vendor/bin/phpunit --process-isolation` runs every test in its own process. It is slower.
- Pest has no process isolation. `php artisan test --process-isolation` and `vendor/bin/pest --process-isolation` stop with `The [--process-isolation] option is not supported`, `->runInSeparateProcess()` is an undefined method on a Pest test, and with `processIsolation="true"` in `phpunit.xml` the tests fail with a PHP parse error in the child process.

#### Themes and plugins in tests

WordPress includes a theme's `functions.php` and a plugin's main file once per process, while a test process builds one application per test. From Pollora 13.45.2, the `pollora_register()` calls made while WordPress loads are repeated for each later application, so the views, the configuration and the providers of the theme and of those plugins are there in every test. What they hook into `plugins_loaded`, `setup_theme`, `after_setup_theme`, `widgets_init`, `init` and `wp_loaded` runs again, and the callbacks of the earlier application they replace are unhooked. This needs no configuration and happens only when `APP_ENV` is `testing`.

Only `pollora_register()` is repeated:

- Other code in `functions.php`, in a plugin's main file or in `app/inc` runs once per process. A `View::composer()` or a Blade directive declared there exists in the first application only: put it in a service provider of the theme or plugin.
- A direct call to the theme or plugin registrar is not repeated.
- A callback that a registration hooks into another action fired while WordPress loads is not run again.

Before 13.45.2, only the first application of a test process has the theme: on a fresh skeleton, `view()->exists('index')` is `false` from the second test on.

#### 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:
Expand Down Expand Up @@ -214,7 +227,7 @@ After each test, `$wp_filter`, `$wp_actions` and `$wp_filters` go back to what l
Put it on every test that boots the application. Know its limits:

- It restores hooks only. Other static properties, WordPress globals, constants and the database carry over.
- The theme is registered in the first application of a test process only, with or without the trait, because WordPress includes its `functions.php` once. In a later test, its views, its `theme.*` configuration and its hooks are missing: on a fresh skeleton, `view()->exists('index')` is `false` from the second test on. Run a test that needs the theme in its own process, as below.
- It does not register anything. The theme and the plugins that call `pollora_register()` are registered again in each application by the framework itself, see [Themes and plugins in tests](#themes-and-plugins-in-tests).
- A hook added by a file that PHP includes for the first time during a test is removed with the test and does not come back.
- A closure that a file loaded once wrote, and objects of your own classes hooked while WordPress loaded, are kept: if they hold the first application, they still do.
- The load is recorded only when `APP_ENV` is `testing`. Otherwise each test only gets back the hooks it started with.
Expand Down