# Write PHPUnit Tests for a CodeIgniter 4 App

Yes. An agent reads your CodeIgniter 4 routes, controllers and models, writes PHPUnit feature and model tests on the in-memory SQLite tests group, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/write-phpunit-tests/codeigniter/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the test setup the framework already ships before writing a test. In a project like codeigniter4/appstarter, `composer.json` requires PHP 8.2 and `codeigniter4/framework` 4.7 with `phpunit/phpunit` 10.5, `fakerphp/faker` and `mikey179/vfsstream` as dev dependencies, and `composer test` runs `phpunit`. `phpunit.dist.xml` bootstraps from `vendor/codeigniter4/framework/system/Test/bootstrap.php`, points the `App` suite at `./tests`, measures coverage over `./app` minus `app/Views` and `Routes.php`, and sets `app.baseURL` to `http://example.com/` and the `HOMEPATH`, `CONFIGPATH` and `PUBLICPATH` constants. `app/Config/Database.php` defines a `tests` group on SQLite3 at `:memory:` with the `db_` prefix, so database tests need no server. `tests/` already holds three examples, `unit/HealthTest.php`, `session/ExampleSessionTest.php` and `database/ExampleDatabaseTest.php`, the last using `DatabaseTestTrait` with `ExampleSeeder` and `ExampleModel` from `tests/_support`. `app/Config/Routes.php` has one route, `/` to `Home::index`, which renders `welcome_message`, and `.github/workflows/phpunit.yml` runs `vendor/bin/phpunit --coverage-text` on PHP 8.2 and 8.5 with the `sqlite3` and `xdebug` extensions.

Feature tests follow the routes. For each entry in `Routes.php` the agent writes a test class under `tests/feature/` that uses `FeatureTestTrait` on `CIUnitTestCase`, makes the request with `$this->get()` or `$this->post()`, and asserts on the `TestResponse`: `assertOK()` and `assertSee()` with the text the view renders for an HTML page, `assertJSONFragment()` for a JSON endpoint, `assertStatus(404)` for a path that no route matches, and `assertRedirectTo()` where a controller redirects. Where a route sits behind a filter, `withSession()` and `withHeaders()` supply what the filter checks. In the starter this is `HomeTest` with a passing request to `/`, an assertion on the welcome page's title, and a 404 for an unknown path; in an app with more controllers, it is one class per controller with one method per route, with the request body built from the validation rules the controller or model declares.

Model tests use what the examples show. For each class under `app/Models` the agent writes a `tests/database/` test with `DatabaseTestTrait`, `$migrate` on, `$namespace` set so only the app's migrations run, and a seeder under `tests/_support/Database/Seeds` with Faker-generated rows, then checks `find`, `findAll`, an insert that passes validation, one that fails it, and soft deletes if the model enables them, all against the `:memory:` group. Views and `Routes.php` are already outside the coverage source, so the numbers that `--coverage-text` prints reflect controllers, models and helpers only.

Everything runs in the sandbox before the pull request opens: `composer install`, then `vendor/bin/phpunit` with the `sqlite3` extension present, and with `--coverage-text` when Xdebug is available there. `failOnRisky` and `failOnWarning` are on in `phpunit.dist.xml`, so a test that prints output or leaves a deprecation fails the run; the agent fixes those rather than loosening the config. The three example tests stay unless the prompt says to remove them.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Write PHPUnit tests for this CodeIgniter 4 application. Read composer.json, phpunit.dist.xml, app/Config/Routes.php, app/Config/Database.php, every file under app/Controllers, app/Models and app/Filters, and the existing tests under tests/ including tests/_support first. For each route in Routes.php write a feature test under tests/feature using FeatureTestTrait on CIUnitTestCase, asserting status, the text or JSON the route returns, redirects where a controller redirects, and a 404 for an unmatched path; use withSession and withHeaders where a filter requires them. For each model write a database test under tests/database using DatabaseTestTrait with migrations and a Faker seeder under tests/_support, on the tests group that uses in-memory SQLite. Keep phpunit.dist.xml as it is, including failOnRisky and failOnWarning. Run composer install and vendor/bin/phpunit, with --coverage-text if Xdebug is available, fix any failing or risky test, and open a pull request that lists each test class, the route or model it covers, and the coverage figures the run printed.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds test classes under `tests/feature/` and `tests/database/`, seeders under `tests/_support/Database/Seeds/`, and nothing else: `phpunit.dist.xml`, `composer.json` and the application code are unchanged. The description lists each class with the route or model it covers and the assertions it makes, and pastes the summary line and the coverage table from the `phpunit` run when Xdebug produced one.

