Skip to content
This repository was archived by the owner on Apr 20, 2021. It is now read-only.
Closed
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
27 changes: 22 additions & 5 deletions src/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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:
*/
Expand Down Expand Up @@ -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'
);
}
}
}
2 changes: 1 addition & 1 deletion src/Json/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/features/json.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/www/json/withref-invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"foo": "not a uri",
"bar": "not a uri"
}