From 0191ae0b06cc00273073b1f3bf55595835a651a4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 14 Aug 2021 12:18:40 -0500 Subject: [PATCH 1/7] add assertInvalid method --- src/Illuminate/Testing/TestResponse.php | 56 +++++++++++++++++++++++++ tests/Testing/TestResponseTest.php | 36 ++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index caf3764a28f1..14197119089f 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -1103,6 +1103,62 @@ public function assertSessionHasInput($key, $value = null) return $this; } + /** + * Assert that the response has the given validation errors. + * + * @param array $keys + * @param string $errorBag + * @param string $responseKey + * @return $this + */ + public function assertInvalid($errors = [], + $errorBag = 'default', + $responseKey = 'errors') + { + if ($this->baseResponse->headers->get('Content-Type') === 'application/json') { + return $this->assertJsonValidationErrors($errors, $responseKey); + } + + $this->assertSessionHas('errors'); + + $keys = (array) $errors; + + $sessionErrors = $this->session()->get('errors')->getBag($errorBag)->getMessages(); + + $errorMessage = $sessionErrors + ? 'Response has the following validation errors in the session:'. + PHP_EOL.PHP_EOL.json_encode($sessionErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL + : 'Response does not have validation errors in the session.'; + + foreach ($errors as $key => $value) { + PHPUnit::assertArrayHasKey( + (is_int($key)) ? $value : $key, + $sessionErrors, + "Failed to find a validation error in session for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage + ); + + if (! is_int($key)) { + $hasError = false; + + foreach (Arr::wrap($sessionErrors[$key]) as $sessionErrorMessage) { + if (Str::contains($sessionErrorMessage, $value)) { + $hasError = true; + + break; + } + } + + if (! $hasError) { + PHPUnit::fail( + "Failed to find a validation error for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage + ); + } + } + } + + return $this; + } + /** * Assert that the session has the given errors. * diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index b0b9780bb658..52c91aa05bf8 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -13,6 +13,8 @@ use Illuminate\Http\Response; use Illuminate\Session\ArraySessionHandler; use Illuminate\Session\Store; +use Illuminate\Support\MessageBag; +use Illuminate\Support\ViewErrorBag; use Illuminate\Testing\Fluent\AssertableJson; use Illuminate\Testing\TestResponse; use JsonSerializable; @@ -916,6 +918,40 @@ public function testAssertJsonValidationErrors() $testResponse->assertJsonValidationErrors('foo'); } + public function testAssertJsonValidationErrorsUsingAssertInvalid() + { + $data = [ + 'status' => 'ok', + 'errors' => ['foo' => 'oops'], + ]; + + $testResponse = TestResponse::fromBaseResponse( + (new Response('', 200, ['Content-Type' => 'application/json']))->setContent(json_encode($data)) + ); + + $testResponse->assertInvalid('foo'); + } + + public function testAssertSessionValidationErrorsUsingAssertInvalid() + { + 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']); + $testResponse->assertInvalid(['first_name' => 'required']); + $testResponse->assertInvalid(['first_name' => 'character']); + } + public function testAssertJsonValidationErrorsCustomErrorsName() { $data = [ From 597d864711e56ab83bfda48738e7d1796e3bb0a2 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 14 Aug 2021 12:19:01 -0500 Subject: [PATCH 2/7] run cleaner --- tests/Testing/TestResponseTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index 52c91aa05bf8..f9a1dc666f3a 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -50,8 +50,7 @@ public function testAssertViewHas() public function testAssertViewHasModel() { - $model = new class extends Model - { + $model = new class extends Model { public function is($model) { return $this == $model; From 3d74802a878bfe0d0e104f8e20ae302d5fbf57e6 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 14 Aug 2021 17:20:16 +0000 Subject: [PATCH 3/7] Apply fixes from StyleCI --- tests/Testing/TestResponseTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index f9a1dc666f3a..52c91aa05bf8 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -50,7 +50,8 @@ public function testAssertViewHas() public function testAssertViewHasModel() { - $model = new class extends Model { + $model = new class extends Model + { public function is($model) { return $this == $model; From 7dfa7e18ae72f6d13748ce93c1a71c9fe6d166d7 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 14 Aug 2021 12:29:44 -0500 Subject: [PATCH 4/7] add assertValid --- src/Illuminate/Testing/TestResponse.php | 155 +++++++++++++++--------- 1 file changed, 99 insertions(+), 56 deletions(-) diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index 14197119089f..1dc2dd5c698c 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -1022,6 +1022,105 @@ protected function responseHasView() return isset($this->original) && $this->original instanceof View; } + /** + * Assert that the given keys do not have validation errors. + * + * @param array|null $keys + * @param string $errorBag + * @param string $responseKey + * @return $this + */ + public function assertValid($keys = null, $errorBag = 'default', $responseKey = 'errors') + { + if ($this->baseResponse->headers->get('Content-Type') === 'application/json') { + return $this->assertJsonMissingValidationErrors($keys, $responseKey); + } + + if ($this->session()->get('errors')) { + $errors = $this->session()->get('errors')->getBag($errorBag)->getMessages(); + } else { + $errors = []; + } + + if (empty($errors)) { + PHPUnit::assertTrue(true); + + return $this; + } + + if (is_null($keys) && count($errors) > 0) { + PHPUnit::fail( + 'Response has unexpected validation errors: '.PHP_EOL.PHP_EOL. + json_encode($errors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) + ); + } + + foreach (Arr::wrap($keys) as $key) { + PHPUnit::assertFalse( + isset($errors[$key]), + "Found unexpected validation error for key: '{$key}'" + ); + } + + return $this; + } + + /** + * Assert that the response has the given validation errors. + * + * @param array $keys + * @param string $errorBag + * @param string $responseKey + * @return $this + */ + public function assertInvalid($errors = [], + $errorBag = 'default', + $responseKey = 'errors') + { + if ($this->baseResponse->headers->get('Content-Type') === 'application/json') { + return $this->assertJsonValidationErrors($errors, $responseKey); + } + + $this->assertSessionHas('errors'); + + $keys = (array) $errors; + + $sessionErrors = $this->session()->get('errors')->getBag($errorBag)->getMessages(); + + $errorMessage = $sessionErrors + ? 'Response has the following validation errors in the session:'. + PHP_EOL.PHP_EOL.json_encode($sessionErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL + : 'Response does not have validation errors in the session.'; + + foreach ($errors as $key => $value) { + PHPUnit::assertArrayHasKey( + (is_int($key)) ? $value : $key, + $sessionErrors, + "Failed to find a validation error in session for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage + ); + + if (! is_int($key)) { + $hasError = false; + + foreach (Arr::wrap($sessionErrors[$key]) as $sessionErrorMessage) { + if (Str::contains($sessionErrorMessage, $value)) { + $hasError = true; + + break; + } + } + + if (! $hasError) { + PHPUnit::fail( + "Failed to find a validation error for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage + ); + } + } + } + + return $this; + } + /** * Assert that the session has a given value. * @@ -1103,62 +1202,6 @@ public function assertSessionHasInput($key, $value = null) return $this; } - /** - * Assert that the response has the given validation errors. - * - * @param array $keys - * @param string $errorBag - * @param string $responseKey - * @return $this - */ - public function assertInvalid($errors = [], - $errorBag = 'default', - $responseKey = 'errors') - { - if ($this->baseResponse->headers->get('Content-Type') === 'application/json') { - return $this->assertJsonValidationErrors($errors, $responseKey); - } - - $this->assertSessionHas('errors'); - - $keys = (array) $errors; - - $sessionErrors = $this->session()->get('errors')->getBag($errorBag)->getMessages(); - - $errorMessage = $sessionErrors - ? 'Response has the following validation errors in the session:'. - PHP_EOL.PHP_EOL.json_encode($sessionErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL - : 'Response does not have validation errors in the session.'; - - foreach ($errors as $key => $value) { - PHPUnit::assertArrayHasKey( - (is_int($key)) ? $value : $key, - $sessionErrors, - "Failed to find a validation error in session for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage - ); - - if (! is_int($key)) { - $hasError = false; - - foreach (Arr::wrap($sessionErrors[$key]) as $sessionErrorMessage) { - if (Str::contains($sessionErrorMessage, $value)) { - $hasError = true; - - break; - } - } - - if (! $hasError) { - PHPUnit::fail( - "Failed to find a validation error for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage - ); - } - } - } - - return $this; - } - /** * Assert that the session has the given errors. * From f769d372bc2b403be00a60d11b2b74b304446ef9 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 14 Aug 2021 12:30:12 -0500 Subject: [PATCH 5/7] add test --- tests/Testing/TestResponseTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index 52c91aa05bf8..eff1f8c4d551 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -947,6 +947,8 @@ public function testAssertSessionValidationErrorsUsingAssertInvalid() $testResponse = TestResponse::fromBaseResponse(new Response); + $testResponse->assertValid(['last_name']); + $testResponse->assertInvalid(); $testResponse->assertInvalid(['first_name']); $testResponse->assertInvalid(['first_name' => 'required']); $testResponse->assertInvalid(['first_name' => 'character']); From 316bf92362119bfb465bd71b690a0b2daec78ffe Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 14 Aug 2021 13:27:26 -0500 Subject: [PATCH 6/7] update signature --- src/Illuminate/Testing/TestResponse.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index 1dc2dd5c698c..9e2db201ce20 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -1068,12 +1068,12 @@ public function assertValid($keys = null, $errorBag = 'default', $responseKey = /** * Assert that the response has the given validation errors. * - * @param array $keys + * @param array $errors * @param string $errorBag * @param string $responseKey * @return $this */ - public function assertInvalid($errors = [], + public function assertInvalid($errors, $errorBag = 'default', $responseKey = 'errors') { From 5420b311884e96ee41ee6e902082048a2169b02b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 14 Aug 2021 16:09:20 -0500 Subject: [PATCH 7/7] Fix tests --- tests/Testing/TestResponseTest.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index eff1f8c4d551..fe5cac6699a2 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -948,12 +948,25 @@ public function testAssertSessionValidationErrorsUsingAssertInvalid() $testResponse = TestResponse::fromBaseResponse(new Response); $testResponse->assertValid(['last_name']); - $testResponse->assertInvalid(); $testResponse->assertInvalid(['first_name']); $testResponse->assertInvalid(['first_name' => 'required']); $testResponse->assertInvalid(['first_name' => 'character']); } + public function testAssertSessionValidationErrorsUsingAssertValid() + { + app()->instance('session.store', $store = new Store('test-session', new ArraySessionHandler(1))); + + $store->put('errors', $errorBag = new ViewErrorBag); + + $errorBag->put('default', new MessageBag([ + ])); + + $testResponse = TestResponse::fromBaseResponse(new Response); + + $testResponse->assertValid(); + } + public function testAssertJsonValidationErrorsCustomErrorsName() { $data = [