From fe72373ea803871d90f352b3e68ccbd812d5519e Mon Sep 17 00:00:00 2001 From: Paul A Date: Wed, 10 Feb 2021 16:32:46 +0200 Subject: [PATCH] [8.x] Allow using dot syntax for $responseKey (#36196) * Allow using dot syntax for $responseKey. * Add tests. * Update message. * Formatting. * Update src/Illuminate/Testing/TestResponse.php Co-authored-by: Paul A. Co-authored-by: Taylor Otwell --- src/Illuminate/Testing/TestResponse.php | 6 ++-- tests/Testing/TestResponseTest.php | 46 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index 0f2a3a749551..1bfc75285518 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -692,13 +692,13 @@ public function assertJsonMissingValidationErrors($keys = null, $responseKey = ' $json = $this->json(); - if (! array_key_exists($responseKey, $json)) { - PHPUnit::assertArrayNotHasKey($responseKey, $json); + if (! Arr::has($json, $responseKey)) { + PHPUnit::assertTrue(true); return $this; } - $errors = $json[$responseKey]; + $errors = Arr::get($json, $responseKey, []); if (is_null($keys) && count($errors) > 0) { PHPUnit::fail( diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index 6c828f01db1c..42d16e72d3fa 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -1048,6 +1048,27 @@ public function testAssertJsonMissingValidationErrorsCanFail2() $response->assertJsonMissingValidationErrors('bar'); } + public function testAssertJsonMissingValidationErrorsCanFail3() + { + $this->expectException(AssertionFailedError::class); + + $baseResponse = tap(new Response, function ($response) { + $response->setContent( + json_encode([ + 'data' => [ + 'errors' => [ + 'foo' => ['one'], + ], + ], + ]), + ); + }); + + $response = TestResponse::fromBaseResponse($baseResponse); + + $response->assertJsonMissingValidationErrors('foo', 'data.errors'); + } + public function testAssertJsonMissingValidationErrorsWithoutArgument() { $data = ['status' => 'ok']; @@ -1109,6 +1130,31 @@ public function testAssertJsonMissingValidationErrorsCustomErrorsName() $testResponse->assertJsonMissingValidationErrors('bar', 'data'); } + public function testAssertJsonMissingValidationErrorsNestedCustomErrorsName1() + { + $data = [ + 'status' => 'ok', + 'data' => [ + 'errors' => ['foo' => 'oops'], + ], + ]; + + $testResponse = TestResponse::fromBaseResponse( + (new Response)->setContent(json_encode($data)) + ); + + $testResponse->assertJsonMissingValidationErrors('bar', 'data.errors'); + } + + public function testAssertJsonMissingValidationErrorsNestedCustomErrorsName2() + { + $testResponse = TestResponse::fromBaseResponse( + (new Response)->setContent(json_encode([])) + ); + + $testResponse->assertJsonMissingValidationErrors('bar', 'data.errors'); + } + public function testMacroable() { TestResponse::macro('foo', function () {