This package provides a simple API to test your application's routes with PestPHP.
Here's a quick example:
use function Spatie\RouteTesting\routeTesting;
it('can access all GET routes of the API', function () {
routeTesting()
->debug()
->including(['/api/*'])
->excluding(['/api/comments/*'])
->bind('post', Post::factory()->create())
->assert(function(Response $response) {
// perform custom assertions
})
->toReturnSuccessfulResponse();
});
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/pest-plugin-route-testing
The easiest way to use this package, is to create a RoutesTest.php
file in your tests
directory.
This example checks all GET routes (without route model binding) in your application and ensures they return a 200 HTTP status code or a redirect.
<?php
namespace Tests;
use function Spatie\RouteTesting\routeTesting;
it('can access all GET routes as an admin', function () {
$user = User::factory()->create(['role' => 'admin']);
test()->actingAs($admin);
routeTesting()
->debug()
->toReturnSuccessfulResponse();
});
When first using this package, it can be useful to have additional information, such as how many routes are being covered.
routeTesting()
->debug()
->toReturnSuccessfulResponse();
This should output something like the following:
Exclude specific routes from being tested using the excluding method.
routeTesting()
->excluding(['api/comments', 'api/posts'])
->toReturnSuccessfulResponse();
You can also use wildcards to ignore routes:
routeTesting()
->excluding(['api/comments/*'])
->toReturnSuccessfulResponse();
By default, the package ignores certain routes such as _ignition
and _debugbar
.
You can only run for specific routes by using the including
method:
routeTesting()
->including(['api/comments', 'api/posts'])
->toReturnSuccessfulResponse();
You can also use wildcards to inclue routes:
routeTesting()
->including(['api/*'])
->toReturnSuccessfulResponse();
In this example, routes like api/posts/{post}
are excluded, except for api/posts/{post}/comments
:
routeTesting()
->excluding(['api/posts/*'])
->including(['api/posts/{post}/comments'])
->toReturnSuccessfulResponse();
You can also use a wildcard for both.
Mock route model bindings using the bind
method:
routeTesting()
->bind('user', User::factory()->create())
->toReturnSuccessfulResponse();
By default, routes with unknown bindings are ignored. The debug
option can be handy for this!
Add custom assertions for specific routes using the assert
method:
use Illuminate\Testing\TestResponse;
routeTesting()
->including('api/*')
->assert(function (TestResponse $response) {
$response->assertStatus(201);
})
->toReturnSuccessfulResponse();
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.