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

Add test on ability to check json payload against json schema #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This modification has no relation with the rest of this PR.


public function __construct($content)
{
Expand Down