gammabeam82/schema-checker

无外部依赖的简单模式验证库。

1.0.0 2018-12-27 10:05 UTC

This package is auto-updated.

Last update: 2024-09-28 04:00:23 UTC


README

Build Status

描述

此库提供了一种简单的方法来验证API响应。

安装

composer require --dev gammabeam82/schema-checker

用法

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()
        );
    }
}