From 1b19f405937e238181f8443f0f490c291cc294e3 Mon Sep 17 00:00:00 2001 From: ahinkle Date: Mon, 25 Feb 2019 08:42:28 -0600 Subject: [PATCH 1/9] Add message value to assertJsonValidationErrors --- .../Foundation/Testing/TestResponse.php | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 4c5e5803a9cc..4c50510c0ee7 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -631,32 +631,38 @@ public function assertJsonCount(int $count, $key = null) } /** - * Assert that the response has the given JSON validation errors for the given keys. + * Assert that the response has the given JSON validation errors. * - * @param string|array $keys + * @param string|array $errors * @return $this */ - public function assertJsonValidationErrors($keys) + public function assertJsonValidationErrors($errors) { - $keys = Arr::wrap($keys); + $errors = Arr::wrap($errors); + + $checkMessages = array_keys($errors) !== range(0, count($errors) - 1); - PHPUnit::assertNotEmpty($keys, 'No keys were provided.'); + PHPUnit::assertNotEmpty($errors, 'No validation errors were provided.'); - $errors = $this->json()['errors'] ?? []; + $jsonErrors = $this->json()['errors'] ?? []; - $errorMessage = $errors + $errorMessage = $jsonErrors ? 'Response has the following JSON validation errors:'. - PHP_EOL.PHP_EOL.json_encode($errors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL + PHP_EOL.PHP_EOL.json_encode($jsonErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL : 'Response does not have JSON validation errors.'; - foreach ($keys as $key) { + foreach ($errors as $key => $error) { PHPUnit::assertArrayHasKey( - $key, - $errors, - "Failed to find a validation error in the response for key: '{$key}'".PHP_EOL.PHP_EOL.$errorMessage + $checkMessages ? $key : $error, + $jsonErrors, + "Failed to find a validation error in the response for key: '{$error}'".PHP_EOL.PHP_EOL.$errorMessage ); } + if ($checkMessages) { + PHPUnit::assertArraySubset($errors, $jsonErrors, false, 'Failed to find validation messages:'); + } + return $this; } From 5023aff3d4baf8a1f0377f0efd70c564a0b8539d Mon Sep 17 00:00:00 2001 From: ahinkle Date: Mon, 25 Feb 2019 08:45:10 -0600 Subject: [PATCH 2/9] Add missing test for assertJsonVlaidationErros via Array --- tests/Foundation/FoundationTestResponseTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index 036acc62fc29..e6bc4196e782 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -355,6 +355,20 @@ public function testAssertJsonValidationErrorsFailsWhenGivenAnEmptyArray() $testResponse->assertJsonValidationErrors([]); } + public function testAssertJsonValidationErrorsWithArray() + { + $data = [ + 'status' => 'ok', + 'errors' => ['foo' => 'one', 'bar' => 'two'], + ]; + + $testResponse = TestResponse::fromBaseResponse( + (new Response)->setContent(json_encode($data)) + ); + + $testResponse->assertJsonValidationErrors(['foo', 'bar']); + } + public function testAssertJsonMissingValidationErrors() { $baseResponse = tap(new Response, function ($response) { From f2e30b763c5039012593071bb654f8baa7ec77e9 Mon Sep 17 00:00:00 2001 From: ahinkle Date: Mon, 25 Feb 2019 08:46:06 -0600 Subject: [PATCH 3/9] Add tests for message value within assertJsonValidationErrors --- .../Foundation/FoundationTestResponseTest.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index e6bc4196e782..fdc748f663a2 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -369,6 +369,50 @@ public function testAssertJsonValidationErrorsWithArray() $testResponse->assertJsonValidationErrors(['foo', 'bar']); } + public function testAssertJsonValidationErrorMessages() + { + $data = [ + 'status' => 'ok', + 'errors' => ['key' => 'foo'], + ]; + + $testResponse = TestResponse::fromBaseResponse( + (new Response)->setContent(json_encode($data)) + ); + + $testResponse->assertJsonValidationErrors(['key' => 'foo']); + } + + public function testAssertJsonValidationErrorMessagesCanFail() + { + $this->expectException(AssertionFailedError::class); + + $data = [ + 'status' => 'ok', + 'errors' => ['key' => 'foo'], + ]; + + $testResponse = TestResponse::fromBaseResponse( + (new Response)->setContent(json_encode($data)) + ); + + $testResponse->assertJsonValidationErrors(['key' => 'bar']); + } + + public function testAssertJsonValidationErrorMessagesMultipleMessages() + { + $data = [ + 'status' => 'ok', + 'errors' => ['one' => 'foo', 'two' => 'bar'], + ]; + + $testResponse = TestResponse::fromBaseResponse( + (new Response)->setContent(json_encode($data)) + ); + + $testResponse->assertJsonValidationErrors(['one' => 'foo', 'two' => 'bar']); + } + public function testAssertJsonMissingValidationErrors() { $baseResponse = tap(new Response, function ($response) { From b1d16ffb80c09d57fb98be544c5508cbb7b7d56f Mon Sep 17 00:00:00 2001 From: ahinkle Date: Mon, 25 Feb 2019 18:17:19 -0600 Subject: [PATCH 4/9] Add Conditional --- .../Foundation/Testing/TestResponse.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 4c50510c0ee7..230ac7bceada 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -651,16 +651,16 @@ public function assertJsonValidationErrors($errors) PHP_EOL.PHP_EOL.json_encode($jsonErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL : 'Response does not have JSON validation errors.'; - foreach ($errors as $key => $error) { - PHPUnit::assertArrayHasKey( - $checkMessages ? $key : $error, - $jsonErrors, - "Failed to find a validation error in the response for key: '{$error}'".PHP_EOL.PHP_EOL.$errorMessage - ); - } - if ($checkMessages) { PHPUnit::assertArraySubset($errors, $jsonErrors, false, 'Failed to find validation messages:'); + } else { + foreach ($errors as $key) { + PHPUnit::assertArrayHasKey( + $key, + $jsonErrors, + "Failed to find a validation error in the response for key: '{$key}'".PHP_EOL.PHP_EOL.$errorMessage + ); + } } return $this; From 02990f94ac21d25221128cd83f9008a4e7392f8e Mon Sep 17 00:00:00 2001 From: ahinkle Date: Mon, 25 Feb 2019 18:43:58 -0600 Subject: [PATCH 5/9] Wording for error handling --- src/Illuminate/Foundation/Testing/TestResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 230ac7bceada..e15cfc217a20 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -652,7 +652,7 @@ public function assertJsonValidationErrors($errors) : 'Response does not have JSON validation errors.'; if ($checkMessages) { - PHPUnit::assertArraySubset($errors, $jsonErrors, false, 'Failed to find validation messages:'); + PHPUnit::assertArraySubset($errors, $jsonErrors, false, 'Failed to find validation errors:'); } else { foreach ($errors as $key) { PHPUnit::assertArrayHasKey( From 236a6cd0c9c032f1964ea7381803959e4a67ed21 Mon Sep 17 00:00:00 2001 From: ahinkle Date: Tue, 26 Feb 2019 20:34:29 -0600 Subject: [PATCH 6/9] Refactor loop, allowing for mixed types --- src/Illuminate/Foundation/Testing/TestResponse.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index e15cfc217a20..9a0ffa6c4243 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -640,8 +640,6 @@ public function assertJsonValidationErrors($errors) { $errors = Arr::wrap($errors); - $checkMessages = array_keys($errors) !== range(0, count($errors) - 1); - PHPUnit::assertNotEmpty($errors, 'No validation errors were provided.'); $jsonErrors = $this->json()['errors'] ?? []; @@ -651,15 +649,15 @@ public function assertJsonValidationErrors($errors) PHP_EOL.PHP_EOL.json_encode($jsonErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL : 'Response does not have JSON validation errors.'; - if ($checkMessages) { - PHPUnit::assertArraySubset($errors, $jsonErrors, false, 'Failed to find validation errors:'); - } else { - foreach ($errors as $key) { + foreach ($errors as $key => $value) { + if (is_int($key)) { PHPUnit::assertArrayHasKey( - $key, + $value, $jsonErrors, - "Failed to find a validation error in the response for key: '{$key}'".PHP_EOL.PHP_EOL.$errorMessage + "Failed to find a validation error in the response for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage ); + } else { + PHPUnit::assertArraySubset([$key => $value], $jsonErrors, false, 'Failed to find validation errors:'); } } From 1e175ccd277d71443f4c74ffbc0d63b923cac1fb Mon Sep 17 00:00:00 2001 From: ahinkle Date: Tue, 26 Feb 2019 20:38:49 -0600 Subject: [PATCH 7/9] Add tests for mixed types --- .../Foundation/Testing/TestResponse.php | 7 ++++- .../Foundation/FoundationTestResponseTest.php | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 9a0ffa6c4243..7ba4656622bb 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -657,7 +657,12 @@ public function assertJsonValidationErrors($errors) "Failed to find a validation error in the response for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage ); } else { - PHPUnit::assertArraySubset([$key => $value], $jsonErrors, false, 'Failed to find validation errors:'); + PHPUnit::assertArraySubset( + [$key => $value], + $jsonErrors, + false, + 'Failed to find validation errors:' + ); } } diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index fdc748f663a2..8ec5a3fb0010 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -413,6 +413,36 @@ public function testAssertJsonValidationErrorMessagesMultipleMessages() $testResponse->assertJsonValidationErrors(['one' => 'foo', 'two' => 'bar']); } + public function testAssertJsonValidationErrorMessagesMixed() + { + $data = [ + 'status' => 'ok', + 'errors' => ['one' => 'foo', 'two' => 'bar'], + ]; + + $testResponse = TestResponse::fromBaseResponse( + (new Response)->setContent(json_encode($data)) + ); + + $testResponse->assertJsonValidationErrors(['one' => 'foo', 'two']); + } + + public function testAssertJsonValidationErrorMessagesMixedCanFail() + { + $this->expectException(AssertionFailedError::class); + + $data = [ + 'status' => 'ok', + 'errors' => ['one' => 'foo', 'two' => 'bar'], + ]; + + $testResponse = TestResponse::fromBaseResponse( + (new Response)->setContent(json_encode($data)) + ); + + $testResponse->assertJsonValidationErrors(['one' => 'taylor', 'otwell']); + } + public function testAssertJsonMissingValidationErrors() { $baseResponse = tap(new Response, function ($response) { From f4eddd9d3cc213011a94745a5d2166d9f1e4cf0a Mon Sep 17 00:00:00 2001 From: ahinkle Date: Tue, 26 Feb 2019 20:47:12 -0600 Subject: [PATCH 8/9] Wording --- src/Illuminate/Foundation/Testing/TestResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 7ba4656622bb..defa311e5ee6 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -661,7 +661,7 @@ public function assertJsonValidationErrors($errors) [$key => $value], $jsonErrors, false, - 'Failed to find validation errors:' + "Failed to find a validation error in the response for key and message:" ); } } From 9ddb9634ebbe537176b4c483ef490dfc4d2877d5 Mon Sep 17 00:00:00 2001 From: ahinkle Date: Tue, 26 Feb 2019 20:48:58 -0600 Subject: [PATCH 9/9] Quotes StyleCI Fix --- src/Illuminate/Foundation/Testing/TestResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index defa311e5ee6..0b673143739a 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -661,7 +661,7 @@ public function assertJsonValidationErrors($errors) [$key => $value], $jsonErrors, false, - "Failed to find a validation error in the response for key and message:" + 'Failed to find a validation error in the response for key and message:' ); } }