rouxtaccess/laravel-openapi-test

1,420 1
Install
composer require rouxtaccess/laravel-openapi-test
Latest Version:1.4.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

gabrieloliverio/laravel5-generators

Database metadata-based generators for Laravel 5

1
erirk/paypalpayment

laravel-paypalpayment is simple package help you process direct credit card paym...

0
doctrine/dbal

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

628,058,007 9,707
laravel/framework

The Laravel Framework.

576,155,700 34,907
laravel/tinker

Powerful REPL for the Laravel framework.

485,678,092 7,440