From 86377ecb513b7e508058d0187d98463171837c79 Mon Sep 17 00:00:00 2001 From: moonhyuk Date: Fri, 2 Aug 2019 11:58:55 +0900 Subject: [PATCH 1/4] Fix assertJsonValidationErrors with muliple messages --- .../Foundation/Testing/TestResponse.php | 11 +++++++---- tests/Foundation/FoundationTestResponseTest.php | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 720ddb597b8c..5a4a8708ffbc 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -675,15 +675,18 @@ public function assertJsonValidationErrors($errors) ); if (! is_int($key)) { + $hasError = false; foreach (Arr::wrap($jsonErrors[$key]) as $jsonErrorMessage) { if (Str::contains($jsonErrorMessage, $value)) { - return $this; + $hasError = true; } } - PHPUnit::fail( - "Failed to find a validation error in the response for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage - ); + if (!$hasError) { + PHPUnit::fail( + "Failed to find a validation error in the response for key and message: '$key' => '$value'" . PHP_EOL . PHP_EOL . $errorMessage + ); + } } } diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index d83f67dc50b0..d19ad0e76237 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -523,6 +523,22 @@ public function testAssertJsonValidationErrorMessagesMultipleMessages() $testResponse->assertJsonValidationErrors(['one' => 'foo', 'two' => 'bar']); } + public function testAssertJsonValidationErrorMessagesMultipleMessagesCanFail() + { + $this->expectException(AssertionFailedError::class); + + $data = [ + 'status' => 'ok', + 'errors' => ['one' => 'foo', 'two' => 'bar'], + ]; + + $testResponse = TestResponse::fromBaseResponse( + (new Response)->setContent(json_encode($data)) + ); + + $testResponse->assertJsonValidationErrors(['one' => 'foo', 'three' => 'baz']); + } + public function testAssertJsonValidationErrorMessagesMixed() { $data = [ From 9d8c11d5190e81b879f1b2bfdb4f4acfe9d94b88 Mon Sep 17 00:00:00 2001 From: moonhyuk Date: Fri, 2 Aug 2019 13:11:04 +0900 Subject: [PATCH 2/4] Fix coding style --- src/Illuminate/Foundation/Testing/TestResponse.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 5a4a8708ffbc..52fe0a9476b5 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -682,9 +682,9 @@ public function assertJsonValidationErrors($errors) } } - if (!$hasError) { + if (! $hasError) { PHPUnit::fail( - "Failed to find a validation error in the response for key and message: '$key' => '$value'" . PHP_EOL . PHP_EOL . $errorMessage + "Failed to find a validation error in the response for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage ); } } From 706e4640221804bc9615025753ce4c7ce5de51b8 Mon Sep 17 00:00:00 2001 From: moonhyuk Date: Fri, 2 Aug 2019 13:11:34 +0900 Subject: [PATCH 3/4] Improve code to break early --- src/Illuminate/Foundation/Testing/TestResponse.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 52fe0a9476b5..61e62fbb922a 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -679,6 +679,7 @@ public function assertJsonValidationErrors($errors) foreach (Arr::wrap($jsonErrors[$key]) as $jsonErrorMessage) { if (Str::contains($jsonErrorMessage, $value)) { $hasError = true; + break; } } From 9c5c640d0871924a77a4f7db2b82884fb4227d89 Mon Sep 17 00:00:00 2001 From: Moonhyuk Im Date: Fri, 2 Aug 2019 17:27:18 +0900 Subject: [PATCH 4/4] Apply suggestions from code review Co-Authored-By: Dries Vints --- src/Illuminate/Foundation/Testing/TestResponse.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 61e62fbb922a..8a6a947a4ac4 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -676,9 +676,11 @@ public function assertJsonValidationErrors($errors) if (! is_int($key)) { $hasError = false; + foreach (Arr::wrap($jsonErrors[$key]) as $jsonErrorMessage) { if (Str::contains($jsonErrorMessage, $value)) { $hasError = true; + break; } }