Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

class Draft06Constraint extends Constraint
{
private const KEYWORD_SCHEMA_PROPERTIES = [
'ref' => ['$ref'],
];

public function __construct(?\JsonSchema\Constraints\Factory $factory = null)
{
parent::__construct(new Factory(
Expand Down Expand Up @@ -66,10 +70,6 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
$this->checkForKeyword('pattern', $value, $schema, $path, $i);
}

private const KEYWORD_SCHEMA_PROPERTIES = [
'ref' => ['$ref'],
];

/**
* @param mixed $value
* @param mixed $schema
Expand Down
12 changes: 6 additions & 6 deletions src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

class Draft07Constraint extends Constraint
{
private const KEYWORD_SCHEMA_PROPERTIES = [
'ref' => ['$ref'],
'ifThenElse' => ['if'],
'content' => ['contentMediaType', 'contentEncoding'],
];

public function __construct(?\JsonSchema\Constraints\Factory $factory = null)
{
parent::__construct(new Factory(
Expand Down Expand Up @@ -68,12 +74,6 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
$this->checkForKeyword('content', $value, $schema, $path, $i);
}

private const KEYWORD_SCHEMA_PROPERTIES = [
'ref' => ['$ref'],
'ifThenElse' => ['if'],
'content' => ['contentMediaType', 'contentEncoding'],
];

/**
* @param mixed $value
* @param mixed $schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

class Draft2019Constraint extends Constraint
{
private const KEYWORD_SCHEMA_PROPERTIES = [
'ref' => ['$ref'],
'ifThenElse' => ['if'],
'content' => ['contentMediaType', 'contentEncoding'],
];

public function __construct(?\JsonSchema\Constraints\Factory $factory = null)
{
parent::__construct(new Factory(
Expand Down Expand Up @@ -69,12 +75,6 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
$this->checkForKeyword('content', $value, $schema, $path, $i);
}

private const KEYWORD_SCHEMA_PROPERTIES = [
'ref' => ['$ref'],
'ifThenElse' => ['if'],
'content' => ['contentMediaType', 'contentEncoding'],
];

/**
* @param mixed $value
* @param mixed $schema
Expand Down
2 changes: 1 addition & 1 deletion tests/Constraints/Drafts/Draft06/Draft06ConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use JsonSchema\Constraints\BaseConstraint;
use JsonSchema\Constraints\ConstraintInterface;
use JsonSchema\Constraints\Drafts\Draft06\Draft06Constraint;
use JsonSchema\Constraints\Factory;
use JsonSchema\Constraints\Drafts\Draft06\Factory;
use PHPUnit\Framework\TestCase;

class Draft06ConstraintTest extends TestCase
Expand Down
2 changes: 1 addition & 1 deletion tests/Constraints/Drafts/Draft07/Draft07ConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use JsonSchema\Constraints\BaseConstraint;
use JsonSchema\Constraints\ConstraintInterface;
use JsonSchema\Constraints\Drafts\Draft07\Draft07Constraint;
use JsonSchema\Constraints\Factory;
use JsonSchema\Constraints\Drafts\Draft07\Factory;
use PHPUnit\Framework\TestCase;

class Draft07ConstraintTest extends TestCase
Expand Down
17 changes: 13 additions & 4 deletions tests/Constraints/Drafts/Draft2019/Draft2019ConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use JsonSchema\Constraints\BaseConstraint;
use JsonSchema\Constraints\ConstraintInterface;
use JsonSchema\Constraints\Drafts\Draft2019\Draft2019Constraint;
use JsonSchema\Constraints\Factory;
use JsonSchema\Constraints\Drafts\Draft2019\Factory;
use PHPUnit\Framework\TestCase;

class Draft2019ConstraintTest extends TestCase
Expand Down Expand Up @@ -87,17 +87,26 @@ public static function contentPropertyProvider(): \Generator
yield 'contentEncoding' => ['contentEncoding'];
}

public function testDependentSchemasAndDependentRequiredAreInstantiatedIndependently(): void
/**
* @dataProvider dependentKeywordProvider
*/
public function testDependentSchemasAndDependentRequiredAreInstantiatedIndependently(string $schemaJson, string $expectedKeyword): void
{
$constraint = new Draft2019Constraint();
$calledKeywords = [];
$this->injectFactory($constraint, $this->createSpyFactory($calledKeywords));

$schema = json_decode('{"dependentRequired": {"a": ["b"]}}');
$schema = json_decode($schemaJson);
$value = json_decode('{"a": 1, "b": 2}');
$constraint->check($value, $schema);

$this->assertSame(['dependentRequired'], $calledKeywords);
$this->assertSame([$expectedKeyword], $calledKeywords);
}

public function dependentKeywordProvider(): \Generator
{
yield 'dependentSchemas' => ['{"dependentSchemas": {"a": {"required": ["b"]}}}', 'dependentSchemas'];
yield 'dependentRequired' => ['{"dependentRequired": {"a": ["b"]}}', 'dependentRequired'];
}

private function createSpyFactory(array &$calledKeywords): Factory
Expand Down
100 changes: 100 additions & 0 deletions tests/Constraints/Drafts/KeywordSchemaPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace JsonSchema\Tests\Constraints\Drafts;

use JsonSchema\Constraints\Drafts\Draft06;
use JsonSchema\Constraints\Drafts\Draft07;
use JsonSchema\Constraints\Drafts\Draft2019;
use PHPUnit\Framework\TestCase;

/**
* The draft constraints only dispatch a keyword when one of the schema properties it maps to is
* present, so a keyword guarding on a property missing from that map would never be checked.
*/
class KeywordSchemaPropertiesTest extends TestCase
{
/**
* @dataProvider keywordProvider
*
* @param class-string $draftConstraintClass
* @param class-string $keywordConstraintClass
*/
public function testDispatcherChecksThePropertiesTheConstraintGuardsOn(
string $draftConstraintClass,
string $keyword,
string $keywordConstraintClass
): void {
$guardedProperties = $this->getGuardedSchemaProperties($keywordConstraintClass);
if ($guardedProperties === []) {
$this->markTestSkipped(sprintf('%s does not guard on any schema property', $keywordConstraintClass));
}

$keywordSchemaProperties = (new \ReflectionClassConstant($draftConstraintClass, 'KEYWORD_SCHEMA_PROPERTIES'))->getValue();
$dispatchedProperties = $keywordSchemaProperties[$keyword] ?? [$keyword];

sort($guardedProperties);
sort($dispatchedProperties);
self::assertSame(
$guardedProperties,
$dispatchedProperties,
sprintf('Add the "%s" keyword to %s::KEYWORD_SCHEMA_PROPERTIES', $keyword, $draftConstraintClass)
);
}

public function keywordProvider(): \Generator
{
$drafts = [
Draft06\Draft06Constraint::class => Draft06\Factory::class,
Draft07\Draft07Constraint::class => Draft07\Factory::class,
Draft2019\Draft2019Constraint::class => Draft2019\Factory::class,
];

foreach ($drafts as $draftConstraintClass => $factoryClass) {
$constraintMap = new \ReflectionProperty($factoryClass, 'constraintMap');
if (PHP_VERSION_ID < 80100) {
$constraintMap->setAccessible(true);
}

foreach ($constraintMap->getValue(new $factoryClass()) as $keyword => $keywordConstraintClass) {
if ($keyword === 'schema') {
continue;
}

yield sprintf('%s: %s', (new \ReflectionClass($draftConstraintClass))->getShortName(), $keyword) => [
$draftConstraintClass,
$keyword,
$keywordConstraintClass,
];
}
}
}

/**
* The schema properties named in the first condition of the constraint's check(), which is
* where every constraint returns early when its keyword is absent.
*
* @param class-string $constraintClass
*
* @return list<string>
*/
private function getGuardedSchemaProperties(string $constraintClass): array
{
$check = new \ReflectionMethod($constraintClass, 'check');
$lines = file((string) $check->getFileName());
if ($lines === false) {
self::fail(sprintf('Unable to read the source of %s', $constraintClass));
}
$startLine = (int) $check->getStartLine();
$body = implode('', array_slice($lines, $startLine, (int) $check->getEndLine() - $startLine));

if (!preg_match('/^\s*if \((.*)\) \{$/m', $body, $condition)) {
return [];
}

preg_match_all('/property_exists\(\$schema, \'([^\']+)\'\)/', $condition[1], $properties);

return $properties[1];
}
}
Loading