diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index 93710466..1ea79d42 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -277,11 +277,7 @@ public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema) */ public function theJsonShouldBeValidAccordingToTheSchema($filename) { - if (false === is_file($filename)) { - throw new \RuntimeException( - 'The JSON schema doesn\'t exist' - ); - } + $this->checkSchemaFile($filename); $this->inspector->validate( $this->getJson(), @@ -292,6 +288,18 @@ public function theJsonShouldBeValidAccordingToTheSchema($filename) ); } + /** + * @Then the JSON should be invalid according to the schema :filename + */ + public function theJsonShouldBeInvalidAccordingToTheSchema($filename) + { + $this->checkSchemaFile($filename); + + $this->not(function () use($filename) { + return $this->theJsonShouldBeValidAccordingToTheSchema($filename); + }, "The schema was valid"); + } + /** * @Then the JSON should be equal to: */ @@ -326,4 +334,13 @@ protected function getJson() { return new Json($this->httpCallResultPool->getResult()->getValue()); } + + private function checkSchemaFile($filename) + { + if (false === is_file($filename)) { + throw new \RuntimeException( + 'The JSON schema doesn\'t exist' + ); + } + } } diff --git a/src/Json/JsonSchema.php b/src/Json/JsonSchema.php index 659ada01..14951e4e 100644 --- a/src/Json/JsonSchema.php +++ b/src/Json/JsonSchema.php @@ -21,7 +21,7 @@ public function resolve(RefResolver $resolver) return $this; } - $resolver->resolve($this->encode(false), $this->uri); + $resolver->resolve($this->getContent(), $this->uri); return $this; } diff --git a/tests/features/json.feature b/tests/features/json.feature index 0677a844..7c4c5f4b 100644 --- a/tests/features/json.feature +++ b/tests/features/json.feature @@ -50,6 +50,10 @@ Feature: Testing JSONContext Given I am on "/json/imajson.json" Then the JSON should be valid according to the schema "tests/fixtures/www/json/schema.json" + Scenario: Json validation with schema containing ref (invalid case) + Given I am on "/json/withref-invalid.json" + Then the JSON should be invalid according to the schema "tests/fixtures/www/json/schemaref.json" + Scenario: Json validation with schema containing ref Given I am on "/json/withref.json" Then the JSON should be valid according to the schema "tests/fixtures/www/json/schemaref.json" diff --git a/tests/fixtures/www/json/withref-invalid.json b/tests/fixtures/www/json/withref-invalid.json new file mode 100644 index 00000000..5fbb3058 --- /dev/null +++ b/tests/fixtures/www/json/withref-invalid.json @@ -0,0 +1,4 @@ +{ + "foo": "not a uri", + "bar": "not a uri" +}