diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 4c5e5803a9cc..0b673143739a 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -631,30 +631,39 @@ 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); - 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) { - PHPUnit::assertArrayHasKey( - $key, - $errors, - "Failed to find a validation error in the response for key: '{$key}'".PHP_EOL.PHP_EOL.$errorMessage - ); + foreach ($errors as $key => $value) { + if (is_int($key)) { + PHPUnit::assertArrayHasKey( + $value, + $jsonErrors, + "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 a validation error in the response for key and message:' + ); + } } return $this; diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index 036acc62fc29..8ec5a3fb0010 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -355,6 +355,94 @@ 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 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 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) {