# Write Unit Tests for a Laravel Project

An agent adds 18 Pest tests for five low-coverage Laravel.io classes, raising statement coverage from 74.60% to 77.01% with no production code changed.

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

## What the agent does

Before it could rank anything, the agent had to make coverage measurable: laravelio/laravel.io's `phpunit.xml` has no `<source>` block. It ran Pest against an untracked copy, `phpunit.cov.xml`, that adds `app/`, and kept that file out of the diff. From the clover report it set aside the Filament widgets and tables at 0%, which are chart and column configuration.

The five it picked: `app/Concerns/PreparesSearch.php` (0 of 19 statements), `app/Notifications/SlowQueryLogged.php` (0/12), `app/Http/Controllers/Settings/NotificationSettingsController.php` (0/7), and the Livewire components `RefreshAvatar` (2/15) and `LikeArticle` (1/8). Two first drafts failed on the tests' own mistakes: a wrong search-splitter expectation and a nonexistent `likesCount()` method.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Run the test suite with coverage, pick the five least-covered files that hold real logic, and write Pest tests for them that exercise behaviour, not implementation details. Do not change production code unless a test reveals a bug; if it does, say so in the description. Open a pull request with the coverage before and after.",
  "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

Tried on laravelio/laravel.io at 55be0d80: 5 files changed, 224 insertions(+), all new files under `tests/`. The new tests alone ended `Tests:    18 passed (61 assertions)`; the run did not capture that exit code. The full suite exited 2 before and after with the same 14 failures; after the change it ended `Tests:    14 failed, 1 skipped, 472 passed (1133 assertions)`, and covered statements went from 1950 to 2013 of 2614. The run installed PHP 8.5, pcov and MariaDB, reset MariaDB's root password to the `.env.example` value after a first 88-failure baseline, and built the Vite assets. No pull request was opened.

For the reviewer: check the 14 failures on real MySQL, which the run could not test. `PreparesSearch` joins merged paragraphs with one `\r\n` where the input had two, and the tests pin that behaviour, so say if it is unintended. Pint was not run.

## Run it on every night

An automation with a `schedule` trigger, `cron` `0 2 * * *` and your `timezone`, runs this prompt in the Project's repository each night. Failing checks end a run `needs_review`, and on this trial's MariaDB the full suite exits 2 before any new test is written, and the run could not confirm MySQL clears it, so expect each morning's run to need a look until that is settled.

## Why run it on Rush?

Laravel.io ships no coverage configuration, so the run added the `<source>` block outside the diff, then looked past a 0% list full of Filament configuration to classes with branches, like the search splitter that cuts a chunk over 5000 characters on single new lines.

## Frequently asked questions

### Why does the full suite still fail?

The run traced all 14 to the sandbox's MariaDB, whose `ONLY_FULL_GROUP_BY` rejects a `group by users.id` query (error 1055 on `users.github_id`), while `phpunit.xml` targets MySQL. Loosening `sql_mode` did not help.

### Did any new test find a bug?

No, and no production code changed. `LikeArticle` stays at 7 of 8 statements: the `likeToggled()` listener's `return $this->article` is the one line left untested.

## More Laravel jobs

- [Add API Rate Limiting to a Laravel App from a Linear Issue](https://getrush.ai/run/add-api-rate-limit/laravel/)
- [Address Review Comments on a Laravel Pull Request](https://getrush.ai/run/answer-review-comments/laravel/)
- [Turn an Asana Task into a Pull Request for a Laravel App](https://getrush.ai/run/asana-task-to-pr/laravel/)

## The same job on other stacks

- [Write Unit Tests for an MCP Server](https://getrush.ai/run/write-unit-tests/mcp-server/)
- [Write Unit Tests for a Medusa Backend](https://getrush.ai/run/write-unit-tests/medusa/)
- [Write Unit Tests for a Node.js App](https://getrush.ai/run/write-unit-tests/nodejs/)

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