Laravel Smoke Test Guest URLs

In this tutorial we're going to build a test class that will run through all guest URLs defined in our web.php routes and check that they all return a HTTP status of 200. This is allow us to quickly check all open URLs and make sure that there aren't any errors on these page.

Defining Your Routes

When you define your routes in Laravel you'll add them to the routes/web.php. Using the guest middleware will make sure that anybody can access these URLs without having to be logged in. When the URLs are wrapped in the guest middleware we can make sure we only check these URLs.

Create Smoke Test Class

Laravel allows use to make HTTP request tests to where we can make a GET request to all the URLs in our web.php file and assert the status of the response. To build this test class we'll need to do the following.

  • Get all guest routes
  • Loop through each route and check if the URL is dynamic
  • If the URL is dynamic then create a test URL
  • Make a HTTP request to this URL
  • Check if URL returns 200 HTTP status

Get All Guest Routes

To get all routes that are defined in Laravel you can use Illuminate\Routing\Router with the method getRoutes method. This will return all routes defined in your application. We need to then loop through all of these, check the middleware is set to guest, remove any dusk added routes and return these routes.

Convert Dynamic Routes

After we have all the routes we need to then loop through the routes, check if they're dynamic and check a test model to match the dynamic route. First we need to map our dynamic routes to a model to create the route.

Then we can pass in the route into this function, check to see if we need to change the dynamic route, create a new model and return the new route for the model.

The return of dynamic URL mapping function can be used to make a HTTP request then we can check to see if the HTTP status is a 200.

Full Test Class

Below is the full test class to use in your application.