From fbb49b9b2797fcdbc96612fa01efe7839a5c602a Mon Sep 17 00:00:00 2001 From: Andreas Heiberg Date: Thu, 20 Aug 2015 00:33:06 -0500 Subject: [PATCH 1/2] Added @Given the JSON node :node should be of type :type Basically I want to check if a node is an array. But by writing it this way you can also check for string, integer and float. --- src/Context/JsonContext.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index 5b1e1fe0..c56d8b3d 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -133,6 +133,20 @@ public function theJsonNodeShouldNotExist($name) return $this->theJsonNodeShouldExist($name); }, "The node '$name' exists."); } + + /** + * Checks, that given JSON node is of the correct type + * + * @Given the JSON node :node should be of type :type + */ + public function theJsonNodeShouldBeOfType($node, $type) + { + $json = $this->getJson(); + + $actual = gettype($this->inspector->evaluate($json, $node)); + + $this->assertSame($type, $actual); + } /** * @Then the JSON should be valid according to this schema: From c2654947a999c4ba14f55d7f735721e3b62f4eba Mon Sep 17 00:00:00 2001 From: AndreasHeiberg Date: Thu, 21 Apr 2016 19:58:15 +0100 Subject: [PATCH 2/2] add tests for theJsonNodeShouldBeOfType --- src/Context/JsonContext.php | 4 ++++ tests/features/json.feature | 11 +++++++++++ tests/fixtures/www/json/objectwithtypes.json | 10 ++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/fixtures/www/json/objectwithtypes.json diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index 407e1c33..608e7315 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -272,6 +272,10 @@ public function theJsonNodeShouldBeOfType($node, $type) $actual = gettype($this->inspector->evaluate($json, $node)); + if ($actual == 'double') { + $actual = 'float'; + } + $this->assertSame($type, $actual); } diff --git a/tests/features/json.feature b/tests/features/json.feature index 7c4c5f4b..545e4999 100644 --- a/tests/features/json.feature +++ b/tests/features/json.feature @@ -131,3 +131,14 @@ Feature: Testing JSONContext And the JSON node "root[4]" should be equal to the number 1312 And the JSON node "root[4]" should be equal to the number 1312.0 And the JSON node "root[5]" should be equal to the number 1936.2 + + Scenario: Check type + Given I am on "/json/objectwithtypes.json" + Then the response should be in JSON + And the JSON node "string" should be of type string + And the JSON node "integer" should be of type integer + And the JSON node "float" should be of type float + And the JSON node "null" should be of type NULL + And the JSON node "array" should be of type array + And the JSON node "object" should be of type object + And the JSON node "boolean" should be of type boolean diff --git a/tests/fixtures/www/json/objectwithtypes.json b/tests/fixtures/www/json/objectwithtypes.json new file mode 100644 index 00000000..3abb97fd --- /dev/null +++ b/tests/fixtures/www/json/objectwithtypes.json @@ -0,0 +1,10 @@ +{ + "string": "abc", + "integer": 123, + "double": 2.0, + "float": 2.123, + "null": null, + "array": [], + "object": {}, + "boolean": false +} \ No newline at end of file