Skip to content

Commit

Permalink
Hunt JSON fragments a bit more effectively
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Apr 26, 2015
1 parent 51a57b6 commit e99799e
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/Extensions/Traits/ApiRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ protected function seeJsonContains($expected)

// If we have a collection of results, we'll sift through each array
// in the collection, and check to see if there's a match.

if ( ! isset($json[0])) $json = [$json];

$containsFragment = array_reduce($json, function($carry, $array) use ($expected) {
Expand All @@ -188,6 +189,44 @@ protected function seeJsonContains($expected)
*/
protected function jsonHasFragment(array $fragment, $json)
{
return @array_intersect($json, $fragment) == $fragment;
$hasMatch = @array_intersect($json, $fragment) == $fragment;

if ( ! $hasMatch) {
$hasMatch = $this->searchJsonFor($fragment, $json);
}

return $hasMatch;
}

/**
* Search through an associative array for a given fragment.
*
* @param array $fragment
* @param array $json
* @return boolean
*/
protected function searchJsonFor($fragment, $json)
{
foreach ($json as $key => $value) {

// We'll do a handful of checks to see if the user's
// given array matches the JSON from the response.

if (is_array($value)) {
if ($this->searchJsonFor($fragment, $value)) {
return true;
}

if (@array_intersect($value, $fragment) == $fragment) {
return true;
}
}

if ($fragment == [$key => $value]) {
return true;
}
}

return false;
}
}

0 comments on commit e99799e

Please sign in to comment.