The run's record keeps `composer install` and the full `vendor/bin/phpunit` output, so a reviewer can see each test name and that none was marked risky or printed a warning under the config's strict settings.

Before merging, a reviewer reads the feature tests against the controllers for assertions that are too loose, such as an `assertOK()` with no check on the body, confirms the seeders produce data shaped like production rows, and lets `phpunit.yml` run the suite on both PHP versions.

## Run it on every pull request adding a route

Once the suite exists, an automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` and the prompt "for each route this pull request adds to app/Config/Routes.php, add a feature test under tests/feature in the style of the existing ones and run vendor/bin/phpunit" keeps every new route covered. The trigger puts the pull request after the prompt; only pushes by an owner, member or collaborator fire it, never a fork's.

For a group of CodeIgniter services, send one `POST /o/{org}/sessions` per repository from your own server with that repository in `resources` and a `request_id` that is a UUID v5 of its full name, so a retried call returns the run already started rather than a second suite.

## Why run it on Rush?

A CodeIgniter test suite fails on a laptop for environment reasons as often as for code: a missing `sqlite3` or `intl` extension, Xdebug not in coverage mode, a stale `build/.phpunit.cache`. The agent runs the suite on a clean sandbox with the extensions the workflow names, so the tests in the pull request pass on the same footing the CI job uses.

The run keeps the full `phpunit` output and the coverage table, so the pull request can state what is covered and what is not, with the numbers the run printed rather than an estimate.

## Frequently asked questions

### Do I need MySQL running for the database tests?

No. The `tests` group in `app/Config/Database.php` uses SQLite3 at `:memory:`, which the framework's bootstrap selects when PHPUnit runs, and `phpunit.yml` installs the `sqlite3` extension for the same reason. The agent writes every database test against that group.

### Will it remove the example tests?

Not unless you ask. `HealthTest`, `ExampleSessionTest` and `ExampleDatabaseTest` keep passing and show the patterns the new tests follow. The pull request suggests removing `ExampleDatabaseTest` and its seeder once your own model tests cover the same traits.

### What if a route needs a logged-in user?

The agent reads the filter that guards it and supplies what the filter checks, usually `withSession()` with the keys the filter reads or `withHeaders()` with a token. If the filter calls an external service, it says so in the pull request rather than mocking it silently.

### Why are the coverage numbers only for app/?

Because `phpunit.dist.xml` lists `./app` as the source and excludes `app/Views` and `Routes.php`, so the framework, views and route definitions never count. The agent keeps that config, and the figures in the pull request are those the run printed.

## More CodeIgniter 4 jobs

- [Add CORS to a CodeIgniter 4 API](https://getrush.ai/run/add-cors/codeigniter/)
- [Add CSRF Protection to a CodeIgniter 4 App](https://getrush.ai/run/add-csrf-protection/codeigniter/)
- [Add Database Migrations to a CodeIgniter 4 App](https://getrush.ai/run/add-database-migrations/codeigniter/)

## The same job on other stacks

- [Write PHPUnit Tests for a Symfony App](https://getrush.ai/run/write-phpunit-tests/symfony/)
- [Write PHPUnit Tests for a CakePHP App](https://getrush.ai/run/write-phpunit-tests/cakephp/)

## More jobs like this: Write the missing tests

- [Write Playwright Tests for an Angular App](https://getrush.ai/run/write-playwright-tests/angular/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
