diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 40d77463d39e..0528f7cec9a2 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -58,6 +58,45 @@ public static function fromBaseResponse($response) return new static($response); } + /** + * JSON decode assoc and keep the empty object. + * @param string $json + * @link https://github.com/laravel/framework/issues/25769 + * @return mixed + */ + public static function jsonDecodeKeepEmptyObject(string $json) + { + $patchEmptyObject = function ($array) use (&$patchEmptyObject) { + + // If it is an empty class,it is reserved. + // Otherwise converted into an associative array. + if (is_object($array)) { + return empty((array) $array) + ? $array + : $patchEmptyObject((array) $array); + } + + foreach ($array as $key => $item) { + // We recursively deal with each of these terms. + if (is_array($item) + || is_object($item) + ) { + $array[$key] = $patchEmptyObject($item); + } + } + + return $array; + }; + + $stdClass = json_decode($json); + + if ($stdClass === false) { + return $stdClass; + } + + return $patchEmptyObject($stdClass); + } + /** * Assert that the response has a successful status code. * @@ -686,7 +725,7 @@ public function assertJsonMissingValidationErrors($keys) */ public function decodeResponseJson($key = null) { - $decodedResponse = json_decode($this->getContent(), true); + $decodedResponse = static::jsonDecodeKeepEmptyObject($this->getContent()); if (is_null($decodedResponse) || $decodedResponse === false) { if ($this->exception) { diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index a1ca2d748a24..7e2be68e43ef 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -6,6 +6,7 @@ use JsonSerializable; use Illuminate\Http\Response; use PHPUnit\Framework\TestCase; +use Illuminate\Http\JsonResponse; use Illuminate\Contracts\View\View; use Illuminate\Filesystem\Filesystem; use Illuminate\Database\Eloquent\Model; @@ -258,6 +259,45 @@ public function testAssertJsonMissing() $response->assertJsonMissing(['id' => 20]); } + public function testAssertExactJson() + { + $response = new TestResponse((new JsonResponse([ + 'payload' => (object) [], + 'a' => [], + 'b' => (object) [], + 'status' => 'success', + 'data' => [ + 'name' => 'West Fannieland', + 'updated_at' => '2018-10-05 20:48:11', + 'created_at' => '2018-10-05 20:48:11', + 'id' => 1, + 'b' => (object) [], + 'c' => [ + 'b' => (object) [], + 'name' => 'albert', + ], + ], + ]))); + + $response->assertExactJson([ + 'payload' => (object) [], + 'b' => (object) [], + 'a' => [], + 'status' => 'success', + 'data' => [ + 'name' => 'West Fannieland', + 'created_at' => '2018-10-05 20:48:11', + 'id' => 1, + 'b' => (object) [], + 'c' => [ + 'b' => (object) [], + 'name' => 'albert', + ], + 'updated_at' => '2018-10-05 20:48:11', + ], + ]); + } + public function testAssertJsonMissingExact() { $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));