All jobs

Write the Missing PHPUnit Tests for a Slim 4 API

Yes. An agent finds the Slim 4 classes your PHPUnit suite never reaches, the error handler and middleware, writes tests in your style and opens a PR with coverage.

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 measures before it writes. In an API laid out like slimphp/Slim-Skeleton, composer.json requires PHP ^7.4 || ^8.0, slim/slim ^4.12, slim/psr7 ^1.6, php-di/php-di ^6.4 and monolog/monolog ^2.9, and for development phpunit/phpunit ^9.6.17, phpspec/prophecy-phpunit ^2.2, phpstan/phpstan ^1.10 at level 4 over src/, and squizlabs/php_codesniffer with a PSR-12 ruleset over src and tests. phpunit.xml boots tests/bootstrap.php and whitelists ./src/ with processUncoveredFilesFromWhitelist, so files no test loads still count as zero. The agent installs PHP 8.2 and Composer in its sandbox, runs composer install, then XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text, and keeps the table.

What the table shows is a suite that covers the happy path and little else. tests/Application/Actions/User/ListUserActionTest.php and ViewUserActionTest.php prophesy UserRepository, set it on the container and compare the JSON body to an ActionPayload; the view test also builds a real HttpErrorHandler and ErrorMiddleware to check the 404 for UserNotFoundException. ActionTest.php checks that respond() and respondWithData() carry a 202. UserTest.php and InMemoryUserRepositoryTest.php cover the domain. Untouched: the six other branches of HttpErrorHandler::respond() (HttpMethodNotAllowedException to NOT_ALLOWED, HttpUnauthorizedException to UNAUTHENTICATED, HttpForbiddenException to INSUFFICIENT_PRIVILEGES, HttpBadRequestException to BAD_REQUEST, HttpNotImplementedException to NOT_IMPLEMENTED, and a plain Throwable whose message is shown only when displayErrorDetails is true), Action::resolveArg() throwing HttpBadRequestException for a missing route argument, ActionPayload::jsonSerialize() choosing between data, error and neither, ActionError's setters, Settings::get() with and without a key, SessionMiddleware, ResponseEmitter and the two closures in app/routes.php.

The agent writes one test class per gap, in the shape the existing ones use: Tests\TestCase::getAppInstance() for anything that goes through routing, createRequest() for the request, and Prophecy for the repository. tests/Application/Handlers/HttpErrorHandlerTest.php registers a throwaway route per exception type, attaches the error middleware the way ViewUserActionTest does, and asserts the status code and the type in the JSON body for all six, then the same route with displayErrorDetails true and false to pin the two 500 messages. ActionTest gains a case where a route declared with {id} is handled by an action that calls resolveArg('missing') and answers 400 with BAD_REQUEST, and a case that posts a JSON body and reads it back through getFormData(), which needs addBodyParsingMiddleware() on the app. ActionPayloadTest and ActionErrorTest are plain unit tests of json_encode output. SettingsTest checks that an empty key returns the whole array. UserTest gets the case the data provider never tries: new User(1, 'Bill.Gates', 'bill', 'gates') comes back as bill.gates, Bill and Gates, which is the only logic the constructor has. RoutesTest asserts GET / says Hello world! and that OPTIONS /anything answers 200 with an empty body.

Two classes get a smaller test than their size suggests, and the pull request says why. SessionMiddleware::process() calls session_start() when HTTP_AUTHORIZATION is set, which under PHPUnit's CLI has already sent output, so its test runs under @runInSeparateProcess and asserts the session request attribute is present with the header and absent without it. ShutdownHandler reads error_get_last() and emits a response, which cannot be triggered from inside a test without a fatal error; the agent tests only the early return when there is no last error and lists the handler as the one file still uncovered. Nothing under src/ changes, every new file passes vendor/bin/phpcs and vendor/bin/phpstan, and the test code stays within PHP 7.4 syntax because .github/workflows/tests.yml still runs the suite on 7.4, 8.0, 8.1 and 8.2.

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 Slim 4 API. Read composer.json, phpunit.xml, phpcs.xml, phpstan.neon.dist, .github/workflows/tests.yml, public/index.php, everything under app/ and src/, and every file under tests/ first. Install PHP and Composer, run composer install, then run XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text and keep the table. Write tests for: every exception branch of HttpErrorHandler::respond and both displayErrorDetails values; Action::resolveArg with a missing argument and getFormData with a parsed JSON body; ActionPayload and ActionError JSON output; Settings::get with and without a key; User's username lowercasing and name capitalisation; SessionMiddleware with and without HTTP_AUTHORIZATION, in a separate process; the GET / and OPTIONS catch-all routes. Use Tests\\TestCase, createRequest and Prophecy the way the existing tests do, keep the syntax PHP 7.4 compatible, and do not change anything under src/. If a test shows behaviour that looks wrong, keep the test on current behaviour and raise it in the description. Run vendor/bin/phpcs, vendor/bin/phpstan and the coverage run again, then open a pull request with the coverage table before and after and the list of what is still uncovered and why.",
  "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 classes under tests/Application/Handlers/, tests/Application/Actions/, tests/Application/Middleware/ and tests/Application/Settings/, extends ActionTest.php, UserTest.php and adds a RoutesTest.php, and changes nothing under src/. The description pastes the --coverage-text table from before and after, the phpcs and phpstan results, and names the one file still at zero, ShutdownHandler.php, with the reason.

