From 0bad283d6ada6f52322caf9b2ff3021c1e216fbc Mon Sep 17 00:00:00 2001 From: JeffreyWay Date: Sat, 18 Apr 2015 18:52:50 -0400 Subject: [PATCH] Better JSON match searching Needs a touch of refactoring. --- src/Extensions/ApiRequests.php | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Extensions/ApiRequests.php b/src/Extensions/ApiRequests.php index 2289034..a1c34d1 100644 --- a/src/Extensions/ApiRequests.php +++ b/src/Extensions/ApiRequests.php @@ -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; + } }