diff --git a/src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php b/src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php index 79e0ee23f..fc962749f 100644 --- a/src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php +++ b/src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php @@ -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( @@ -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 diff --git a/src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php b/src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php index d1cac103f..dcc9e1187 100644 --- a/src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php +++ b/src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php @@ -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( @@ -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 diff --git a/src/JsonSchema/Constraints/Drafts/Draft2019/Draft2019Constraint.php b/src/JsonSchema/Constraints/Drafts/Draft2019/Draft2019Constraint.php index 2167387e3..8ebcd725a 100644 --- a/src/JsonSchema/Constraints/Drafts/Draft2019/Draft2019Constraint.php +++ b/src/JsonSchema/Constraints/Drafts/Draft2019/Draft2019Constraint.php @@ -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( @@ -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 diff --git a/tests/Constraints/Drafts/Draft06/Draft06ConstraintTest.php b/tests/Constraints/Drafts/Draft06/Draft06ConstraintTest.php index ed6d98427..8996db15f 100644 --- a/tests/Constraints/Drafts/Draft06/Draft06ConstraintTest.php +++ b/tests/Constraints/Drafts/Draft06/Draft06ConstraintTest.php @@ -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 diff --git a/tests/Constraints/Drafts/Draft07/Draft07ConstraintTest.php b/tests/Constraints/Drafts/Draft07/Draft07ConstraintTest.php index a2ccb048c..d68f909b1 100644 --- a/tests/Constraints/Drafts/Draft07/Draft07ConstraintTest.php +++ b/tests/Constraints/Drafts/Draft07/Draft07ConstraintTest.php @@ -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 diff --git a/tests/Constraints/Drafts/Draft2019/Draft2019ConstraintTest.php b/tests/Constraints/Drafts/Draft2019/Draft2019ConstraintTest.php index 4d11573ef..d8f699701 100644 --- a/tests/Constraints/Drafts/Draft2019/Draft2019ConstraintTest.php +++ b/tests/Constraints/Drafts/Draft2019/Draft2019ConstraintTest.php @@ -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 @@ -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 diff --git a/tests/Constraints/Drafts/KeywordSchemaPropertiesTest.php b/tests/Constraints/Drafts/KeywordSchemaPropertiesTest.php new file mode 100644 index 000000000..598225c73 --- /dev/null +++ b/tests/Constraints/Drafts/KeywordSchemaPropertiesTest.php @@ -0,0 +1,100 @@ +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 + */ + 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]; + } +}