1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
<?php
/*
* This file is part of the JsonSchema package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JsonSchema\Tests\Constraints;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Constraints\Factory;
use JsonSchema\SchemaStorage;
use JsonSchema\Uri\UriResolver;
use JsonSchema\Validator;
use PHPUnit\Framework\Attributes\DataProvider;
/**
* @package JsonSchema\Tests\Constraints
*/
abstract class BaseTestCase extends VeryBaseTestCase
{
protected $schemaSpec = 'http://json-schema.org/draft-04/schema#';
protected $validateSchema = false;
#[DataProvider('getInvalidTests')]
public function testInvalidCases($input, $schema, $checkMode = Constraint::CHECK_MODE_NORMAL, $errors = []): void
{
$checkMode = $checkMode === null ? Constraint::CHECK_MODE_NORMAL : $checkMode;
if ($this->validateSchema) {
$checkMode |= Constraint::CHECK_MODE_VALIDATE_SCHEMA;
}
$schemaStorage = new SchemaStorage($this->getUriRetrieverMock(json_decode($schema)));
$schema = $schemaStorage->getSchema('http://www.my-domain.com/schema.json');
if (is_object($schema) && !isset($schema->{'$schema'})) {
$schema->{'$schema'} = $this->schemaSpec;
}
$validator = new Validator(new Factory($schemaStorage, null, $checkMode));
$checkValue = json_decode($input);
$errorMask = $validator->validate($checkValue, $schema);
$this->assertTrue((bool) ($errorMask & Validator::ERROR_DOCUMENT_VALIDATION));
$this->assertGreaterThan(0, $validator->numErrors());
if ([] !== $errors) {
$this->assertEquals($errors, $validator->getErrors(), print_r($validator->getErrors(), true));
}
$this->assertFalse($validator->isValid(), print_r($validator->getErrors(), true));
}
#[DataProvider('getInvalidForAssocTests')]
public function testInvalidCasesUsingAssoc($input, $schema, $checkMode = Constraint::CHECK_MODE_TYPE_CAST, $errors = []): void
{
$checkMode = $checkMode === null ? Constraint::CHECK_MODE_TYPE_CAST : $checkMode;
if ($this->validateSchema) {
$checkMode |= Constraint::CHECK_MODE_VALIDATE_SCHEMA;
}
if (!($checkMode & Constraint::CHECK_MODE_TYPE_CAST)) {
$this->markTestSkipped('Test indicates that it is not for "CHECK_MODE_TYPE_CAST"');
}
$schemaStorage = new SchemaStorage($this->getUriRetrieverMock(json_decode($schema)));
$schema = $schemaStorage->getSchema('http://www.my-domain.com/schema.json');
if (is_object($schema) && !isset($schema->{'$schema'})) {
$schema->{'$schema'} = $this->schemaSpec;
}
$validator = new Validator(new Factory($schemaStorage, null, $checkMode));
$checkValue = json_decode($input, true);
$errorMask = $validator->validate($checkValue, $schema);
$this->assertTrue((bool) ($errorMask & Validator::ERROR_DOCUMENT_VALIDATION));
$this->assertGreaterThan(0, $validator->numErrors());
if ([] !== $errors) {
$this->assertEquals($errors, $validator->getErrors(), print_r($validator->getErrors(), true));
}
$this->assertFalse($validator->isValid(), print_r($validator->getErrors(), true));
}
#[DataProvider('getValidTests')]
public function testValidCases($input, $schema, $checkMode = Constraint::CHECK_MODE_NORMAL): void
{
if ($this->validateSchema) {
$checkMode |= Constraint::CHECK_MODE_VALIDATE_SCHEMA;
}
$schemaStorage = new SchemaStorage($this->getUriRetrieverMock(json_decode($schema)));
$schema = $schemaStorage->getSchema('http://www.my-domain.com/schema.json');
if (is_object($schema) && !isset($schema->{'$schema'})) {
$schema->{'$schema'} = $this->schemaSpec;
}
$validator = new Validator(new Factory($schemaStorage, null, $checkMode));
$checkValue = json_decode($input);
$errorMask = $validator->validate($checkValue, $schema);
$this->assertEquals(0, $errorMask);
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
}
#[DataProvider('getValidForAssocTests')]
public function testValidCasesUsingAssoc($input, $schema, $checkMode = Constraint::CHECK_MODE_TYPE_CAST): void
{
if ($this->validateSchema) {
$checkMode |= Constraint::CHECK_MODE_VALIDATE_SCHEMA;
}
if (!($checkMode & Constraint::CHECK_MODE_TYPE_CAST)) {
$this->markTestSkipped('Test indicates that it is not for "CHECK_MODE_TYPE_CAST"');
}
$schema = json_decode($schema);
$schemaStorage = new SchemaStorage($this->getUriRetrieverMock($schema), new UriResolver());
$schema = $schemaStorage->getSchema('http://www.my-domain.com/schema.json');
if (is_object($schema) && !isset($schema->{'$schema'})) {
$schema->{'$schema'} = $this->schemaSpec;
}
$value = json_decode($input, true);
$validator = new Validator(new Factory($schemaStorage, null, $checkMode));
$errorMask = $validator->validate($value, $schema);
$this->assertEquals(0, $errorMask);
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
}
abstract public function getValidTests(): array;
public function getValidForAssocTests(): array
{
return $this->getValidTests();
}
abstract public function getInvalidTests(): array;
public function getInvalidForAssocTests(): array
{
return $this->getInvalidTests();
}
}
|