1. Go to this page and download the library: Download radebatz/openapi-verifier library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
radebatz / openapi-verifier example snippets
use Radebatz\OpenApi\Verifier\OpenApiVerifier;
$verifier = OpenApiVerifier::fromSpecification(__DIR__ . '/specifications/users.yaml');
// throws `OpenApiSchemaMismatchException` if the body does not match;
// returns `false` if the specification has no schema for this response
$verifier->verify($request, $response);
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Radebatz\OpenApi\Verifier\Adapters\Guzzle\OpenApiVerifierMiddleware;
use Radebatz\OpenApi\Verifier\OpenApiVerifier;
$stack = HandlerStack::create();
$stack->push(new OpenApiVerifierMiddleware(OpenApiVerifier::fromSpecification('openapi.yaml')), 'openapi-verifier');
$client = new Client(['handler' => $stack, 'base_uri' => 'http://localhost:8080']);
$client->get('/user/1'); // verified against `/user/{id}`
use Radebatz\OpenApi\Verifier\Adapters\Psr18\OpenApiVerifyingClient;
$client = new OpenApiVerifyingClient($psr18Client, OpenApiVerifier::fromSpecification('openapi.yaml'));
$response = $client->sendRequest($request);
namespace Tests\Feature;
use Radebatz\OpenApi\Verifier\VerifiesOpenApi;
use Radebatz\OpenApi\Verifier\OpenApiSpecificationLoader;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\Test;
class UsersTest extends TestCase
{
use VerifiesOpenApi;
/** @inheritdoc */
protected function getOpenApiSpecificationLoader(): ?OpenApiSpecificationLoader
{
return new OpenApiSpecificationLoader(__DIR__ . '/specifications/users.yaml');
}
#[Test]
public function index()
{
// PSR client
$client = $this->client();
$response = $client->get('/users');
$this->assertEquals(200, $response->getStatusCode());
// will throw OpenApiSchemaMismatchException if verification fails
$this->verifyOpenApiResponseBody('get', '/users', 200, (string) $response->getBody());
}
}
use Radebatz\OpenApi\Verifier\Adapters\Psr15\OpenApiVerifierMiddleware;
use Radebatz\OpenApi\Verifier\OpenApiVerifier;
$app->add(new OpenApiVerifierMiddleware(OpenApiVerifier::fromSpecification('openapi.yaml')));
namespace Tests\Feature;
use Radebatz\OpenApi\Verifier\Adapters\Laravel\OpenApiResponseVerifier;
use Tests\TestCase;
use PHPUnit\Framework\Attributes\Test;
class UsersTest extends TestCase
{
use OpenApiResponseVerifier;
public function setUp(): void
{
parent::setUp();
$this->registerOpenApiVerifier(/* $this->>createApplication() */ /* , [specification filename] */);
}
#[Test]
public function index()
{
// will `fail` if schema found and validation fails
$response = $this->get('/users');
$response->assertOk();
}
}
namespace Tests\Functional;
use ...
use Radebatz\OpenApi\Verifier\Adapters\Slim\OpenApiResponseVerifier;
use PHPUnit\Framework\TestCase;
class BaseTestCase extends TestCase
{
use OpenApiResponseVerifier;
public function runApp($requestMethod, $requestUri, $requestData = null)
{
...
$app = new App();
// register OpenApi verifier, before or after the routing middleware
$this->registerOpenApiVerifier($app, __DIR__ . '/../specifications/users.yaml');
$app->addRoutingMiddleware();
// ...
}
}
namespace Tests\Functional;
use PHPUnit\Framework\Attributes\Test;
class UsersTest extends BaseTestCase
{
#[Test]
public function index()
{
// will `fail` if schema found and validation fails
$response = $this->runApp('GET', '/users');
$this->assertEquals(200, $response->getStatusCode());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.