1 | <?php |
||
10 | class JsonContext implements Context, SnippetAcceptingContext |
||
11 | { |
||
12 | private $jsonInspector; |
||
13 | |||
14 | private $asserter; |
||
15 | |||
16 | private $jsonSchemaBaseUrl; |
||
17 | |||
18 | public function __construct(JsonInspector $jsonInspector, $jsonSchemaBaseUrl = null) |
||
19 | { |
||
20 | $this->jsonInspector = $jsonInspector; |
||
21 | $this->asserter = new asserter; |
||
22 | $this->jsonSchemaBaseUrl = rtrim($jsonSchemaBaseUrl, '/'); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * @When /^I load JSON:$/ |
||
27 | */ |
||
28 | public function iLoadJson(PyStringNode $jsonContent) |
||
29 | { |
||
30 | $this->jsonInspector->writeJson((string) $jsonContent); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @Then /^the response should be in JSON$/ |
||
35 | */ |
||
36 | public function responseShouldBeInJson() |
||
37 | { |
||
38 | $this->jsonInspector->readJson(); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @Then /^the JSON node "(?P<jsonNode>[^"]*)" should be equal to "(?P<expectedValue>.*)"$/ |
||
43 | */ |
||
44 | public function theJsonNodeShouldBeEqualTo($jsonNode, $expectedValue) |
||
45 | { |
||
46 | $realValue = $this->evaluateJsonNodeValue($jsonNode); |
||
47 | |||
48 | $this->asserter->variable($realValue)->isEqualTo($expectedValue); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @Then /^the JSON node "(?P<jsonNode>[^"]*)" should have (?P<expectedNth>\d+) elements?$/ |
||
53 | * @Then /^the JSON array node "(?P<jsonNode>[^"]*)" should have (?P<expectedNth>\d+) elements?$/ |
||
54 | */ |
||
55 | public function theJsonNodeShouldHaveElements($jsonNode, $expectedNth) |
||
56 | { |
||
57 | $realValue = $this->evaluateJsonNodeValue($jsonNode); |
||
58 | |||
59 | $this->asserter->phpArray($realValue)->hasSize($expectedNth); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @Then /^the JSON array node "(?P<jsonNode>[^"]*)" should contain "(?P<expectedValue>.*)" element$/ |
||
64 | */ |
||
65 | public function theJsonArrayNodeShouldContainElements($jsonNode, $expectedValue) |
||
71 | |||
72 | /** |
||
73 | * @Then /^the JSON array node "(?P<jsonNode>[^"]*)" should not contain "(?P<expectedValue>.*)" element$/ |
||
74 | */ |
||
75 | public function theJsonArrayNodeShouldNotContainElements($jsonNode, $expectedValue) |
||
76 | { |
||
81 | |||
82 | /** |
||
83 | * @Then /^the JSON node "(?P<jsonNode>[^"]*)" should contain "(?P<expectedValue>.*)"$/ |
||
84 | */ |
||
85 | public function theJsonNodeShouldContain($jsonNode, $expectedValue) |
||
91 | |||
92 | /** |
||
93 | * Checks, that given JSON node does not contain given value |
||
94 | * |
||
95 | * @Then /^the JSON node "(?P<jsonNode>[^"]*)" should not contain "(?P<unexpectedValue>.*)"$/ |
||
96 | */ |
||
97 | public function theJsonNodeShouldNotContain($jsonNode, $unexpectedValue) |
||
103 | |||
104 | /** |
||
105 | * Checks, that given JSON node exist |
||
106 | * |
||
107 | * @Given /^the JSON node "(?P<jsonNode>[^"]*)" should exist$/ |
||
108 | */ |
||
109 | public function theJsonNodeShouldExist($jsonNode) |
||
117 | |||
118 | /** |
||
119 | * Checks, that given JSON node does not exist |
||
120 | * |
||
121 | * @Given /^the JSON node "(?P<jsonNode>[^"]*)" should not exist$/ |
||
122 | */ |
||
123 | public function theJsonNodeShouldNotExist($jsonNode) |
||
137 | |||
138 | /** |
||
139 | * @Then /^the JSON should be valid according to this schema:$/ |
||
140 | */ |
||
141 | public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $jsonSchemaContent) |
||
147 | |||
148 | /** |
||
149 | * @Then /^the JSON should be valid according to the schema "(?P<filename>[^"]*)"$/ |
||
150 | */ |
||
151 | public function theJsonShouldBeValidAccordingToTheSchema($filename) |
||
162 | |||
163 | /** |
||
164 | * @Then /^the JSON should be equal to:$/ |
||
165 | */ |
||
166 | public function theJsonShouldBeEqualTo(PyStringNode $jsonContent) |
||
178 | |||
179 | private function evaluateJsonNodeValue($jsonNode) |
||
183 | |||
184 | private function readJson() |
||
188 | |||
189 | private function resolveFilename($filename) |
||
213 | } |
||
214 |