Skip to content

Commit

Permalink
Merge pull request #29380 from MoonHyuk/5.8
Browse files Browse the repository at this point in the history
[5.8] Fix assertJsonValidationErrors with muliple messages
  • Loading branch information
taylorotwell authored Aug 2, 2019
2 parents f5435fb + 9c5c640 commit 4d3f770
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,15 +675,21 @@ public function assertJsonValidationErrors($errors)
);

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

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

break;
}
}

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

Expand Down
16 changes: 16 additions & 0 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,22 @@ public function testAssertJsonValidationErrorMessagesMultipleMessages()
$testResponse->assertJsonValidationErrors(['one' => 'foo', 'two' => 'bar']);
}

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

$data = [
'status' => 'ok',
'errors' => ['one' => 'foo', 'two' => 'bar'],
];

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

$testResponse->assertJsonValidationErrors(['one' => 'foo', 'three' => 'baz']);
}

public function testAssertJsonValidationErrorMessagesMixed()
{
$data = [
Expand Down

0 comments on commit 4d3f770

Please sign in to comment.