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

[8.x] Allow assertion of multiple JSON validation errors. #39568

Merged
merged 3 commits into from
Nov 11, 2021
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
55 changes: 41 additions & 14 deletions src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,34 +832,61 @@ public function assertJsonValidationErrors($errors, $responseKey = 'errors')
: 'Response does not have JSON validation errors.';

foreach ($errors as $key => $value) {
PHPUnit::assertArrayHasKey(
(is_int($key)) ? $value : $key,
$jsonErrors,
"Failed to find a validation error in the response for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage
);
if (is_int($key)) {
$this->assertJsonValidationErrorFor($value, $responseKey);

if (! is_int($key)) {
$hasError = false;
continue;
}

$this->assertJsonValidationErrorFor($key, $responseKey);

foreach (Arr::wrap($value) as $expectedMessage) {
$errorMissing = true;

foreach (Arr::wrap($jsonErrors[$key]) as $jsonErrorMessage) {
if (Str::contains($jsonErrorMessage, $value)) {
$hasError = true;
if (Str::contains($jsonErrorMessage, $expectedMessage)) {
$errorMissing = false;

break;
}
}
}

if (! $hasError) {
PHPUnit::fail(
"Failed to find a validation error in the response for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage
);
}
if ($errorMissing) {
PHPUnit::fail(
"Failed to find a validation error in the response for key and message: '$key' => '$expectedMessage'".PHP_EOL.PHP_EOL.$errorMessage
);
}
}

return $this;
}

/**
* Assert the response has any JSON validation errors for the given key.
*
* @param string $key
* @param string $responseKey
* @return $this
*/
public function assertJsonValidationErrorFor($key, $responseKey = 'errors')
{
$jsonErrors = Arr::get($this->json(), $responseKey) ?? [];

$errorMessage = $jsonErrors
? 'Response has the following JSON validation 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.';

PHPUnit::assertArrayHasKey(
$key,
$jsonErrors,
"Failed to find a validation error in the response for key: '{$key}'".PHP_EOL.PHP_EOL.$errorMessage
);

return $this;
}

/**
* Assert that the response has no JSON validation errors for the given keys.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,45 @@ public function testAssertJsonValidationErrorMessagesMixedCanFail()
$testResponse->assertJsonValidationErrors(['one' => 'taylor', 'otwell']);
}

public function testAssertJsonValidationErrorMessagesMultipleErrors()
{
$data = [
'status' => 'ok',
'errors' => [
'one' => [
'First error message.',
'Second error message.',
],
],
];

$testResponse = TestResponse::fromBaseResponse(
(new Response)->setContent(json_encode($data))
);

$testResponse->assertJsonValidationErrors(['one' => ['First error message.', 'Second error message.']]);
}

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

$data = [
'status' => 'ok',
'errors' => [
'one' => [
'First error message.',
],
],
];

$testResponse = TestResponse::fromBaseResponse(
(new Response)->setContent(json_encode($data))
);

$testResponse->assertJsonValidationErrors(['one' => ['First error message.', 'Second error message.']]);
}

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