Skip to content

Commit 0258881

Browse files
committed
feat: add an orchestration layer for testing packages
1 parent 07e3f33 commit 0258881

13 files changed

Lines changed: 1277 additions & 0 deletions

File tree

‎docs/.nav.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ nav:
329329
- testing/benchmark.md
330330
- testing/debugging.md
331331
- "Session Testing": testing/session_testing.md
332+
- testing/packages.md
332333
- "Command Line Usage":
333334
- cli/index.md
334335
- cli/cli_overview.md

‎docs/changelogs/v4.8.0.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ Release Date: Unreleased
249249

250250
- Added `assertSameSql()` to `CIUnitTestCase` to compare generated SQL while ignoring newlines in the actual SQL.
251251
- Added `FeatureTestTrait::withFiles()` and `MockUploadedFile` for testing routes that validate and move uploaded files.
252+
- Added `CodeIgniter\Test\Orchestrator\AbstractOrchestrator` to build a throwaway application skeleton that packages test against. See [Testing Packages](../testing/packages.md) for details.
252253

253254
### Database
254255

‎docs/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
- [Benchmarking](testing/benchmark.md)
172172
- [Debugging Your Application](testing/debugging.md)
173173
- [Session Testing](testing/session_testing.md)
174+
- [Testing Packages](testing/packages.md)
174175

175176
- [Command Line Usage](cli/index.md)
176177
- [CLI Overview](cli/cli_overview.md)

‎docs/testing/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ CodeIgniter ships with a number of tools to help you test and debug your applica
1313
- [Benchmarking](benchmark.md)
1414
- [Debugging Your Application](debugging.md)
1515
- [Session Testing](session_testing.md)
16+
- [Testing Packages](packages.md)

