diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 20fed91bd8f4..15520b878a52 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -199,6 +199,30 @@ public function assertJson(array $data) return $this; } + /** + * Assert that the response contains the given JSON fragment. + * + * @param array $data + * @return $this + */ + public function assertJsonFragment(array $data) + { + $actual = json_encode(Arr::sortRecursive( + (array) $this->decodeResponseJson() + )); + + foreach (Arr::sortRecursive($data) as $key => $value) { + $expected = substr(json_encode([$key => $value]), 1, -1); + + PHPUnit::assertTrue( + Str::contains($actual, $expected), + 'Unable to find JSON fragment'.PHP_EOL."[{$expected}]".PHP_EOL.'within'.PHP_EOL."[{$actual}]." + ); + } + + return $this; + } + /** * Assert that the response has the exact given JSON. *