gammabeam82/schema-checker

Simple schema validation library with no external dependencies.

1.0.0 2018-12-27 10:05 UTC

This package is auto-updated.

Last update: 2024-04-28 03:09:18 UTC


README

Build Status

Description

This library provides an easy way to validate api responses.

Installation

composer require --dev gammabeam82/schema-checker

Usage

use Gammabeam82\SchemaChecker\SchemaChecker;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class CategoryControllerTest extends WebTestCase
{
    /**
     * @var Client
     */
    protected static $client;
    
    /**
     * @var SchemaChecker
     */
    protected static $schemaChecker;
    
    /**
     * @inheritdoc
     */
    public function setUpBeforeClass()
    {
        static::$client = static::createClient();
        static::$schemaChecker = new SchemaChecker();
    }

    public function testListAction(): void
    {
        static::$client->request('GET', '/api/v1/categories/');
        $response = static::$client->getResponse();
        
        $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
        
        $this->assertTrue(
            static::$schemaChecker->assertDataMatchesSchema($response->getContent(), [
                'id' => 'integer',
                'name' => 'string',
                'image' => 'string|nullable',
                'is_active' => 'boolean',
                'products' => [
                    'nullable' => true,
                    'id' => 'integer',
                    'name' => 'string',
                    'description' => 'string',
                    'images' => [
                        'nullable' => true,
                        'id' => 'integer',
                        'filename' => 'string'
                    ]
                ]
            ]),
            static::$schemaChecker->getViolations()
        );
    }
}