|
| 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