Skip to content

Commit e99799e

Browse files
committed
Hunt JSON fragments a bit more effectively
1 parent 51a57b6 commit e99799e

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/Extensions/Traits/ApiRequests.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ protected function seeJsonContains($expected)
163163

164164
// If we have a collection of results, we'll sift through each array
165165
// in the collection, and check to see if there's a match.
166+
166167
if ( ! isset($json[0])) $json = [$json];
167168

168169
$containsFragment = array_reduce($json, function($carry, $array) use ($expected) {
@@ -188,6 +189,44 @@ protected function seeJsonContains($expected)
188189
*/
189190
protected function jsonHasFragment(array $fragment, $json)
190191
{
191-
return @array_intersect($json, $fragment) == $fragment;
192+
$hasMatch = @array_intersect($json, $fragment) == $fragment;
193+
194+
if ( ! $hasMatch) {
195+
$hasMatch = $this->searchJsonFor($fragment, $json);
196+
}
197+
198+
return $hasMatch;
199+
}
200+
201+
/**
202+
* Search through an associative array for a given fragment.
203+
*
204+
* @param array $fragment
205+
* @param array $json
206+
* @return boolean
207+
*/
208+
protected function searchJsonFor($fragment, $json)
209+
{
210+
foreach ($json as $key => $value) {
211+
212+
// We'll do a handful of checks to see if the user's
213+
// given array matches the JSON from the response.
214+
215+
if (is_array($value)) {
216+
if ($this->searchJsonFor($fragment, $value)) {
217+
return true;
218+
}
219+
220+
if (@array_intersect($value, $fragment) == $fragment) {
221+
return true;
222+
}
223+
}
224+
225+
if ($fragment == [$key => $value]) {
226+
return true;
227+
}
228+
}
229+
230+
return false;
192231
}
193232
}

0 commit comments

Comments
 (0)