Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

Commit

Permalink
Fix ability to test json payload against json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume MOREL committed Feb 2, 2015
1 parent fc1ffd6 commit 3938b76
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
37 changes: 37 additions & 0 deletions features/json.feature
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,43 @@ Feature: Testing JSONContext
}
"""

Scenario: Json validation deep
Given I am on "/json/booking.json"
Then the JSON should be invalid according to this schema:
"""
{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"required":false,
"properties":{
"Booking": {
"type":"object",
"required":false
},
"Metadata": {
"type":"object",
"required":false,
"properties":{
"First": {
"type":"object",
"required":false,
"properties":{
"default_value": {
"type":"boolean",
"required":false
},
"enabled": {
"type":"boolean",
"required":true
}
}
}
}
}
}
}
"""

Scenario: Json contents validation
Given I am on "/json/imajson.json"
Then the JSON should be equal to:
Expand Down
12 changes: 12 additions & 0 deletions fixtures/www/json/booking.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Booking": {
"id": "1",
"price": "77.21"
}, "Metadata":
{
"First": {
"bad_property_name": true,
"default_value": true
}
}
}
21 changes: 21 additions & 0 deletions src/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Behat\Gherkin\Node\PyStringNode;

use Behat\Mink\Exception\ExpectationException;
use Sanpi\Behatch\Json\Json;
use Sanpi\Behatch\Json\JsonSchema;
use Sanpi\Behatch\Json\JsonInspector;
Expand Down Expand Up @@ -158,6 +159,26 @@ public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema)
);
}

/**
* @Then the JSON should be invalid according to this schema:
*/
public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema)
{
try {
$isValid = $this->inspector->validate(
$this->getJson(),
new JsonSchema($schema)
);

} catch (\Exception $e) {
$isValid = false;
}

if (true === $isValid) {
throw new ExpectationException('Expected to receive invalid json, got valid one', $this->getSession());
}
}

/**
* @Then the JSON should be valid according to the schema :filename
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Json/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Json
{
private $content;
protected $content;

public function __construct($content)
{
Expand Down

0 comments on commit 3938b76

Please sign in to comment.