<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mimosafa / laravel-openapi-validation-helper example snippets
// tests/TestCase.php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use LaravelOpenAPIValidationHelper\TestCaseHelper;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use TestCaseHelper; // Add this trait
}
protected function setUp(): void
{
parent::setUp();
$this->setUpTransparentlyTest(); // Call this method
}
// tests/Feature/ExampleTest.php
namespace Tests\Feature;
use LaravelOpenAPIValidationHelper\HttpRequestMethod;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use Tests\TestCase;
class ExampleTest extends TestCase
{
// Set these properties in each test method
protected string $path = '/users/1';
protected HttpRequestMethod $operation = HttpRequestMethod::GET;
protected function path(): string
{
return $this->path;
}
protected function operation(): HttpRequestMethod
{
return $this->operation;
}
protected function getValidatorBuilder(): ValidatorBuilder
{
// Specify the path to your openapi.yml
return (new ValidatorBuilder)->fromYamlFile(base_path('tests/openapi.yml'));
}
protected function setUp(): void
{
parent::setUp();
$this->setUpTransparentlyTest();
}
/** @test */
public function a_user_can_be_retrieved(): void
{
// Set properties
$this->path = '/users/1';
$this->operation = HttpRequestMethod::GET;
// Method and URI can be omitted (automatically retrieved from operation() and path())
$response = $this->json();
$response->assertStatus(200);
// Or you can explicitly specify them as before
// $response = $this->getJson('/users/1');
}
}
class ExampleTest extends TestCase
{
protected string $path = '/users/1';
protected HttpRequestMethod $operation = HttpRequestMethod::GET;
// Override the method to return the prefix
protected function prefix(): string
{
return '/api';
}
protected function path(): string
{
return $this->path;
}
protected function operation(): HttpRequestMethod
{
return $this->operation;
}
protected function getValidatorBuilder(): ValidatorBuilder
{
return (new ValidatorBuilder)->fromYamlFile(base_path('tests/openapi.yml'));
}
protected function setUp(): void
{
parent::setUp();
$this->setUpTransparentlyTest();
}
/** @test */
public function a_user_can_be_retrieved(): void
{
$this->path = '/users/1';
$this->operation = HttpRequestMethod::GET;
// Request to /api/users/1, validated against schema /users/{id}
$response = $this->json();
$response->assertStatus(200);
}
}
protected function path(): string
{
return '/users/{id}'; // or a concrete value like '/users/1'
}
protected function operation(): HttpRequestMethod
{
return HttpRequestMethod::GET;
}
protected function getValidatorBuilder(): ValidatorBuilder
{
return (new ValidatorBuilder)->fromYamlFile(base_path('tests/openapi.yml'));
}
protected function prefix(): string
{
return '/api'; // Default is '' (empty string)
}
public function test_with_invalid_request(): void
{
$this->ignoreRequestCompliance();
// Request validation will not run even for an invalid request
$this->postJson('/api/users', []);
}
public function test_with_invalid_response(): void
{
$this->ignoreResponseCompliance();
// Response validation will not run
$this->postJson('/api/users', ['generate_invalid_response' => true]);
}
// Shorthand (recommended)
$this->json(); // Uses operation() and prefix() . path()
// Explicit specification also possible
$this->json('GET', '/api/users/1');
$this->getJson('/api/users/1'); // Traditional usage still works
// Shorthand
$this->call(); // Uses operation() and prefix() . path()
// Explicit specification also possible
$this->call('POST', '/api/users', ['name' => 'John']);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.