Skip to content

Commit 4f4eed7

Browse files
committed
Simpler seeJsonContains logic.
Hat tip to Taylor Otwell.
1 parent 27037a3 commit 4f4eed7

File tree

1 file changed

+19
-61
lines changed

1 file changed

+19
-61
lines changed

src/Extensions/Traits/ApiRequests.php

Lines changed: 19 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -167,80 +167,38 @@ protected function seeJsonEquals($expected)
167167
*/
168168
protected function seeJsonContains($expected)
169169
{
170-
$response = $this->response();
171-
$json = json_decode($response, true);
172-
173-
// If we have a collection of results, we'll sift through each array
174-
// in the collection, and check to see if there's a match.
170+
$response = json_decode($this->response(), true);
175171

176-
if (! isset($json[0])) {
177-
$json = [$json];
178-
}
172+
$this->sortJson($expected);
173+
$this->sortJson($response);
179174

180-
$containsFragment = array_reduce($json, function ($carry, $array) use ($expected) {
181-
if ($carry) {
182-
return $carry;
175+
foreach ($expected as $key => $value) {
176+
if ( ! str_contains(json_encode($response), trim(json_encode([$key => $value]), '{}'))) {
177+
$this->fail(sprintf(
178+
"Dang! Expected %s to exist in %s, but nope. Ideas?",
179+
json_encode($expected), json_encode($response)
180+
));
183181
}
184-
185-
return $this->jsonHasFragment($expected, $array);
186-
});
187-
188-
$this->assertTrue($containsFragment, sprintf(
189-
"Dang! Expected %s to exist in %s, but nope. Ideas?",
190-
json_encode($expected), $response
191-
));
192-
193-
return $this;
194-
}
195-
196-
/**
197-
* Determine if the given fragment is contained with a decoded set of JSON.
198-
*
199-
* @param array $fragment
200-
* @param array $json
201-
* @return boolean
202-
*/
203-
protected function jsonHasFragment(array $fragment, $json)
204-
{
205-
$hasMatch = @array_intersect($json, $fragment) == $fragment;
206-
207-
if (! $hasMatch) {
208-
$hasMatch = $this->searchJsonFor($fragment, $json);
209182
}
210-
211-
return $hasMatch;
212183
}
213184

214185
/**
215-
* Search through an associative array for a given fragment.
186+
* Sort a JSON response, for easy assertions and comparisons.
216187
*
217-
* @param array $fragment
218-
* @param array $json
219-
* @return boolean
188+
* @param array &$array
189+
* @return array
220190
*/
221-
protected function searchJsonFor($fragment, $json)
191+
protected function sortJson(&$array)
222192
{
223-
foreach ($json as $key => $value) {
224-
225-
// We'll do a handful of checks to see if the user's
226-
// given array matches the JSON from the response.
227-
228-
if (is_array($value)) {
229-
if ($this->searchJsonFor($fragment, $value)) {
230-
return true;
231-
}
232-
233-
if (@array_intersect($value, $fragment) == $fragment) {
234-
return true;
235-
}
236-
}
237-
238-
if ($fragment == [$key => $value]) {
239-
return true;
193+
foreach ($array as &$value) {
194+
if (is_array($value) && isset($value[0])) {
195+
sort($value);
196+
} elseif (is_array($value)) {
197+
$this->sortJson($value);
240198
}
241199
}
242200

243-
return false;
201+
return ksort($array);
244202
}
245203

246204
/**

0 commit comments

Comments
 (0)