rouxtaccess/laravel-openapi-test

1,420 1
Install
composer require rouxtaccess/laravel-openapi-test
Latest Version:1.4.0
PHP:>=7.4|>=8.0
License:MIT
Last Updated:Jun 23, 2025
Links: GitHub  ·  Packagist
Maintainer: RouxtAccess

Laravel OpenApi Test

Underlying logic uses PHP Swagger Test from byjg Built for use with L5-Swagger This was based upon Laravel Swagger Test from pionl

Test your routes using Laravel's underlying request testing against your API schema.

Support

For how the assertions work against your documentation, please check the PHP Swagger Test.

Currently, this only supports json api's, it should be very easy to override any required functionality

Install

  1. Require the package

    composer require --dev rouxtaccess/laravel-openapi-test
    

Usage

Use the Laravel's TestCase and add the ImplementsOpenApiFunctions trait.

Add $this->setUpOpenApiTester(); to your test's setUp function

Uses same "request building" as ApiRequester. For more details check the PHP Swagger Test.

For validation and testing, there are methods for validateRequest(), validateRequestFails(), sendRequest(), validateResponse(Response::HTTP_OK);

For asserting response data on top of the OpenApi required spec you can use the assertResponseHas() helper method See example below:

<?php

namespace Tests\Feature\Api;

use App\User;
use RouxtAccess\OpenApi\Testing\Laravel\Traits\ImplementsOpenApiFunctions;
use Symfony\Component\HttpFoundation\Response;
use Tests\TestCase;

class AuthLoginTest extends TestCase
{
   use ImplementsOpenApiFunctions;
   
   protected function setUp(): void
   {
       parent::setUp();
       $this->setUpOpenApiTester();
   }

   public function testLoginWithoutDetails()
   {
       $this->requester->withMethod('POST')
           ->withPath('/api/auth/login');

       $this->validateRequestFails()
           ->sendRequest()
           ->validateResponse(Response::HTTP_UNPROCESSABLE_ENTITY);

       $this->assertResponseHas('errors.email');
       $this->assertResponseHas('errors.password');
   }

   public function testLoginIncorrectDetails()
   {
       $this->requester->withMethod('POST')
           ->withPath('/api/auth/login')
           ->withRequestBody(['email' => 'not_a_real_users_email@notreal.com', 'password' => 'not_a_valid_password']);

       $this->validateRequestFails()
           ->sendRequest()
           ->validateResponse(Response::HTTP_UNAUTHORIZED);
   }

   public function testLoginSuccess()
   {
       $user = factory(User::class)->create(['name' => 'test-user', 'email' => 'testemail@example.com', 'password' => bcrypt('bestpassword')]);

       $this->requester->withMethod('POST')
           ->withPath('/api/auth/login')
           ->withRequestBody(['email' => $user->email, 'password' => 'bestpassword']);

       $this->validateRequest()
           ->sendRequest()
           ->validateResponse(Response::HTTP_OK);
   }
}

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

637,827,751 9,703
laravel/framework

The Laravel Framework.

589,172,971 34,950
laravel/tinker

Powerful REPL for the Laravel framework.

498,216,183 7,442
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

413,412,504 609
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

394,964,158 4,660

Version History

Version Released PHP Laravel License
1.4.0 >=7.4|>=8.0 * MIT
1.3.3 >=7.4|>=8.0 * MIT
1.3.2 ^7.4|^8.0 * MIT
1.3.1 ^7.4 * MIT
1.3.0 ^7.4 * MIT