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.8] Add message value assertion to assertJsonValidationErrors #28787

Merged
merged 1 commit into from
Jun 11, 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
32 changes: 20 additions & 12 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,30 +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);

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 => $value) {
PHPUnit::assertArrayHasKey(
$key,
$errors,
"Failed to find a validation error in the response for key: '{$key}'".PHP_EOL.PHP_EOL.$errorMessage
(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)) {
PHPUnit::assertStringContainsString(
$value,
$jsonErrors[$key],
"Failed to find a validation error in the response for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage
);
}
}

return $this;
Expand Down
118 changes: 118 additions & 0 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,124 @@ 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 testAssertJsonValidationErrorContainsMessages()
{
$data = [
'status' => 'ok',
'errors' => ['key' => 'foo bar'],
];

$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 testAssertJsonValidationErrorMessageKeyCanFail()
{
$this->expectException(AssertionFailedError::class);

$data = [
'status' => 'ok',
'errors' => ['foo' => 'value'],
];

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

$testResponse->assertJsonValidationErrors(['bar' => 'value']);
}

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) {
Expand Down