Skip to content

Commit 73bf339

Browse files
authored
[8.x] Assert invalid (#38384)
* add assertInvalid method * run cleaner * Apply fixes from StyleCI * add assertValid * add test * update signature * Fix tests Co-authored-by: Taylor Otwell <[email protected]>
1 parent 3a8efd7 commit 73bf339

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

src/Illuminate/Testing/TestResponse.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,105 @@ protected function responseHasView()
10221022
return isset($this->original) && $this->original instanceof View;
10231023
}
10241024

1025+
/**
1026+
* Assert that the given keys do not have validation errors.
1027+
*
1028+
* @param array|null $keys
1029+
* @param string $errorBag
1030+
* @param string $responseKey
1031+
* @return $this
1032+
*/
1033+
public function assertValid($keys = null, $errorBag = 'default', $responseKey = 'errors')
1034+
{
1035+
if ($this->baseResponse->headers->get('Content-Type') === 'application/json') {
1036+
return $this->assertJsonMissingValidationErrors($keys, $responseKey);
1037+
}
1038+
1039+
if ($this->session()->get('errors')) {
1040+
$errors = $this->session()->get('errors')->getBag($errorBag)->getMessages();
1041+
} else {
1042+
$errors = [];
1043+
}
1044+
1045+
if (empty($errors)) {
1046+
PHPUnit::assertTrue(true);
1047+
1048+
return $this;
1049+
}
1050+
1051+
if (is_null($keys) && count($errors) > 0) {
1052+
PHPUnit::fail(
1053+
'Response has unexpected validation errors: '.PHP_EOL.PHP_EOL.
1054+
json_encode($errors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
1055+
);
1056+
}
1057+
1058+
foreach (Arr::wrap($keys) as $key) {
1059+
PHPUnit::assertFalse(
1060+
isset($errors[$key]),
1061+
"Found unexpected validation error for key: '{$key}'"
1062+
);
1063+
}
1064+
1065+
return $this;
1066+
}
1067+
1068+
/**
1069+
* Assert that the response has the given validation errors.
1070+
*
1071+
* @param array $errors
1072+
* @param string $errorBag
1073+
* @param string $responseKey
1074+
* @return $this
1075+
*/
1076+
public function assertInvalid($errors,
1077+
$errorBag = 'default',
1078+
$responseKey = 'errors')
1079+
{
1080+
if ($this->baseResponse->headers->get('Content-Type') === 'application/json') {
1081+
return $this->assertJsonValidationErrors($errors, $responseKey);
1082+
}
1083+
1084+
$this->assertSessionHas('errors');
1085+
1086+
$keys = (array) $errors;
1087+
1088+
$sessionErrors = $this->session()->get('errors')->getBag($errorBag)->getMessages();
1089+
1090+
$errorMessage = $sessionErrors
1091+
? 'Response has the following validation errors in the session:'.
1092+
PHP_EOL.PHP_EOL.json_encode($sessionErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL
1093+
: 'Response does not have validation errors in the session.';
1094+
1095+
foreach ($errors as $key => $value) {
1096+
PHPUnit::assertArrayHasKey(
1097+
(is_int($key)) ? $value : $key,
1098+
$sessionErrors,
1099+
"Failed to find a validation error in session for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage
1100+
);
1101+
1102+
if (! is_int($key)) {
1103+
$hasError = false;
1104+
1105+
foreach (Arr::wrap($sessionErrors[$key]) as $sessionErrorMessage) {
1106+
if (Str::contains($sessionErrorMessage, $value)) {
1107+
$hasError = true;
1108+
1109+
break;
1110+
}
1111+
}
1112+
1113+
if (! $hasError) {
1114+
PHPUnit::fail(
1115+
"Failed to find a validation error for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage
1116+
);
1117+
}
1118+
}
1119+
}
1120+
1121+
return $this;
1122+
}
1123+
10251124
/**
10261125
* Assert that the session has a given value.
10271126
*

tests/Testing/TestResponseTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Illuminate\Http\Response;
1414
use Illuminate\Session\ArraySessionHandler;
1515
use Illuminate\Session\Store;
16+
use Illuminate\Support\MessageBag;
17+
use Illuminate\Support\ViewErrorBag;
1618
use Illuminate\Testing\Fluent\AssertableJson;
1719
use Illuminate\Testing\TestResponse;
1820
use JsonSerializable;
@@ -916,6 +918,55 @@ public function testAssertJsonValidationErrors()
916918
$testResponse->assertJsonValidationErrors('foo');
917919
}
918920

921+
public function testAssertJsonValidationErrorsUsingAssertInvalid()
922+
{
923+
$data = [
924+
'status' => 'ok',
925+
'errors' => ['foo' => 'oops'],
926+
];
927+
928+
$testResponse = TestResponse::fromBaseResponse(
929+
(new Response('', 200, ['Content-Type' => 'application/json']))->setContent(json_encode($data))
930+
);
931+
932+
$testResponse->assertInvalid('foo');
933+
}
934+
935+
public function testAssertSessionValidationErrorsUsingAssertInvalid()
936+
{
937+
app()->instance('session.store', $store = new Store('test-session', new ArraySessionHandler(1)));
938+
939+
$store->put('errors', $errorBag = new ViewErrorBag);
940+
941+
$errorBag->put('default', new MessageBag([
942+
'first_name' => [
943+
'Your first name is required',
944+
'Your first name must be at least 1 character',
945+
],
946+
]));
947+
948+
$testResponse = TestResponse::fromBaseResponse(new Response);
949+
950+
$testResponse->assertValid(['last_name']);
951+
$testResponse->assertInvalid(['first_name']);
952+
$testResponse->assertInvalid(['first_name' => 'required']);
953+
$testResponse->assertInvalid(['first_name' => 'character']);
954+
}
955+
956+
public function testAssertSessionValidationErrorsUsingAssertValid()
957+
{
958+
app()->instance('session.store', $store = new Store('test-session', new ArraySessionHandler(1)));
959+
960+
$store->put('errors', $errorBag = new ViewErrorBag);
961+
962+
$errorBag->put('default', new MessageBag([
963+
]));
964+
965+
$testResponse = TestResponse::fromBaseResponse(new Response);
966+
967+
$testResponse->assertValid();
968+
}
969+
919970
public function testAssertJsonValidationErrorsCustomErrorsName()
920971
{
921972
$data = [

0 commit comments

Comments
 (0)