All jobs

Write PHPUnit Tests for a CakePHP App

Yes. An agent reads your CakePHP app, finds the middleware, controller branches and templates no test reaches, writes PHPUnit tests and opens a pull request.

Run this on RushOpens 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 before it reads the code. In an app created from the cakephp/app skeleton, phpunit.xml.dist has one suite over tests/TestCase/, loads CakePHP's fixture extension, and counts src/ and plugins/*/src/ as source with src/Console/Installer.php excluded. tests/bootstrap.php loads config/bootstrap.php, sets App.fullBaseUrl to http://localhost when no host is known, freezes time with Chronos::setTestNow, aliases the connections to their test versions and builds the schema with Migrator. composer.json defines test as phpunit --colors=always and check as the tests followed by phpcs, and the test connection defaults to a SQLite file under tmp/.

Then it lists what the existing tests reach. There are three files with thirteen test methods: ApplicationTest checks which plugins load with debug on and off and the position of the first four middleware, PagesControllerTest requests /pages/home, a missing page with debug on and off, a path containing .., and a POST with and without a CSRF token, and DefaultTemplateTest renders the HTML email template. Reading src/ against that list shows the gaps. HostHeaderMiddleware is only ever checked for its position, yet it has four outcomes: pass through in debug, an InternalErrorException when App.fullBaseUrl is empty in production, a BadRequestException when the request host differs from the configured one, and pass through on a match that ignores case. PagesController::display() redirects to / when called with no path, refuses a . segment as well as .., and sets page and subpage view variables; none of the three is asserted. The text email template and AjaxView, which sets the ajax layout and response type, have no test at all.

It writes the missing tests in the style already there. tests/TestCase/Middleware/HostHeaderMiddlewareTest.php builds a ServerRequest for a given host and a stub request handler, switches debug and App.fullBaseUrl with Configure::write, and restores both afterwards so no other test inherits a production setting. PagesControllerTest gains cases for the redirect, the single-dot path and the two view variables, using IntegrationTestTrait as the file already does. DefaultTemplateTest gains the text template, and a short AjaxViewTest asserts the layout and the response type. Each test targets one branch, and the new files are written to pass the CakePHP coding standard, since phpcs.xml checks tests/ as well as src/.

Finally it measures. The CI workflow sets coverage: none, so the agent enables a coverage driver in its sandbox only, runs vendor/bin/phpunit --coverage-text before and after, and reports the line coverage of each file under src/ both times. It runs composer check so the new files pass phpcs. In an app that has grown tables and controllers of its own, the same pass also bakes fixtures into tests/Fixture and tests validation rules, finders and controller actions; Migrator builds their schema from config/Migrations, so no schema is copied into the tests.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Write the missing PHPUnit tests for this CakePHP 5 app. Read phpunit.xml.dist, tests/bootstrap.php, composer.json, phpcs.xml and every file under tests/TestCase first, then every class under src/ and the templates under templates/. List which branches the existing tests reach and which they do not. Add tests in the existing style: a HostHeaderMiddlewareTest covering debug mode, a missing App.fullBaseUrl, a host mismatch and a case-insensitive match, restoring Configure values afterwards; PagesControllerTest cases for the redirect on an empty path, a single-dot segment and the page and subpage view variables; the text email template; AjaxView. If the app has tables or other controllers, bake fixtures and test their validation, finders and actions too. Do not change anything under src/ or templates/: if a test shows a bug, mark that test skipped with a note and describe the bug in the pull request. Enable a coverage driver in the sandbox only, run vendor/bin/phpunit --coverage-text before and after, run composer check, and open a pull request with both coverage tables and the test output.",
  "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 only: a middleware test, a view test, and new methods in PagesControllerTest and DefaultTemplateTest, plus fixtures when the app has tables. Its description holds the two coverage tables, the full PHPUnit output and the phpcs result, and a short list of anything the agent found while writing them, such as a branch that cannot be reached or a behaviour that looks unintended.

Nothing under src/, templates/ or config/ changes, so the diff can be reviewed as tests alone. In a repository that keeps the skeleton's workflow, the testsuite job runs the new tests on PHP 8.2 with the lowest dependency versions and on PHP 8.5 with the highest, which catches a test that leans on a method only a newer CakePHP has.

A reviewer reads the assertions rather than the count. The useful questions are whether the host-mismatch test uses a host your deployment could really see, and whether each test would fail if the branch it names were deleted. A test marked skipped with a note is the agent's way of handing over a suspected bug without changing the code under test.

Run it on every night

In the console, add an automation to the Project with this prompt and a schedule trigger, with cron set to an early hour in your timezone. Each fire starts a run on the default branch as it stands that night, so a controller merged during the day gets its tests by morning, in one pull request on a branch of its own. Set the automation's max runs per hour to 1; a schedule has no reason to overlap itself.

To cover pull requests instead, give the automation a github trigger on pull_request.opened and pull_request.synchronize. The trigger puts the pull request after the prompt, so word the prompt to say "write tests for the classes this pull request changes under src/" and it needs no placeholder. Only work by an owner, member or collaborator fires it.

The use case this job follows, with its automation and trigger: Write the missing tests.

Why run it on Rush?

A test nobody ran is a guess. The agent has a sandbox of its own with PHP and Composer, installs the app's dependencies, lets post-install-cmd create config/app_local.php, and runs PHPUnit after every file it writes, so a test that passes for the wrong reason or leaks a Configure value into the next test shows up while it can still be fixed. The coverage numbers in the pull request come from those commands, and the commands and their output stay with the run for anyone who wants to check them.

Writing tests for middleware and error paths is also work that waits behind features on most teams. A nightly run does it without taking a seat at anyone's desk, and several repositories can each have their own run on the same night, isolated from one another.

Frequently asked questions

Will it change my application code to make it testable?

No. The prompt limits the change to tests/. When a class cannot be tested without a change, or a test exposes a bug, the agent says so in the pull request and leaves the decision to you; a follow-up on the same session can then make that one change.

Does it need a MySQL server for the tests?

Not for an app that keeps the skeleton's defaults: the test connection falls back to a SQLite file, and the CI workflow sets DATABASE_TEST_URL to SQLite too. If your tests need MySQL or Postgres features, say so, and the agent installs that server in its sandbox and points DATABASE_TEST_URL at it.

How does it test code that depends on debug mode?

The way the existing tests do: by writing debug with Configure::write inside the test. The new middleware test also restores debug and App.fullBaseUrl when it finishes, because tests/bootstrap.php sets the base URL once for the whole run and a later test would otherwise see the changed value.

Is the coverage driver added to my project?

No. It is enabled in the sandbox for the two coverage runs and nothing about it is committed. The CI workflow keeps coverage: none; if you want coverage in CI, that is a separate, small request.

More CakePHP jobs

The same job on other stacks

More jobs like this: Write the missing tests

Keep reading