# Write Codeception Tests for a Yii2 App

Yes. An agent reads your Yii2 app's Codeception suites, finds the branches no test reaches, writes unit, functional and acceptance tests for them and opens a PR.

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

## What the agent does

A Yii2 app generated from the basic template arrives with three Codeception suites, so the work is finding what they miss and putting each new test in the right one. In a repository laid out like yiisoft/yii2-app-basic, `codeception.yml` sets the namespace `app\tests`, loads the `Yii2` module with `config/test.php`, and enables coverage over `commands`, `controllers`, `mail`, `models`, `views` and `widgets`. `tests/Unit.suite.yml` gives unit tests the `Asserts` module and three parts of `Yii2`. `tests/Functional.suite.yml` runs the whole application in process, with no web server. `tests/Acceptance.suite.yml` drives `PhpBrowser` through `web/index-test.php`, and the `php-builtin` environment starts `php -S localhost:8080 -t web` for it. The agent installs a coverage driver, runs the README's coverage command, `vendor/bin/codecept run --coverage --coverage-html --coverage-xml --env php-builtin`, and reads the report beside the source.

Reading the source against the existing tests already shows where the gaps are, and the report confirms them. `commands/HelloController.php` sits inside the coverage paths and no test names it. `SiteController::behaviors()` restricts `logout` to POST with a `VerbFilter` and to signed-in users with `AccessControl`, but `tests/Unit/LogoutTest.php` calls `actionLogout()` directly, which bypasses both filters. `actionLogin` sends an already signed-in user home, and no test visits the login page while signed in. `tests/Unit/Models/ContactFormTest.php` has one method, for the path where mail is sent; nothing asserts that an invalid form returns `false` and sends nothing. `LoginForm::login` passes thirty days to `Yii::$app->user->login` when `rememberMe` is set and zero otherwise, and the string `rememberMe` appears in no test. `User::findByUsername` matches with `strcasecmp`, yet `UserTest` only looks up `admin` in lower case.

Each gap goes to the suite that can reach it. Filters only run when a request goes through the application, so the 405 for a GET to `site/logout` and the redirect for a guest's POST are functional Cests using `FunctionalTester`, next to the existing `LoginFormCest`, which already shows `amLoggedInAs`. The unknown-route case, which should render `views/site/error.php` through the `site/error` action with a 404, goes there too. Model branches are unit tests written with `verify()` like their neighbours: `ContactForm::contact` with a bad email, checked with the `Yii2` module's mail assertions that `ContactFormTest` already uses through `$this->tester`, and `findByUsername('ADMIN')`. The console command is a unit test that builds `HelloController`, captures what `actionIndex` echoes and checks `ExitCode::OK`. The captcha keeps working in tests because `SiteController::actions()` fixes the code to `testme` when `YII_ENV_TEST` is set, and the agent reuses that value as the existing contact tests do.

After adding or changing a suite's modules it would run `vendor/bin/codecept build`, as the comment in each suite file says; for tests that use only what is enabled, no rebuild is needed. It runs `composer tests`, then the coverage command again to compare the two reports, then `composer static` and `composer cs`, because `phpstan.neon` and `phpcs.xml.dist` both include the test directories and the workflows under `.github/workflows` run Codeception on PHP 8.2 through 8.5 with PHPStan and the coding standard beside it. A test that fails because the application misbehaves is kept and reported, not adjusted until it passes.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Write the missing Codeception tests for this Yii2 application. Read composer.json, codeception.yml, the three suite files under tests, tests/Support/envs/php-builtin.yml, config/test.php, web/index-test.php, phpstan.neon, phpcs.xml.dist, the workflows under .github, every file under commands, controllers, models and widgets, and every existing test first. Install PHP 8.2 or later with the dom, gd and intl extensions, a coverage driver and Composer if the sandbox lacks them, and run composer install without committing the cookie validation key it writes into config/web.php. Run vendor/bin/codecept run --coverage --coverage-html --coverage-xml --env php-builtin and record the result and the uncovered lines per file. For each uncovered branch, decide which suite can reach it: unit tests with verify() for model and command logic, functional Cests for anything that depends on controller filters or routing, acceptance only for a full browser journey. Cover at least: the POST-only and signed-in-only rules on logout, the login page while already signed in, an unknown route rendering the error view, ContactForm::contact with invalid input sending no mail, LoginForm with rememberMe on and off, a case-insensitive username lookup, and HelloController. Follow the style of the neighbouring tests. Do not change application code and do not weaken a test to make it pass; report a failing test as a finding. Run composer tests, the coverage command again, composer static and composer cs. Open a pull request with a table of each new test, the branch it covers and its suite, the coverage summary before and after as the tool printed it, and the command outputs.",
  "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

