From 609c76860efc642be616d448f9105a3f41a7af27 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Tue, 4 Dec 2018 20:39:55 +0100 Subject: [PATCH] [5.7] Add tests for decodeResponseJson (#26741) * Add test to prevent assertJsonStructure from breaking This test will prevent assertJsonStructure from breaking when an array with numeric keys which don't start from 0 is encoded to json and passed through it. See https://github.com/laravel/framework/issues/26677 for more info. * Add test to expect exception message from decodeResponseJson This test will ensure that the exception message is thrown when invalid JSON is being returned from the route. See https://github.com/laravel/framework/issues/26684 --- .../Foundation/FoundationTestResponseTest.php | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index a1ca2d748a24..78e5c3c988b9 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -167,6 +167,18 @@ public function testAssertJsonWithArray() $response->assertJson($resource->jsonSerialize()); } + public function testAssertJsonWithNull() + { + $response = TestResponse::fromBaseResponse(new Response(null)); + + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage('Invalid JSON was returned from the route.'); + + $resource = new JsonSerializableSingleResourceStub; + + $response->assertJson($resource->jsonSerialize()); + } + public function testAssertJsonWithMixed() { $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub)); @@ -224,6 +236,9 @@ public function testAssertJsonStructure() // Wildcard (repeating structure) $response->assertJsonStructure(['bars' => ['*' => ['bar', 'foo']]]); + // Wildcard (numeric keys) + $response->assertJsonStructure(['numeric_keys' => ['*' => ['bar', 'foo']]]); + // Nested after wildcard $response->assertJsonStructure(['baz' => ['*' => ['foo', 'bar' => ['foo', 'bar']]]]); @@ -395,17 +410,17 @@ class JsonSerializableMixedResourcesStub implements JsonSerializable public function jsonSerialize() { return [ - 'foo' => 'bar', + 'foo' => 'bar', 'foobar' => [ 'foobar_foo' => 'foo', 'foobar_bar' => 'bar', ], - 'bars' => [ + 'bars' => [ ['bar' => 'foo 0', 'foo' => 'bar 0'], ['bar' => 'foo 1', 'foo' => 'bar 1'], ['bar' => 'foo 2', 'foo' => 'bar 2'], ], - 'baz' => [ + 'baz' => [ ['foo' => 'bar 0', 'bar' => ['foo' => 'bar 0', 'bar' => 'foo 0']], ['foo' => 'bar 1', 'bar' => ['foo' => 'bar 1', 'bar' => 'foo 1']], ], @@ -414,6 +429,11 @@ public function jsonSerialize() ['bar' => ['bar' => 'foo 0', 'bar' => 'foo 0']], ['bar' => ['foo' => 'bar 0', 'bar' => 'foo 0', 'rab' => 'rab 0']], ], + 'numeric_keys' => [ + 2 => ['bar' => 'foo 0', 'foo' => 'bar 0'], + 3 => ['bar' => 'foo 1', 'foo' => 'bar 1'], + 4 => ['bar' => 'foo 2', 'foo' => 'bar 2'], + ], ]; } }