From 8170e49fe784a28df66918be0a3f841033c6eb66 Mon Sep 17 00:00:00 2001 From: danilopolani Date: Wed, 11 May 2022 22:51:19 +0200 Subject: [PATCH 1/2] add TestResponse::assertJsonMissingPath --- .../Testing/AssertableJsonString.php | 13 ++++++ src/Illuminate/Testing/TestResponse.php | 13 ++++++ tests/Testing/TestResponseTest.php | 42 +++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/src/Illuminate/Testing/AssertableJsonString.php b/src/Illuminate/Testing/AssertableJsonString.php index 54d04b2d4594..0d3fb2fe9bec 100644 --- a/src/Illuminate/Testing/AssertableJsonString.php +++ b/src/Illuminate/Testing/AssertableJsonString.php @@ -211,6 +211,19 @@ public function assertMissingExact(array $data) return $this; } + /** + * Assert that the response does not contain the given path. + * + * @param string $path + * @return $this + */ + public function assertMissingPath($path) + { + PHPUnit::assertFalse(Arr::has($this->json(), $path)); + + return $this; + } + /** * Assert that the expected value and type exists at the given path in the response. * diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index c5259f832fd5..5be67081ded4 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -810,6 +810,19 @@ public function assertJsonMissingExact(array $data) return $this; } + /** + * Assert that the response does not contain the given path. + * + * @param array $data + * @return $this + */ + public function assertJsonMissingPath(string $path) + { + $this->decodeResponseJson()->assertMissingPath($path); + + return $this; + } + /** * Assert that the response has a given JSON structure. * diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index 1ebea906576f..cd0f76f28ffb 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -967,6 +967,48 @@ public function testAssertJsonMissingExactCanFail2() $response->assertJsonMissingExact(['id' => 20, 'foo' => 'bar']); } + public function testAssertJsonMissingPath() + { + $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub)); + + // With simple key + $response->assertJsonMissingPath('missing'); + + // With nested key + $response->assertJsonMissingPath('foobar.missing'); + $response->assertJsonMissingPath('numeric_keys.0'); + } + + public function testAssertJsonMissingPathCanFail() + { + + $this->expectException(AssertionFailedError::class); + + $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub)); + + $response->assertJsonMissingPath('foo'); + } + + public function testAssertJsonMissingPathCanFail2() + { + + $this->expectException(AssertionFailedError::class); + + $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub)); + + $response->assertJsonMissingPath('foobar.foobar_foo'); + } + + public function testAssertJsonMissingPathCanFail3() + { + + $this->expectException(AssertionFailedError::class); + + $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub)); + + $response->assertJsonMissingPath('numeric_keys.3'); + } + public function testAssertJsonValidationErrors() { $data = [ From 030f67a7e88adb32c19bdf7504cee0c4e72a4365 Mon Sep 17 00:00:00 2001 From: danilopolani Date: Wed, 11 May 2022 23:33:18 +0200 Subject: [PATCH 2/2] fix styleci --- tests/Testing/TestResponseTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index cd0f76f28ffb..1457c7b85dcf 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -981,7 +981,6 @@ public function testAssertJsonMissingPath() public function testAssertJsonMissingPathCanFail() { - $this->expectException(AssertionFailedError::class); $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub)); @@ -991,7 +990,6 @@ public function testAssertJsonMissingPathCanFail() public function testAssertJsonMissingPathCanFail2() { - $this->expectException(AssertionFailedError::class); $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub)); @@ -1001,7 +999,6 @@ public function testAssertJsonMissingPathCanFail2() public function testAssertJsonMissingPathCanFail3() { - $this->expectException(AssertionFailedError::class); $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));