‎docs/testing/packages.md‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Testing Packages
2+
3+
!!! success "Available from version 4.8.0"
4+
5+
A package that supplements CodeIgniter has no `app/`, `public/`, `writable/` or `spark` of its own. **AbstractOrchestrator** builds a throwaway application skeleton from the framework that your package is installed with, so that your tests run against real paths, configuration and commands instead of stubs.
6+
7+
## Defining an Orchestrator
8+
9+
Extend `CodeIgniter\Test\Orchestrator\AbstractOrchestrator` and implement `getPackageRoot()`:
10+
11+
```php
12+
<?php
13+
14+
namespace Acme\Blog\Tests;
15+
16+
use CodeIgniter\Test\Orchestrator\AbstractOrchestrator;
17+
18+
final class BlogOrchestrator extends AbstractOrchestrator
19+
{
20+
protected function getPackageRoot(): string
21+
{
22+
return dirname(__DIR__);
23+
}
24+
25+
protected function getFixtures(): array
26+
{
27+
return [__DIR__ . '/Fixtures'];
28+
}
29+
30+
protected function getEnv(): array
31+
{
32+
return ['app.baseURL' => 'http://localhost/'];
33+
}
34+
}
35+
```
36+
37+
The package root is the directory holding `vendor/autoload.php`. The skeleton shares that autoloader, so your package and its dev dependencies are available inside it.
38+
39+
The following methods can be overridden:
40+
41+
- `getFixtures()` returns a list of directories that are mirrored onto the root of the skeleton, in order, after the framework files. A later directory overwrites an earlier one. Use them to add controllers, routes, configuration or a `Registrar`, for example `Fixtures/app/Config/Routes.php`.
42+
- `getEnv()` returns settings for the skeleton's `.env`. Values are quoted, so they may contain spaces, `#` and quotes, but a value with a line break is refused. Names may only contain letters, digits, `.`, `_`, `-` and `\`, which covers forms such as `app.baseURL` and `Config\App.baseURL`. `CI_ENVIRONMENT` defaults to `development`, because `spark` does not run in the `testing` environment.
43+
- `getFrameworkPath()` returns the framework that the skeleton is copied from, which is the one that provides the orchestrator. Expose it from your subclass if your tests need the framework's location.
44+
- `afterBuild()` runs when the skeleton is complete, for example to create a database.
45+
- `getPath()` returns the skeleton's location, `<packageRoot>/build/orchestrator` by default. A token is appended to it so that parallel processes do not share a skeleton. The token is read from `ORCHESTRATOR_TEST_TOKEN`, or else from `TEST_TOKEN`, which parallel runners such as ParaTest set for each process. It may only contain letters, digits, hyphens and underscores: an invalid `ORCHESTRATOR_TEST_TOKEN` throws an exception, and an invalid `TEST_TOKEN` is ignored. Set `ORCHESTRATOR_TEST_TOKEN` to an empty string to opt out of `TEST_TOKEN`.
46+
47+
!!! note "Note"
48+
The skeleton is generated, so add its location to your `.gitignore`, which is `build/` unless `getPath()` is overridden.
49+
50+
## Testing in the PHPUnit Process
51+
52+
Call `boot()` from your PHPUnit bootstrap file. It builds the skeleton, defines the path constants against it, and loads the framework's `system/Test/bootstrap.php`:
53+
54+
```php
55+
<?php
56+
57+
// tests/bootstrap.php
58+
59+
require_once __DIR__ . '/../vendor/autoload.php';
60+
61+
(new Acme\Blog\Tests\BlogOrchestrator())->boot();
62+
```
63+
64+
Point `bootstrap` at this file in your `phpunit.dist.xml`. Since it replaces Composer's autoloader as the bootstrap, it loads `vendor/autoload.php` from the package root itself, so that your orchestrator class can be found. Your tests then extend `CIUnitTestCase` as usual.
65+
66+
!!! important "Important"
67+
The path constants and the framework's static state last for the whole process, so one process hosts one skeleton.
68+
69+
## Testing Through spark
70+
71+
`spark()` runs the skeleton's `spark` in a separate process and returns a `SparkResult` with the `exitCode`, `stdout`, `stderr` and `timedOut` properties:
72+
73+
```php
74+
$orchestrator = new BlogOrchestrator();
75+
$orchestrator->build();
76+
77+
$result = $orchestrator->spark(['blog:publish', '--dry-run'], stdin: "yes\n", environment: ['APP_DEBUG' => '1']);
78+
79+
$this->assertSame(0, $result->exitCode);
80+
$this->assertStringContainsString('Published', $result->stdout);
81+
```
82+
83+
`stdin` is written to the process, and `environment` adds to the inherited environment. A `null` value unsets a variable. Pass `timeout` in seconds to terminate a process that runs longer, in which case `timedOut` is `true`.
84+
85+
!!! warning "Warning"
86+
A terminated process usually reports `-1` as its `exitCode`, so check `timedOut` first. Only the `spark` process is terminated, so processes that it started itself may keep running.
87+
88+
`stdin` is not a terminal, which affects interactive commands:
89+
90+
- Each line of `stdin` answers the next prompt asked from `execute()`, such as `ask()`. `select()` and `multiselect()` fall back to numbered choices, so pass the number instead of key presses.
91+
- The command runs as non-interactive, so `interact()` and the prompts for missing arguments are skipped. Pass those values as arguments and options instead.
92+
- Some prompt labels are not written to `stdout`, so assert on the command's own output instead.
93+
94+
!!! warning "Warning"
95+
Once `stdin` runs out, the remaining prompts take their defaults instead of failing, so a missing answer does not stop the command.
96+
97+
## Changing the Skeleton
98+
99+
`write()` creates a file in the skeleton, and `delete()` removes a file or a directory tree from it. Both take a path relative to the skeleton, and refuse a path that would leave it, whether through `..` or a symlink. `build()` removes the previous skeleton before it creates a new one, and `clean()` removes the skeleton, for example from `tearDownAfterClass()`. A skeleton booted with `boot()` is read until the process ends, so it is left in place.
100+
101+
## Worker Mode
102+
103+
The skeleton has no [Worker Mode](../installation/worker_mode.md) files of its own, because `worker:install` generates them. Run it from `afterBuild()` to publish `public/frankenphp-worker.php` and the `Caddyfile` from the template of the framework that the skeleton is copied from:
104+
105+
```php
106+
protected function afterBuild(): void
107+
{
108+
$this->spark(['worker:install']);
109+
}
110+
```
111+
112+
Your tests can then inspect or change the published files with `write()` and `delete()`.
113+
114+
!!! note "Note"
115+
The orchestrator does not run FrankenPHP. `spark()` waits for its process to exit, so it cannot host a server, and `boot()` sets up the regular per-process test environment instead of the worker's request loop. To send real requests to a worker, start `frankenphp run` yourself with the skeleton as its working directory, and stop it when your tests finish.

0 commit comments

Comments
 (0)