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

Commit d1faf68

Browse files
committed
increase JSON context DX
1 parent 8f28369 commit d1faf68

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/Context/JsonContext.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
namespace Behatch\Context;
44

55
use Behat\Gherkin\Node\PyStringNode;
6-
76
use Behat\Gherkin\Node\TableNode;
8-
use Behat\Mink\Exception\ExpectationException;
7+
use Behatch\HttpCall\HttpCallResultPool;
98
use Behatch\Json\Json;
10-
use Behatch\Json\JsonSchema;
119
use Behatch\Json\JsonInspector;
12-
use Behatch\HttpCall\HttpCallResultPool;
10+
use Behatch\Json\JsonSchema;
1311

1412
class JsonContext extends BaseContext
1513
{
@@ -49,17 +47,17 @@ public function theResponseShouldNotBeInJson()
4947
/**
5048
* Checks, that given JSON node is equal to given value
5149
*
52-
* @Then the JSON node :node should be equal to :text
50+
* @Then the JSON node :node should be equal to :expected
5351
*/
54-
public function theJsonNodeShouldBeEqualTo($node, $text)
52+
public function theJsonNodeShouldBeEqualTo($node, $expected)
5553
{
5654
$json = $this->getJson();
5755

5856
$actual = $this->inspector->evaluate($json, $node);
5957

60-
if ($actual != $text) {
58+
if ($actual != $expected) {
6159
throw new \Exception(
62-
sprintf("The node value is '%s'", json_encode($actual))
60+
sprintf("The node '%s' value is '%s', '%s' expected", $node, json_encode($actual), json_encode($expected))
6361
);
6462
}
6563
}
@@ -89,7 +87,7 @@ public function theJsonNodeShouldMatch($node, $pattern)
8987

9088
if (preg_match($pattern, $actual) === 0) {
9189
throw new \Exception(
92-
sprintf("The node value is '%s'", json_encode($actual))
90+
sprintf("The node '%s' value is '%s', '%s' pattern expected", $node, json_encode($actual), $pattern)
9391
);
9492
}
9593
}
@@ -107,7 +105,7 @@ public function theJsonNodeShouldBeNull($node)
107105

108106
if (null !== $actual) {
109107
throw new \Exception(
110-
sprintf('The node value is `%s`', json_encode($actual))
108+
sprintf("The node '%s' value is '%s', null expected", $node, json_encode($actual))
111109
);
112110
}
113111
}
@@ -119,9 +117,15 @@ public function theJsonNodeShouldBeNull($node)
119117
*/
120118
public function theJsonNodeShouldNotBeNull($node)
121119
{
122-
$this->not(function () use ($node) {
123-
return $this->theJsonNodeShouldBeNull($node);
124-
}, sprintf('The node %s should not be null', $node));
120+
$json = $this->getJson();
121+
122+
$actual = $this->inspector->evaluate($json, $node);
123+
124+
if (null === $actual) {
125+
throw new \Exception(
126+
sprintf("The node '%s' value is null, '%s' expected", $node, json_encode($actual))
127+
);
128+
}
125129
}
126130

127131
/**
@@ -137,7 +141,7 @@ public function theJsonNodeShouldBeTrue($node)
137141

138142
if (true !== $actual) {
139143
throw new \Exception(
140-
sprintf('The node value is `%s`', json_encode($actual))
144+
sprintf("The node '%s' value is '%s', 'true' expected", $node, json_encode($actual))
141145
);
142146
}
143147
}
@@ -155,25 +159,25 @@ public function theJsonNodeShouldBeFalse($node)
155159

156160
if (false !== $actual) {
157161
throw new \Exception(
158-
sprintf('The node value is `%s`', json_encode($actual))
162+
sprintf("The node '%s' value is '%s', 'false' expected", $node, json_encode($actual))
159163
);
160164
}
161165
}
162166

163167
/**
164168
* Checks, that given JSON node is equal to the given string
165169
*
166-
* @Then the JSON node :node should be equal to the string :text
170+
* @Then the JSON node :node should be equal to the string :expected
167171
*/
168-
public function theJsonNodeShouldBeEqualToTheString($node, $text)
172+
public function theJsonNodeShouldBeEqualToTheString($node, $expected)
169173
{
170174
$json = $this->getJson();
171175

172176
$actual = $this->inspector->evaluate($json, $node);
173177

174-
if ($actual !== $text) {
178+
if ($actual !== $expected) {
175179
throw new \Exception(
176-
sprintf('The node value is `%s`', json_encode($actual))
180+
sprintf("The node '%s' value is '%s', string '%s' expected", $node, json_encode($actual), $expected)
177181
);
178182
}
179183
}
@@ -191,7 +195,7 @@ public function theJsonNodeShouldBeEqualToTheNumber($node, $number)
191195

192196
if ($actual !== (float) $number && $actual !== (int) $number) {
193197
throw new \Exception(
194-
sprintf('The node value is `%s`', json_encode($actual))
198+
sprintf("The node '%s' value is '%s', numder '%6' expected", $node, json_encode($actual), (float) $number)
195199
);
196200
}
197201
}
@@ -207,7 +211,7 @@ public function theJsonNodeShouldHaveElements($node, $count)
207211

208212
$actual = $this->inspector->evaluate($json, $node);
209213

210-
$this->assertSame($count, sizeof((array) $actual));
214+
$this->assertSame($count, count((array) $actual));
211215
}
212216

213217
/**
@@ -276,6 +280,7 @@ public function theJsonNodeShouldExist($name)
276280
} catch (\Exception $e) {
277281
throw new \Exception("The node '$name' does not exist.");
278282
}
283+
279284
return $node;
280285
}
281286

@@ -337,7 +342,7 @@ public function theJsonShouldBeInvalidAccordingToTheSchema($filename)
337342

338343
$this->not(function () use ($filename) {
339344
return $this->theJsonShouldBeValidAccordingToTheSchema($filename);
340-
}, "The schema was valid");
345+
}, 'The schema was valid');
341346
}
342347

343348
/**
@@ -356,7 +361,7 @@ public function theJsonShouldBeEqualTo(PyStringNode $content)
356361
$this->assertSame(
357362
(string) $expected,
358363
(string) $actual,
359-
"The json is equal to:\n". $actual->encode()
364+
"The json is equal to:\n" . $actual->encode()
360365
);
361366
}
362367

@@ -390,8 +395,8 @@ public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $sche
390395
)
391396
);
392397
}
398+
393399
/**
394-
*
395400
* Checks, that response JSON not matches with a swagger dump
396401
*
397402
* @Then the JSON should not be valid according to swagger :dumpPath dump schema :schemaName
@@ -403,8 +408,6 @@ public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $s
403408
}, 'JSON Schema matches but it should not');
404409
}
405410

406-
407-
408411
protected function getJson()
409412
{
410413
return new Json($this->httpCallResultPool->getResult()->getValue());

0 commit comments

Comments
 (0)