It also lists what the tests pinned that a maintainer may want to change: ResponseEmitter reflects whatever HTTP_ORIGIN the client sent into Access-Control-Allow-Origin together with Access-Control-Allow-Credentials: true, app/settings.php ships with displayErrorDetails true, and the 500 handler reveals the exception message whenever that flag is on. Each is a question in the description, not a code change.

Before merging, a reviewer checks that the separate-process test is marked so it does not slow the rest of the suite, that the new classes read like the old ones, and that the matrix in .github/workflows/tests.yml passes on PHP 7.4 as well as 8.2, since the sandbox ran one version and CI runs four.

Run it on every night

An automation with a schedule trigger, cron 0 2 * * * in your timezone, runs this prompt in the Project that holds the API each night. Because the run starts from the coverage table, the first night takes the handler and payload classes above, and later nights take whatever the day's commits added without tests, so the suite follows the code rather than trailing it.

Each night opens one pull request on its own branch with its before-and-after table, and a night that finds nothing a unit test can reach ends with a short note and no branch. max_runs_per_hour on the automation keeps a mis-set schedule from starting a second run.

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

Why run it on Rush?

The gaps in a Slim skeleton stay open because each one needs a small trick: an error branch needs a route that throws exactly that exception behind the real middleware, the session middleware needs a process of its own, and the body parser has to be registered before getFormData() returns anything. Working those out by hand costs an afternoon for a few hundred lines of coverage. The agent does it in a sandbox with the project's own PHP, Composer and PHPUnit, and the transcript keeps every command and its output.

The result is a pull request a reviewer can judge from the description alone: the coverage table moved, the linters passed, and the behaviours the tests froze are listed as questions. Several services can be covered the same night, each in its own sandbox, without anyone installing Xdebug on a laptop.

Frequently asked questions

Why does ShutdownHandler stay uncovered?

Its __invoke() reads error_get_last(), and the only way to make that return something is a real PHP fatal error, which ends the test process. The agent writes the one test it can, that the handler returns without emitting anything when there is no last error, and says in the pull request that the message formatting for E_USER_ERROR, E_USER_WARNING and E_USER_NOTICE is still untested.

Does it need Xdebug or PCOV?

One of them, for the coverage table only. The sandbox installs Xdebug and runs PHPUnit with XDEBUG_MODE=coverage, which is also what shivammathur/setup-php provides in the repository's workflow with coverage: xdebug. The tests themselves run without either.

Will the tests pass on PHP 7.4?

The agent keeps the new tests to syntax PHP 7.4 accepts: no enums, no readonly, no named arguments, since the workflow matrix still includes 7.4. It runs on one PHP version in the sandbox and says so; the matrix is what proves the other three.

Why not mock the Slim App and test the actions in isolation?

The existing tests build the real app from app/settings.php, app/dependencies.php and app/repositories.php and replace only the repository, so the routing, the container and the error middleware are exercised too. The new tests follow that pattern. A test that mocked App would prove how the mock was written, not how the handler behaves behind Slim's middleware stack.

What if a test exposes a bug?

The test asserts what the code does today and the pull request lists it. In this skeleton the clear cases are the open CORS reflection in ResponseEmitter and the displayErrorDetails default. Changing them is a decision for the maintainer, and the test makes the current behaviour visible until that decision is made.

More Slim jobs

The same job on other stacks

Keep reading