Skip to content

Commit 0bad283

Browse files
committed
Better JSON match searching
Needs a touch of refactoring.
1 parent 81b3975 commit 0bad283

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/Extensions/ApiRequests.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,33 @@ protected function seeJsonContains($expected)
159159
$response = $this->response();
160160
$json = json_decode($response, true);
161161

162-
$this->assertEquals(
163-
@array_intersect($json, $expected),
164-
$expected,
165-
sprintf("Dang! Expected %s to exist in %s, but no dice. Any ideas?", json_encode($expected), $response)
166-
);
162+
// If we have a collection of results, we'll sift through each array
163+
// in the collection, and check to see if there's a match.
164+
if ( ! isset($json[0])) $json = [$json];
165+
166+
$containsFragment = array_reduce($json, function($carry, $array) use ($expected) {
167+
if ($carry) return $carry;
168+
169+
return $this->jsonHasFragment($expected, $array);
170+
});
171+
172+
$this->assertTrue($containsFragment, sprintf(
173+
"Dang! Expected %s to exist in %s, but nope. Ideas?",
174+
json_encode($expected), $response
175+
));
167176

168177
return $this;
169178
}
179+
180+
/**
181+
* Determine if the given fragment is contained with a decoded set of JSON.
182+
*
183+
* @param array $fragment
184+
* @param array $json
185+
* @return boolean
186+
*/
187+
protected function jsonHasFragment(array $fragment, $json)
188+
{
189+
return @array_intersect($json, $fragment) == $fragment;
190+
}
170191
}

0 commit comments

Comments
 (0)