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

Commit 3938b76

Browse files
author
Guillaume MOREL
committed
Fix ability to test json payload against json schema
1 parent fc1ffd6 commit 3938b76

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

features/json.feature

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,43 @@ Feature: Testing JSONContext
7272
}
7373
"""
7474

75+
Scenario: Json validation deep
76+
Given I am on "/json/booking.json"
77+
Then the JSON should be invalid according to this schema:
78+
"""
79+
{
80+
"type":"object",
81+
"$schema": "http://json-schema.org/draft-03/schema",
82+
"required":false,
83+
"properties":{
84+
"Booking": {
85+
"type":"object",
86+
"required":false
87+
},
88+
"Metadata": {
89+
"type":"object",
90+
"required":false,
91+
"properties":{
92+
"First": {
93+
"type":"object",
94+
"required":false,
95+
"properties":{
96+
"default_value": {
97+
"type":"boolean",
98+
"required":false
99+
},
100+
"enabled": {
101+
"type":"boolean",
102+
"required":true
103+
}
104+
}
105+
}
106+
}
107+
}
108+
}
109+
}
110+
"""
111+
75112
Scenario: Json contents validation
76113
Given I am on "/json/imajson.json"
77114
Then the JSON should be equal to:

fixtures/www/json/booking.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Booking": {
3+
"id": "1",
4+
"price": "77.21"
5+
}, "Metadata":
6+
{
7+
"First": {
8+
"bad_property_name": true,
9+
"default_value": true
10+
}
11+
}
12+
}

src/Context/JsonContext.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Behat\Gherkin\Node\PyStringNode;
66

7+
use Behat\Mink\Exception\ExpectationException;
78
use Sanpi\Behatch\Json\Json;
89
use Sanpi\Behatch\Json\JsonSchema;
910
use Sanpi\Behatch\Json\JsonInspector;
@@ -158,6 +159,26 @@ public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema)
158159
);
159160
}
160161

162+
/**
163+
* @Then the JSON should be invalid according to this schema:
164+
*/
165+
public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema)
166+
{
167+
try {
168+
$isValid = $this->inspector->validate(
169+
$this->getJson(),
170+
new JsonSchema($schema)
171+
);
172+
173+
} catch (\Exception $e) {
174+
$isValid = false;
175+
}
176+
177+
if (true === $isValid) {
178+
throw new ExpectationException('Expected to receive invalid json, got valid one', $this->getSession());
179+
}
180+
}
181+
161182
/**
162183
* @Then the JSON should be valid according to the schema :filename
163184
*/

src/Json/Json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Json
88
{
9-
private $content;
9+
protected $content;
1010

1111
public function __construct($content)
1212
{

0 commit comments

Comments
 (0)