Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Improve assertJsonValidationErrors error message, make it fail when no keys given #27495

Merged
merged 2 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,22 @@ public function assertJsonCount(int $count, $key = null)
*/
public function assertJsonValidationErrors($keys)
{
$keys = Arr::wrap($keys);

PHPUnit::assertNotEmpty($keys, 'You need to provide at least one expected key');

$errors = $this->json()['errors'] ?? [];

foreach (Arr::wrap($keys) as $key) {
$errorMessage = $errors
? 'Response has the following JSON validation errors: '.
PHP_EOL.PHP_EOL.json_encode($errors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
: '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}'"
"Failed to find a validation error in the response for key: '{$key}'".PHP_EOL.PHP_EOL.$errorMessage
);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@ public function testAssertJsonValidationErrorsCanFailWhenThereAreNoErrors()
$testResponse->assertJsonValidationErrors('bar');
}

public function testAssertJsonValidationErrorsFailsWhenGivenAnEmptyArray()
{
$this->expectException(AssertionFailedError::class);

$testResponse = TestResponse::fromBaseResponse(
(new Response)->setContent(json_encode(['errors' => ['foo' => 'oops']]))
);

$testResponse->assertJsonValidationErrors([]);
}

public function testAssertJsonMissingValidationErrors()
{
$baseResponse = tap(new Response, function ($response) {
Expand Down