PHP code example of tunezilla / openapi-test-validation

1. Go to this page and download the library: Download tunezilla/openapi-test-validation 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/ */

    

tunezilla / openapi-test-validation example snippets




namespace Tests\Feature;

use TuneZilla\OpenAPITestValidation\MakesOpenAPIRequests;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use MakesOpenAPIRequests;

    public function testShow()
    {
        $this->openAPI(resource_path('openapi.yaml'))
            ->json('GET', '/api/settings')
            ->assertSuccessful();
    }
}



namespace Tests\Feature;

use TuneZilla\OpenAPITestValidation\MakesOpenAPIRequests;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use MakesOpenAPIRequests;
    
    public function setUp()
    {
        parent::setUp();
        // if all requests in this testcase use the same schema, you can configure it once in `setUp`
        // see https://phpunit.readthedocs.io/en/9.3/fixtures.html for information about `setUp`
        $this->openAPI(resource_path('openapi.yaml'));
    }

    public function testShow()
    {
        $this->json('GET', '/api/settings')
            ->assertSuccessful();
    }
}



namespace Tests\Feature;

use TuneZilla\OpenAPITestValidation\MakesOpenAPIRequests;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use MakesOpenAPIRequests;

    public function testShow()
    {
        // if your requests use different schemas, you can configure them as needed
        
        // this one uses settings/openapi.yaml
        $this->openAPI(resource_path('settings/openapi.yaml'))
            ->json('GET', '/api/settings')
            ->assertSuccessful();
            
        // this one uses other/openapi.yaml
        $this->openAPI(resource_path('other/openapi.yaml'))
            ->json('GET', '/api/other')
            ->assertSuccessful();
    }
}



namespace Tests\Feature;

use TuneZilla\OpenAPITestValidation\MakesOpenAPIRequests;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use MakesOpenAPIRequests;

    public function testInvalidRequest()
    {
        // you can use `ignoreNextOpenAPIRequest` to allow invalid requests to pass through to your backend.
        // this is useful if you're making sure 422 validation errors are thrown:
        $this->openAPI(resource_path('openapi.yaml'))
            ->ignoreNextOpenAPIRequest()
            ->json('GET', '/api/settings?invalid-param=12345')
            ->assertJsonValidationError(['invalid-param']);
    }
}