99use JsonSerializable ;
1010use PHPUnit \Framework \Assert ;
1111use PHPUnit \Framework \ExpectationFailedException ;
12+ use Tempest \Container \Container ;
1213use Tempest \Cryptography \Encryption \Encrypter ;
1314use Tempest \Http \Cookie \Cookie ;
1415use Tempest \Http \Response ;
1718use Tempest \Http \Status ;
1819use Tempest \Support \Arr ;
1920use Tempest \Validation \Rule ;
21+ use Tempest \Validation \Validator ;
2022use Tempest \View \View ;
2123use Tempest \View \ViewRenderer ;
2224
@@ -27,6 +29,7 @@ final class TestResponseHelper
2729{
2830 public function __construct (
2931 private(set) Response $ response ,
32+ private(set) ?Container $ container = null ,
3033 ) {}
3134
3235 public Status $ status {
@@ -153,7 +156,7 @@ public function assertHasCookie(string $key, null|string|Closure $value = null):
153156 message: sprintf ('No cookie was set for [%s], available cookies: %s ' , $ key , implode (', ' , array_keys ($ cookies ))),
154157 );
155158
156- $ encrypter = get (Encrypter::class);
159+ $ encrypter = $ this -> container -> get (Encrypter::class);
157160 $ cookie = $ cookies [$ key ]->value
158161 ? $ encrypter ->decrypt ($ cookies [$ key ]->value )
159162 : '' ;
@@ -171,14 +174,14 @@ public function assertHasCookie(string $key, null|string|Closure $value = null):
171174
172175 public function assertHasSession (string $ key , ?Closure $ callback = null ): self
173176 {
174- /** @var Session $session */
175- $ session = get (Session::class);
177+ $ this ->assertHasContainer ();
176178
179+ $ session = $ this ->container ->get (Session::class);
177180 $ data = $ session ->get ($ key );
178181
179182 Assert::assertNotNull (
180- $ data ,
181- sprintf (
183+ actual: $ data ,
184+ message: sprintf (
182185 'No session value was set for [%s], available session keys: %s ' ,
183186 $ key ,
184187 implode (', ' , array_keys ($ session ->data )),
@@ -194,15 +197,13 @@ public function assertHasSession(string $key, ?Closure $callback = null): self
194197
195198 public function assertHasValidationError (string $ key , ?Closure $ callback = null ): self
196199 {
197- /** @var Session $session */
198- $ session = get (Session::class);
199-
200+ $ session = $ this ->container ->get (Session::class);
200201 $ validationErrors = $ session ->get (Session::VALIDATION_ERRORS ) ?? [];
201202
202203 Assert::assertArrayHasKey (
203- $ key ,
204- $ validationErrors ,
205- sprintf (
204+ key: $ key ,
205+ array: $ validationErrors ,
206+ message: sprintf (
206207 'No validation error was set for [%s], available validation errors: %s ' ,
207208 $ key ,
208209 implode (', ' , array_keys ($ validationErrors )),
@@ -218,23 +219,16 @@ public function assertHasValidationError(string $key, ?Closure $callback = null)
218219
219220 public function assertHasNoValidationsErrors (): self
220221 {
221- /** @var Session $session */
222- $ session = get (Session::class);
223-
222+ $ session = $ this ->container ->get (Session::class);
224223 $ validationErrors = $ session ->get (Session::VALIDATION_ERRORS ) ?? [];
225224
226225 Assert::assertEmpty (
227- $ validationErrors ,
228- sprintf (
229- 'There should be no validation errors, but there were: %s ' ,
230- arr ($ validationErrors )
231- ->map (function (array $ failingRules , $ key ) {
232- $ failingRules = arr ($ failingRules )->map (fn (Rule $ rule ) => $ rule ->getTranslationVariables ())->implode (', ' );
233-
234- return $ key . ': ' . $ failingRules ;
235- })
236- ->implode (', ' ),
237- ),
226+ actual: $ validationErrors ,
227+ message: arr ($ validationErrors )
228+ ->map (fn (array $ failingRules , string $ key ) => $ key . ': ' . arr ($ failingRules )->map (fn (Rule $ rule ) => $ rule ::class)->implode (', ' ))
229+ ->implode (', ' )
230+ ->prepend ('There should be no validation errors, but there were: ' )
231+ ->toString (),
238232 );
239233
240234 return $ this ;
@@ -245,7 +239,7 @@ public function assertSee(string $search): self
245239 $ body = $ this ->body ;
246240
247241 if ($ body instanceof View) {
248- $ body = get (ViewRenderer::class)->render ($ body );
242+ $ body = $ this -> container -> get (ViewRenderer::class)->render ($ body );
249243 }
250244
251245 Assert::assertStringContainsString ($ search , $ body );
@@ -258,7 +252,7 @@ public function assertNotSee(string $search): self
258252 $ body = $ this ->body ;
259253
260254 if ($ body instanceof View) {
261- $ body = get (ViewRenderer::class)->render ($ body );
255+ $ body = $ this -> container -> get (ViewRenderer::class)->render ($ body );
262256 }
263257
264258 Assert::assertStringNotContainsString ($ search , $ body );
@@ -481,18 +475,21 @@ public function assertJsonContains(array $expected): self
481475 */
482476 public function assertHasJsonValidationErrors (array $ expectedErrors ): self
483477 {
478+ $ this ->assertHasContainer ();
479+
484480 Assert::assertInstanceOf (Invalid::class, $ this ->response );
485481 Assert::assertContains ($ this ->response ->status , [Status::BAD_REQUEST , Status::FOUND ]);
486482 Assert::assertNotNull ($ this ->response ->getHeader ('x-validation ' ));
487483
488- $ session = get (Session::class);
484+ $ session = $ this ->container ->get (Session::class);
485+ $ validator = $ this ->container ->get (Validator::class);
489486 $ validationRules = arr ($ session ->get (Session::VALIDATION_ERRORS ))->dot ();
490487
491- $ dottedExpectedErrors = arr ($ expectedErrors )-> dot ();
492- arr ( $ dottedExpectedErrors )
488+ arr ($ expectedErrors )
489+ -> dot ( )
493490 ->each (fn ($ expectedErrorValue , $ expectedErrorKey ) => Assert::assertEquals (
494- $ expectedErrorValue ,
495- $ validationRules ->get ($ expectedErrorKey )-> message ( ),
491+ expected: $ expectedErrorValue ,
492+ actual: $ validator -> getErrorMessage ( $ validationRules ->get ($ expectedErrorKey )),
496493 ));
497494
498495 return $ this ;
@@ -524,4 +521,11 @@ public function dd(): void
524521 */
525522 dd ($ this ->response ); // @mago-expect best-practices/no-debug-symbols
526523 }
524+
525+ private function assertHasContainer (): void
526+ {
527+ if ($ this ->container === null ) {
528+ Assert::fail ('This assertion requires a container. ' );
529+ }
530+ }
527531}
0 commit comments