rouxtaccess/laravel-openapi-test is a Laravel package.
It currently has 1 GitHub stars and 1.420 downloads on Packagist (latest version 1.4.0).
Install it with composer require rouxtaccess/laravel-openapi-test.
Discover more Laravel packages by rouxtaccess
or browse all Laravel packages to compare alternatives.
Last updated
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.
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
Require the package
composer require --dev rouxtaccess/laravel-openapi-test
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' => '[email protected]', 'password' => 'not_a_valid_password']);
$this->validateRequestFails()
->sendRequest()
->validateResponse(Response::HTTP_UNAUTHORIZED);
}
public function testLoginSuccess()
{
$user = factory(User::class)->create(['name' => 'test-user', 'email' => '[email protected]', 'password' => bcrypt('bestpassword')]);
$this->requester->withMethod('POST')
->withPath('/api/auth/login')
->withRequestBody(['email' => $user->email, 'password' => 'bestpassword']);
$this->validateRequest()
->sendRequest()
->validateResponse(Response::HTTP_OK);
}
}