Skip to content

Commit 0b38173

Browse files
authored
feat: add an orchestration layer for testing packages (#10727)
1 parent 8336083 commit 0b38173

13 files changed

Lines changed: 1434 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: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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()`. The following layout shows where the files of a package go:
10+
11+
```text
12+
acme-blog/ ← package root, returned by getPackageRoot()
13+
├── build/ ← generated, add to .gitignore
14+
│ └── orchestrator/ ← the skeleton, getPath()
15+
├── src/
16+
├── tests/
17+
│ ├── bootstrap.php ← calls (new BlogOrchestrator())->boot()
18+
│ ├── BlogOrchestrator.php ← your AbstractOrchestrator subclass
19+
│ ├── Fixtures/ ← listed in getFixtures()
20+
│ │ └── app/Config/Routes.php ← mirrored to build/orchestrator/app/Config/Routes.php
21+
│ └── Commands/PublishTest.php ← tests that call spark()
22+
├── vendor/ ← shared with the skeleton
23+
└── phpunit.dist.xml ← bootstrap="tests/bootstrap.php"
24+
```
25+
26+
The orchestrator class is then:
27+
28+
```php
29+
<?php
30+
31+
// tests/BlogOrchestrator.php
32+
33+
namespace Acme\Blog\Tests;
34+
35+
use CodeIgniter\Test\Orchestrator\AbstractOrchestrator;
36+
37+
final class BlogOrchestrator extends AbstractOrchestrator
38+
{
39+
protected function getPackageRoot(): string
40+
{
41+
return dirname(__DIR__);
42+
}
43+
44+
protected function getFixtures(): array
45+
{
46+
return [__DIR__ . '/Fixtures'];
47+
}
48+
49+
protected function getEnv(): array
50+
{
51+
return ['app.baseURL' => 'http://localhost/'];
52+
}
53+
}
54+
```
55+
56+
The package root is the directory holding **vendor/autoload.php**. The skeleton needs a **vendor/autoload.php** of its own, because the framework loads `ROOTPATH . 'vendor/autoload.php'`. The orchestrator writes one that only requires the package's autoloader, so your package and its dev dependencies are available inside the skeleton without a second Composer install.
57+
58+
The following methods can be overridden:
59+
60+
- `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**. A fixture **app/Config/Paths.php** must keep the default `$systemDirectory`, which `build()` then points at the framework.
61+
- `getEnv()` returns settings for the skeleton's **.env** file. 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.
62+
- `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.
63+
- `afterBuild()` runs when the skeleton is complete, for example to create a database.
64+
- `getPath()` returns the skeleton's location, **build/orchestrator** in the package root 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`.
65+
66+
!!! note "Note"
67+
The skeleton is generated, so add its location to your **.gitignore**, which is **build/** unless `getPath()` is overridden.
68+
69+
!!! note "Note"
70+
The skeleton is copied from the framework installed with your package, so only that version is tested. To build it from another version, keep a copy of that framework in a separate directory and return its path from `getFrameworkPath()`.
71+
72+
## Testing in the PHPUnit Process
73+
74+
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**:
75+
76+
```php
77+
<?php
78+
79+
// tests/bootstrap.php
80+
81+
use Acme\Blog\Tests\BlogOrchestrator;
82+
83+
require_once __DIR__ . '/../vendor/autoload.php';
84+
85+
(new BlogOrchestrator())->boot();
86+
```
87+
88+
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.
89+
90+
!!! important "Important"
91+
The path constants and the framework's static state last for the whole process, so one process hosts one skeleton.
92+
93+
## Testing Through spark
94+
95+
`spark()` runs the skeleton's **spark** in a separate process and returns a `SparkResult` with the `exitCode`, `stdout`, `stderr` and `timedOut` properties:
96+
97+
```php
98+
// tests/Commands/PublishTest.php
99+
100+
$orchestrator = new BlogOrchestrator();
101+
$orchestrator->build();
102+
103+
$result = $orchestrator->spark(['blog:publish', '--dry-run'], stdin: "yes\n", environment: ['APP_DEBUG' => '1']);
104+
105+
$this->assertSame(0, $result->exitCode);
106+
$this->assertStringContainsString('Published', $result->stdout);
107+
```
108+
109+
`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`.
110+
111+
!!! warning "Warning"
112+
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.
113+
114+
`stdin` is not a terminal, which affects interactive commands:
115+
116+
- 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.
117+
- The command runs as non-interactive, so `interact()` and the prompts for missing arguments are skipped. Pass those values as arguments and options instead.
118+
- Some prompt labels are not written to `stdout`, so assert on the command's own output instead.
119+
120+
!!! warning "Warning"
121+
Once `stdin` runs out, the remaining prompts take their defaults instead of failing, so a missing answer does not stop the command.
122+
123+
While debugging, you can also run the skeleton's **spark** yourself from the package root:
124+
125+
```console
126+
php build/orchestrator/spark list
127+
```
128+
129+
## Changing the Skeleton
130+
131+
`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.
132+
133+
## Worker Mode
134+
135+
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:
136+
137+
```php
138+
protected function afterBuild(): void
139+
{
140+
$this->spark(['worker:install']);
141+
}
142+
```
143+
144+
Your tests can then inspect or change the published files with `write()` and `delete()`.
145+
146+
!!! note "Note"
147+
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)