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

Added @Given the JSON node :node should be of type :type #135

Open
wants to merge 3 commits 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
18 changes: 18 additions & 0 deletions src/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions tests/features/json.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions tests/fixtures/www/json/objectwithtypes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"string": "abc",
"integer": 123,
"double": 2.0,
"float": 2.123,
"null": null,
"array": [],
"object": {},
"boolean": false
}