Skip to content

feat: update to latest json schema test suite #821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "3.3.0",
"json-schema/json-schema-test-suite": "1.2.0",
"json-schema/json-schema-test-suite": "^23.2",
"phpunit/phpunit": "^8.5",
"phpspec/prophecy": "^1.19",
"phpstan/phpstan": "^1.12",
Expand All @@ -59,11 +59,11 @@
"type": "package",
"package": {
"name": "json-schema/json-schema-test-suite",
"version": "1.2.0",
"version": "23.2.0",
"source": {
"type": "git",
"url": "https://github.com/json-schema/JSON-Schema-Test-Suite",
"reference": "1.2.0"
"reference": "23.2.0"
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
<directory>./src/JsonSchema/</directory>
</whitelist>
</filter>

<php>
<ini name="memory_limit" value="-1"/>
</php>
</phpunit>
48 changes: 34 additions & 14 deletions tests/Constraints/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class BaseTestCase extends VeryBaseTestCase
/**
* @dataProvider getInvalidTests
*
* @param int-mask-of<Constraint::CHECK_MODE_*> $checkMode
* @param ?int-mask-of<Constraint::CHECK_MODE_*> $checkMode
*/
public function testInvalidCases(string $input, string $schema, ?int $checkMode = Constraint::CHECK_MODE_NORMAL, array $errors = []): void
{
Expand All @@ -35,8 +35,9 @@ public function testInvalidCases(string $input, string $schema, ?int $checkMode
$checkMode |= Constraint::CHECK_MODE_VALIDATE_SCHEMA;
}

$schemaStorage = new SchemaStorage($this->getUriRetrieverMock(json_decode($schema, false)));
$schema = $schemaStorage->getSchema('http://www.my-domain.com/schema.json');
$schema = json_decode($schema, false);
$schemaStorage = new SchemaStorage($this->getUriRetrieverMock($schema));
$schema = $schemaStorage->getSchema($schema->id ?? 'http://www.my-domain.com/schema.json');
if (is_object($schema) && !isset($schema->{'$schema'})) {
$schema->{'$schema'} = $this->schemaSpec;
}
Expand All @@ -45,7 +46,7 @@ public function testInvalidCases(string $input, string $schema, ?int $checkMode
$checkValue = json_decode($input, false);
$errorMask = $validator->validate($checkValue, $schema);

$this->assertTrue((bool) ($errorMask & Validator::ERROR_DOCUMENT_VALIDATION));
$this->assertTrue((bool) ($errorMask & Validator::ERROR_DOCUMENT_VALIDATION), 'Document is invalid');
$this->assertGreaterThan(0, $validator->numErrors());

if ([] !== $errors) {
Expand All @@ -56,8 +57,10 @@ public function testInvalidCases(string $input, string $schema, ?int $checkMode

/**
* @dataProvider getInvalidForAssocTests
*
* @param ?int-mask-of<Constraint::CHECK_MODE_*> $checkMode
*/
public function testInvalidCasesUsingAssoc($input, $schema, $checkMode = Constraint::CHECK_MODE_TYPE_CAST, $errors = []): void
public function testInvalidCasesUsingAssoc(string $input, string $schema, ?int $checkMode = Constraint::CHECK_MODE_TYPE_CAST, array $errors = []): void
{
$checkMode = $checkMode === null ? Constraint::CHECK_MODE_TYPE_CAST : $checkMode;
if ($this->validateSchema) {
Expand All @@ -67,8 +70,9 @@ public function testInvalidCasesUsingAssoc($input, $schema, $checkMode = Constra
$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');
$schema = json_decode($schema, false);
$schemaStorage = new SchemaStorage($this->getUriRetrieverMock($schema));
$schema = $schemaStorage->getSchema($schema->id ?? 'http://www.my-domain.com/schema.json');
if (is_object($schema) && !isset($schema->{'$schema'})) {
$schema->{'$schema'} = $this->schemaSpec;
}
Expand All @@ -88,14 +92,18 @@ public function testInvalidCasesUsingAssoc($input, $schema, $checkMode = Constra

/**
* @dataProvider getValidTests
*
* @param ?int-mask-of<Constraint::CHECK_MODE_*> $checkMode
*/
public function testValidCases($input, $schema, $checkMode = Constraint::CHECK_MODE_NORMAL): void
public function testValidCases(string $input, string $schema, ?int $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');

$schema = json_decode($schema, false);
$schemaStorage = new SchemaStorage($this->getUriRetrieverMock($schema));
$schema = $schemaStorage->getSchema($schema->id ?? 'http://www.my-domain.com/schema.json');
if (is_object($schema) && !isset($schema->{'$schema'})) {
$schema->{'$schema'} = $this->schemaSpec;
}
Expand All @@ -110,8 +118,10 @@ public function testValidCases($input, $schema, $checkMode = Constraint::CHECK_M

/**
* @dataProvider getValidForAssocTests
*
* @param ?int-mask-of<Constraint::CHECK_MODE_*> $checkMode
*/
public function testValidCasesUsingAssoc($input, $schema, $checkMode = Constraint::CHECK_MODE_TYPE_CAST): void
public function testValidCasesUsingAssoc(string $input, string $schema, ?int $checkMode = Constraint::CHECK_MODE_TYPE_CAST): void
{
if ($this->validateSchema) {
$checkMode |= Constraint::CHECK_MODE_VALIDATE_SCHEMA;
Expand All @@ -120,9 +130,9 @@ public function testValidCasesUsingAssoc($input, $schema, $checkMode = Constrain
$this->markTestSkipped('Test indicates that it is not for "CHECK_MODE_TYPE_CAST"');
}

$schema = json_decode($schema);
$schema = json_decode($schema, false);
$schemaStorage = new SchemaStorage($this->getUriRetrieverMock($schema), new UriResolver());
$schema = $schemaStorage->getSchema('http://www.my-domain.com/schema.json');
$schema = $schemaStorage->getSchema($schema->id ?? 'http://www.my-domain.com/schema.json');
if (is_object($schema) && !isset($schema->{'$schema'})) {
$schema->{'$schema'} = $this->schemaSpec;
}
Expand All @@ -131,7 +141,7 @@ public function testValidCasesUsingAssoc($input, $schema, $checkMode = Constrain
$validator = new Validator(new Factory($schemaStorage, null, $checkMode));

$errorMask = $validator->validate($value, $schema);
$this->assertEquals(0, $errorMask);
$this->assertEquals(0, $errorMask, $this->validatorErrorsToString($validator));
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
}

Expand All @@ -148,4 +158,14 @@ public function getInvalidForAssocTests(): array
{
return $this->getInvalidTests();
}

private function validatorErrorsToString(Validator $validator): string
{
return implode(
', ',
array_map(
static function (array $error) { return $error['message']; }, $validator->getErrors()
)
);
}
}
36 changes: 18 additions & 18 deletions tests/Constraints/NumberAndIntegerTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,37 @@ public function getInvalidTests(): array
{
return [
[
'{
'input' => '{
"integer": 1.4
}',
'{
'schema' => '{
"type":"object",
"properties":{
"integer":{"type":"integer"}
}
}'
],
[
'{"integer": 1.001}',
'{
'input' => '{"integer": 1.001}',
'schema' => '{
"type": "object",
"properties": {
"integer": {"type": "integer"}
}
}'
],
[
'{"integer": true}',
'{
'input' => '{"integer": true}',
'schema' => '{
"type": "object",
"properties": {
"integer": {"type": "integer"}
}
}'
],
[
'{"number": "x"}',
'{
'input' => '{"number": "x"}',
'schema' => '{
"type": "object",
"properties": {
"number": {"type": "number"}
Expand All @@ -61,39 +61,39 @@ public function getValidTests(): array
{
return [
[
'{
'input' => '{
"integer": 1
}',
'{
'schema' => '{
"type":"object",
"properties":{
"integer":{"type":"integer"}
}
}'
],
[
'{
'input' => '{
"number": 1.4
}',
'{
'schema' => '{
"type":"object",
"properties":{
"number":{"type":"number"}
}
}'
],
[
'{"number": 1e5}',
'{
'input' => '{"number": 1e5}',
'schema' => '{
"type": "object",
"properties": {
"number": {"type": "number"}
}
}'
],
[
'{"number": 1}',
'{
'input' => '{"number": 1}',
'schema' => '{
"type": "object",
"properties": {
"number": {"type": "number"}
Expand All @@ -102,8 +102,8 @@ public function getValidTests(): array
}'
],
[
'{"number": -49.89}',
'{
'input' => '{"number": -49.89}',
'schema' => '{
"type": "object",
"properties": {
"number": {
Expand Down
2 changes: 1 addition & 1 deletion tests/Constraints/VeryBaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class VeryBaseTestCase extends TestCase
protected function getUriRetrieverMock(?object $schema): object
{
$uriRetriever = $this->prophesize(UriRetrieverInterface::class);
$uriRetriever->retrieve('http://www.my-domain.com/schema.json')
$uriRetriever->retrieve($schema->id ?? 'http://www.my-domain.com/schema.json')
->willReturn($schema)
->shouldBeCalled();

Expand Down
22 changes: 22 additions & 0 deletions tests/Drafts/Draft3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ protected function getFilePaths(): array
];
}

public function getInvalidTests(): array
{
$tests = parent::getInvalidTests();
unset(
$tests['ref.json / $ref prevents a sibling id from changing the base uri / $ref resolves to /definitions/base_foo, data does not validate']
);

return $tests;
}

public function getInvalidForAssocTests(): array
{
$tests = parent::getInvalidForAssocTests();
Expand All @@ -92,6 +102,17 @@ public function getInvalidForAssocTests(): array
return $tests;
}

public function getValidTests(): array
{
$tests = parent::getValidTests();
unset(
$tests['ref.json / $ref prevents a sibling id from changing the base uri / $ref resolves to /definitions/base_foo, data validates'],
$tests['ref.json / naive replacement of $ref with its destination is not correct / match the enum exactly']
);

return $tests;
}

public function getValidForAssocTests(): array
{
$tests = parent::getValidForAssocTests();
Expand All @@ -111,6 +132,7 @@ protected function getSkippedTests(): array
return [
// Optional
'bignum.json',
'ecmascript-regex.json',
'format.json',
'jsregex.json',
'zeroTerminatedFloats.json'
Expand Down
41 changes: 41 additions & 0 deletions tests/Drafts/Draft4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,60 @@ protected function getFilePaths(): array
];
}

public function getInvalidTests(): array
{
$tests = parent::getInvalidTests();
unset(
$tests['id.json / id inside an enum is not a real identifier / no match on enum or $ref to id'],
$tests['ref.json / $ref prevents a sibling id from changing the base uri / $ref resolves to /definitions/base_foo, data does not validate'],
$tests['ref.json / Recursive references between schemas / invalid tree'],
$tests['ref.json / refs with quote / object with strings is invalid'],
$tests['ref.json / Location-independent identifier / mismatch'],
$tests['ref.json / Location-independent identifier with base URI change in subschema / mismatch'],
$tests['ref.json / empty tokens in $ref json-pointer / non-number is invalid'],
$tests['ref.json / id must be resolved against nearest parent, not just immediate parent / non-number is invalid'],
$tests['refRemote.json / Location-independent identifier in remote ref / string is invalid']
);

return $tests;
}

public function getInvalidForAssocTests(): array
{
$tests = parent::getInvalidForAssocTests();
unset(
$tests['ref.json / Recursive references between schemas / valid tree'],
$tests['type.json / object type matches objects / an array is not an object'],
$tests['type.json / array type matches arrays / an object is not an array']
);

return $tests;
}

public function getValidTests(): array
{
$tests = parent::getValidTests();
unset(
$tests['ref.json / $ref prevents a sibling id from changing the base uri / $ref resolves to /definitions/base_foo, data validates'],
$tests['ref.json / Recursive references between schemas / valid tree'],
$tests['ref.json / refs with quote / object with numbers is valid'],
$tests['ref.json / Location-independent identifier / match'],
$tests['ref.json / Location-independent identifier with base URI change in subschema / match'],
$tests['ref.json / empty tokens in $ref json-pointer / number is valid'],
$tests['ref.json / naive replacement of $ref with its destination is not correct / match the enum exactly'],
$tests['refRemote.json / Location-independent identifier in remote ref / integer is valid']
);

return $tests;
}

public function getValidForAssocTests(): array
{
$tests = parent::getValidForAssocTests();
unset(
$tests['minProperties.json / minProperties validation / ignores arrays'],
$tests['required.json / required properties whose names are Javascript object property names / ignores arrays'],
$tests['required.json / required validation / ignores arrays'],
$tests['type.json / object type matches objects / an array is not an object'],
$tests['type.json / array type matches arrays / an object is not an array']
);
Expand All @@ -58,7 +97,9 @@ protected function getSkippedTests(): array
return [
// Optional
'bignum.json',
'ecmascript-regex.json',
'format.json',
'float-overflow.json',
'zeroTerminatedFloats.json',
// Required
'not.json' // only one test case failing
Expand Down
Loading
Loading