From ec359925ac62d5599c1c3d3efd5bb2513fdf8045 Mon Sep 17 00:00:00 2001 From: Guillaume MOREL Date: Tue, 7 Oct 2014 13:43:20 +0200 Subject: [PATCH] Fix ability to test json payload against json schema --- features/json.feature | 45 ++++++++++++++++++++++++++++++++++ fixtures/www/json/booking.json | 12 +++++++++ src/Context/JsonContext.php | 21 ++++++++++++++++ src/Json/Json.php | 2 +- src/Json/JsonSchema.php | 2 +- 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 fixtures/www/json/booking.json diff --git a/features/json.feature b/features/json.feature index 2742fe57..9aacf3ca 100644 --- a/features/json.feature +++ b/features/json.feature @@ -72,6 +72,51 @@ 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", + "id": "http://jsonschema.net", + "required":false, + "properties":{ + "Booking": { + "type":"object", + "id": "http://jsonschema.net/Booking", + "required":false + }, + "Metadata": { + "type":"object", + "id": "http://jsonschema.net/Metadata", + "required":false, + "properties":{ + "First": { + "type":"object", + "id": "http://jsonschema.net/Metadata/First", + "required":false, + "properties":{ + "default_value": { + "type":"boolean", + "id": "http://jsonschema.net/Metadata/First/default_value", + "required":false + }, + "enabled": { + "type":"boolean", + "id": "http://jsonschema.net/Metadata/First/enabled", + "required":true + } + } + } + } + } + } + } + """ + + Scenario: Json contents validation Given I am on "/json/imajson.json" Then the JSON should be equal to: diff --git a/fixtures/www/json/booking.json b/fixtures/www/json/booking.json new file mode 100644 index 00000000..eac43e70 --- /dev/null +++ b/fixtures/www/json/booking.json @@ -0,0 +1,12 @@ +{ + "Booking": { + "id": "1", + "price": "77.21" + }, "Metadata": + { + "First": { + "bad_property_name": true, + "default_value": true + } + } +} diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index f7a616bb..83f23e01 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -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; @@ -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 */ diff --git a/src/Json/Json.php b/src/Json/Json.php index a95dacb6..78e84dad 100644 --- a/src/Json/Json.php +++ b/src/Json/Json.php @@ -6,7 +6,7 @@ class Json { - private $content; + protected $content; public function __construct($content) { diff --git a/src/Json/JsonSchema.php b/src/Json/JsonSchema.php index 32b9835f..d47254a9 100644 --- a/src/Json/JsonSchema.php +++ b/src/Json/JsonSchema.php @@ -28,7 +28,7 @@ public function resolve(RefResolver $resolver) public function validate(Json $json, Validator $validator) { - $validator->check($json, $this); + $validator->check($json->content, $this->content); if (!$validator->isValid()) { $msg = "JSON does not validate. Violations:".PHP_EOL;