Skip to content

Commit

Permalink
Better JSON match searching
Browse files Browse the repository at this point in the history
Needs a touch of refactoring.
  • Loading branch information
JeffreyWay committed Apr 18, 2015
1 parent 81b3975 commit 0bad283
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/Extensions/ApiRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,33 @@ protected function seeJsonContains($expected)
$response = $this->response();
$json = json_decode($response, true);

$this->assertEquals(
@array_intersect($json, $expected),
$expected,
sprintf("Dang! Expected %s to exist in %s, but no dice. Any ideas?", json_encode($expected), $response)
);
// 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) {
if ($carry) return $carry;

return $this->jsonHasFragment($expected, $array);
});

$this->assertTrue($containsFragment, sprintf(
"Dang! Expected %s to exist in %s, but nope. Ideas?",
json_encode($expected), $response
));

return $this;
}

/**
* Determine if the given fragment is contained with a decoded set of JSON.
*
* @param array $fragment
* @param array $json
* @return boolean
*/
protected function jsonHasFragment(array $fragment, $json)
{
return @array_intersect($json, $fragment) == $fragment;
}
}

0 comments on commit 0bad283

Please sign in to comment.