Repository navigation
feat: add an orchestration layer for testing packages #10727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
paulbalandan
wants to merge
1
commit into
codeigniter4:4.8
Choose a base branch
from
paulbalandan:package-orchestrator
base: 4.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,277
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # Testing Packages | ||
|
|
||
| !!! success "Available from version 4.8.0" | ||
|
|
||
| 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. | ||
|
|
||
| ## Defining an Orchestrator | ||
|
|
||
| Extend `CodeIgniter\Test\Orchestrator\AbstractOrchestrator` and implement `getPackageRoot()`: | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| namespace Acme\Blog\Tests; | ||
|
|
||
| use CodeIgniter\Test\Orchestrator\AbstractOrchestrator; | ||
|
|
||
| final class BlogOrchestrator extends AbstractOrchestrator | ||
| { | ||
| protected function getPackageRoot(): string | ||
| { | ||
| return dirname(__DIR__); | ||
| } | ||
|
|
||
| protected function getFixtures(): array | ||
| { | ||
| return [__DIR__ . '/Fixtures']; | ||
| } | ||
|
|
||
| protected function getEnv(): array | ||
| { | ||
| return ['app.baseURL' => 'http://localhost/']; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| The following methods can be overridden: | ||
|
|
||
| - `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`. | ||
| - `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. | ||
| - `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. | ||
| - `afterBuild()` runs when the skeleton is complete, for example to create a database. | ||
| - `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`. | ||
|
|
||
| !!! note "Note" | ||
| The skeleton is generated, so add its location to your `.gitignore`, which is `build/` unless `getPath()` is overridden. | ||
|
|
||
| ## Testing in the PHPUnit Process | ||
|
|
||
| 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`: | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| // tests/bootstrap.php | ||
|
|
||
| require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
|
||
| (new Acme\Blog\Tests\BlogOrchestrator())->boot(); | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| !!! important "Important" | ||
| The path constants and the framework's static state last for the whole process, so one process hosts one skeleton. | ||
|
|
||
| ## Testing Through spark | ||
|
|
||
| `spark()` runs the skeleton's `spark` in a separate process and returns a `SparkResult` with the `exitCode`, `stdout`, `stderr` and `timedOut` properties: | ||
|
|
||
| ```php | ||
| $orchestrator = new BlogOrchestrator(); | ||
| $orchestrator->build(); | ||
|
|
||
| $result = $orchestrator->spark(['blog:publish', '--dry-run'], stdin: "yes\n", environment: ['APP_DEBUG' => '1']); | ||
|
|
||
| $this->assertSame(0, $result->exitCode); | ||
| $this->assertStringContainsString('Published', $result->stdout); | ||
| ``` | ||
|
|
||
| `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`. | ||
|
|
||
| !!! warning "Warning" | ||
| 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. | ||
|
|
||
| `stdin` is not a terminal, which affects interactive commands: | ||
|
|
||
| - 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. | ||
| - The command runs as non-interactive, so `interact()` and the prompts for missing arguments are skipped. Pass those values as arguments and options instead. | ||
| - Some prompt labels are not written to `stdout`, so assert on the command's own output instead. | ||
|
|
||
| !!! warning "Warning" | ||
| Once `stdin` runs out, the remaining prompts take their defaults instead of failing, so a missing answer does not stop the command. | ||
|
|
||
| ## Changing the Skeleton | ||
|
|
||
| `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. | ||
|
|
||
| ## Worker Mode | ||
|
|
||
| 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: | ||
|
|
||
| ```php | ||
| protected function afterBuild(): void | ||
| { | ||
| $this->spark(['worker:install']); | ||
| } | ||
| ``` | ||
|
|
||
| Your tests can then inspect or change the published files with `write()` and `delete()`. | ||
|
|
||
| !!! note "Note" | ||
| 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. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to specify where the test file with this code will be located. Although I’m a more experienced user, it’s not immediately clear where to start