JSON Schema schema assertions for PHP. Uses swaggest/php-json-schema under the hood.
You can install the package via composer:
> composer require --dev sixlive/json-schema-assertions
If you are making use of external schema refrences e.g. $ref: 'bar.json
, you must reference the schema through file path or using the config path resolution.
├── schemas
│ ├── bar.json
│ └── foo.json
You can either use the AssertsJsonSchema
trait or manually construct the schema assertion.
use sixlive\JsonSchemaAssertions\Concerns\AssertJsonSchema;
class ExampleTest extends TestCase
{
use AssertsJsonSchema;
public function setUp()
{
parent::setUp();
$this->setJsonSchemaBasePath(__DIR__.'/../Schemas');
}
/** @test */
function it_has_a_valid_response()
{
$this->schemaAssertion
->schema('foo')
->assert('{"foo": "bar"}');
}
}
/** @test */
public function it_has_a_valid_response()
{
$schema = [
'type' => 'object',
'properties' => [
'foo' => [
'type' => 'string',
],
],
'required' => [
'foo',
],
];
// Schema as an array
(new SchemaAssertion)->schema($schema)->assert('{"foo": "bar"}');
// Schema from raw JSON
(new SchemaAssertion)->schema(json_encode($schema))->assert('{"foo": "bar"}');
// Schema from a file
(new SchemaAssertion)->schema(__DIR__.'/../schemas/foo.json'))
->assert('{"foo": "bar"}');
// Remote schema
(new SchemaAssertion)->schema('https://docs.foo.io/schemas/foo.json')
->assert('{"foo": "bar"}')
// Schema from a path
(new SchemaAssertion(__DIR__.'/../schemas/'))
->schema('foo')
->assert('{"foo": "bar"}');
}
> composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
In addition to the php-cs-fixer rules, StyleCI will apply the Laravel preset.
> composer styles:lint
> composer styles:fix
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.