diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index 1ea79d42..608e7315 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -260,6 +260,24 @@ 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)); + + if ($actual == 'double') { + $actual = 'float'; + } + + $this->assertSame($type, $actual); + } /** * @Then the JSON should be valid according to this schema: 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