Skip to content

spatie/pest-plugin-route-testing

Repository files navigation

Make sure all routes in your Laravel app are ok

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();
});

Support us

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.

Installation

You can install the package via composer:

composer require spatie/pest-plugin-route-testing

Usage

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();
});

Debugging

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:

Excluding routes

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.

Including routes

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();

Combining excludes and includes

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.

Route model binding

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!

Custom assertions

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();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.