Skip to content

Commit

Permalink
[10.x] Allow error list per field (#49309)
Browse files Browse the repository at this point in the history
* Allow error list per field

* Fix bug with existing assertion
  • Loading branch information
timacdonald authored Dec 11, 2023
1 parent 062450d commit 2039e83
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -1205,26 +1205,28 @@ public function assertInvalid($errors = null,

foreach (Arr::wrap($errors) as $key => $value) {
PHPUnit::assertArrayHasKey(
(is_int($key)) ? $value : $key,
$resolvedKey = (is_int($key)) ? $value : $key,
$sessionErrors,
"Failed to find a validation error in session for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage
"Failed to find a validation error in session for key: '{$resolvedKey}'".PHP_EOL.PHP_EOL.$errorMessage
);

if (! is_int($key)) {
$hasError = false;
foreach (Arr::wrap($value) as $message) {
if (! is_int($key)) {
$hasError = false;

foreach (Arr::wrap($sessionErrors[$key]) as $sessionErrorMessage) {
if (Str::contains($sessionErrorMessage, $value)) {
$hasError = true;
foreach (Arr::wrap($sessionErrors[$key]) as $sessionErrorMessage) {
if (Str::contains($sessionErrorMessage, $message)) {
$hasError = true;

break;
break;
}
}
}

if (! $hasError) {
PHPUnit::fail(
"Failed to find a validation error for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage
);
if (! $hasError) {
PHPUnit::fail(
"Failed to find a validation error for key and message: '$key' => '$message'".PHP_EOL.PHP_EOL.$errorMessage
);
}
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,54 @@ public function testAssertSessionValidationErrorsUsingAssertValid()
$testResponse->assertValid();
}

public function testAssertingKeyIsInvalidErrorMessage()
{
app()->instance('session.store', $store = new Store('test-session', new ArraySessionHandler(1)));
$store->put('errors', $errorBag = new ViewErrorBag);
$testResponse = TestResponse::fromBaseResponse(new Response);

try {
$testResponse->assertInvalid(['input_name']);
$this->fail();
} catch (AssertionFailedError $e) {
$this->assertStringStartsWith("Failed to find a validation error in session for key: 'input_name'", $e->getMessage());
}

try {
$testResponse->assertInvalid(['input_name' => 'Expected error message.']);
$this->fail();
} catch (AssertionFailedError $e) {
$this->assertStringStartsWith("Failed to find a validation error in session for key: 'input_name'", $e->getMessage());
}
}

public function testInvalidWithListOfErrors()
{
app()->instance('session.store', $store = new Store('test-session', new ArraySessionHandler(1)));

$store->put('errors', $errorBag = new ViewErrorBag);

$errorBag->put('default', new MessageBag([
'first_name' => [
'Your first name is required',
'Your first name must be at least 1 character',
],
]));

$testResponse = TestResponse::fromBaseResponse(new Response);

$testResponse->assertInvalid(['first_name' => 'Your first name is required']);
$testResponse->assertInvalid(['first_name' => 'Your first name must be at least 1 character']);
$testResponse->assertInvalid(['first_name' => ['Your first name is required', 'Your first name must be at least 1 character']]);

try {
$testResponse->assertInvalid(['first_name' => ['Your first name is required', 'FOO']]);
$this->fail();
} catch (AssertionFailedError $e) {
$this->assertStringStartsWith("Failed to find a validation error for key and message: 'first_name' => 'FOO'", $e->getMessage());
}
}

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

0 comments on commit 2039e83

Please sign in to comment.