The pull request adds test files under `tests/Unit`, `tests/Functional` and, where a journey needs it, `tests/Acceptance`, and changes nothing outside `tests/`. Its description holds a table with one row per new test: the method it exercises, the branch it reaches and the suite it lives in, with a sentence on why that suite. Under the table are Codeception's coverage summaries from before and after, copied as printed, and the output of `composer tests`, `composer static` and `composer cs`.

A reviewer checks placement first, because a test in the wrong suite can pass while proving nothing: a logout test that calls the action method cannot see the `VerbFilter`. They look for assertions on outcomes and not on markup details that change with the next Bootstrap update, and they confirm that the acceptance suite grew only where a real request cycle was the point. Any test the description lists as failing deserves a close read. The request tells the agent to keep such a test and describe it, so a red check on this pull request means the suite found something in the application.

## Run it on every Sunday night

Create an automation on the Project with this prompt and a `schedule` trigger whose `cron` is `0 22 * * 0` in your `timezone`. Each fire starts one run that measures coverage, writes tests for what merged during the week and opens one pull request for Monday. When coverage shows nothing new to reach, the run says so and opens nothing.

A team that wants the check earlier can add a second automation with a `github` trigger on `pull_request.opened`, marked read only so its runs can comment and cannot push, with a prompt that asks whether each controller filter, model rule or console action the pull request adds has a test in the right suite. The trigger adds the event after the prompt and fills in no placeholder.

## Why run it on Rush?

Finding untested branches is a measurement, and the measurement needs the suites to run with a coverage driver, a PHP built-in server for the acceptance environment and the application booted from `config/test.php`. A Rush run has a sandbox of its own for that. The agent installs what is missing, runs the coverage command before and after, and every command with its output is kept in the run's transcript, so the summaries in the pull request are the tool's own output and can be checked there.

The schedule is what makes it useful over time. Nobody has to remember to look at the coverage report; the run starts on its own, works in a fresh checkout that no developer is using, and leaves a pull request. Repositories that share the template can each have the same automation, and each run works in a sandbox of its own.

## Frequently asked questions

### How does it decide between a unit, functional and acceptance test?

By what has to execute for the branch to be reached. Logic inside a model or a console action is a unit test. Anything decided by `behaviors()`, routing or the error handler needs a request through the application, which the functional suite does in process. The acceptance suite, which goes over HTTP to the built-in server, is kept for journeys where the real request cycle is the thing being checked.

### Does it need a database?

Not for this template. `config/test.php` defines a `db` component, but no model is an ActiveRecord and no existing test opens the connection. In an app with ActiveRecord models, the agent sets up the test database named in `config/test_db.php` and loads rows through fixtures, which the Unit suite's `Yii2` module already enables.

### Will it chase 100 percent coverage?

No. The request asks for branches with behaviour worth pinning down: access rules, validation outcomes, redirects, the remember-me duration. Lines that only render static markup add little, and the agent lists what it chose not to cover so the remaining gap is a known one.

### What happens when a new test fails?

It stays in the pull request, failing, with an explanation. The agent first checks that the test is right by reading the code path again. If the application really does behave differently from what the code's author evidently intended, that is a finding for the team, and the fix is a separate change.

## More Yii jobs

- [Add Role-Based Access Control (RBAC) to a Yii2 App](https://getrush.ai/run/add-rbac/yii/)
- [Add a REST API to a Yii2 App](https://getrush.ai/run/add-rest-api/yii/)
- [Move Yii2 Users From a Hardcoded Array to a Database Table](https://getrush.ai/run/move-users-to-database/yii/)

## More jobs like this: Write the missing tests

- [Write Cypress Component Tests for the Untested Forms in a React App](https://getrush.ai/run/write-component-tests/cypress/)
- [Write Component Tests for an Expo App](https://getrush.ai/run/write-component-tests/expo/)
- [Write Livewire Component Tests for a Laravel App](https://getrush.ai/run/write-component-tests/livewire/)

## 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/)
