77use Closure ;
88use Generator ;
99use PHPUnit \Framework \Assert ;
10+ use Tempest \Container \Container ;
1011use Tempest \Http \Cookie \CookieManager ;
1112use Tempest \Http \Response ;
1213use Tempest \Http \Responses \Invalid ;
1314use Tempest \Http \Session \Session ;
1415use Tempest \Http \Status ;
1516use Tempest \Validation \Rule ;
17+ use Tempest \Validation \Validator ;
1618use Tempest \View \View ;
1719use Tempest \View \ViewRenderer ;
1820
@@ -23,6 +25,7 @@ final class TestResponseHelper
2325{
2426 public function __construct (
2527 private(set) Response $ response ,
28+ private(set) ?Container $ container = null ,
2629 ) {}
2730
2831 public Status $ status {
@@ -110,9 +113,9 @@ public function assertStatus(Status $expected): self
110113
111114 public function assertHasCookie (string $ key , ?Closure $ callback = null ): self
112115 {
113- $ cookies = get (CookieManager::class );
116+ $ this -> assertHasContainer ( );
114117
115- $ cookie = $ cookies ->get ($ key );
118+ $ cookie = $ this -> container -> get (CookieManager::class) ->get ($ key );
116119
117120 Assert::assertNotNull ($ cookie );
118121
@@ -125,14 +128,14 @@ public function assertHasCookie(string $key, ?Closure $callback = null): self
125128
126129 public function assertHasSession (string $ key , ?Closure $ callback = null ): self
127130 {
128- /** @var Session $session */
129- $ session = get (Session::class);
131+ $ this ->assertHasContainer ();
130132
133+ $ session = $ this ->container ->get (Session::class);
131134 $ data = $ session ->get ($ key );
132135
133136 Assert::assertNotNull (
134- $ data ,
135- sprintf (
137+ actual: $ data ,
138+ message: sprintf (
136139 'No session value was set for [%s], available session keys: %s ' ,
137140 $ key ,
138141 implode (', ' , array_keys ($ session ->data )),
@@ -148,15 +151,13 @@ public function assertHasSession(string $key, ?Closure $callback = null): self
148151
149152 public function assertHasValidationError (string $ key , ?Closure $ callback = null ): self
150153 {
151- /** @var Session $session */
152- $ session = get (Session::class);
153-
154+ $ session = $ this ->container ->get (Session::class);
154155 $ validationErrors = $ session ->get (Session::VALIDATION_ERRORS ) ?? [];
155156
156157 Assert::assertArrayHasKey (
157- $ key ,
158- $ validationErrors ,
159- sprintf (
158+ key: $ key ,
159+ array: $ validationErrors ,
160+ message: sprintf (
160161 'No validation error was set for [%s], available validation errors: %s ' ,
161162 $ key ,
162163 implode (', ' , array_keys ($ validationErrors )),
@@ -172,23 +173,16 @@ public function assertHasValidationError(string $key, ?Closure $callback = null)
172173
173174 public function assertHasNoValidationsErrors (): self
174175 {
175- /** @var Session $session */
176- $ session = get (Session::class);
177-
176+ $ session = $ this ->container ->get (Session::class);
178177 $ validationErrors = $ session ->get (Session::VALIDATION_ERRORS ) ?? [];
179178
180179 Assert::assertEmpty (
181- $ validationErrors ,
182- sprintf (
183- 'There should be no validation errors, but there were: %s ' ,
184- arr ($ validationErrors )
185- ->map (function (array $ failingRules , $ key ) {
186- $ failingRules = arr ($ failingRules )->map (fn (Rule $ rule ) => $ rule ->getTranslationVariables ())->implode (', ' );
187-
188- return $ key . ': ' . $ failingRules ;
189- })
190- ->implode (', ' ),
191- ),
180+ actual: $ validationErrors ,
181+ message: arr ($ validationErrors )
182+ ->map (fn (array $ failingRules , string $ key ) => $ key . ': ' . arr ($ failingRules )->map (fn (Rule $ rule ) => $ rule ::class)->implode (', ' ))
183+ ->implode (', ' )
184+ ->prepend ('There should be no validation errors, but there were: ' )
185+ ->toString (),
192186 );
193187
194188 return $ this ;
@@ -199,7 +193,7 @@ public function assertSee(string $search): self
199193 $ body = $ this ->body ;
200194
201195 if ($ body instanceof View) {
202- $ body = get (ViewRenderer::class)->render ($ body );
196+ $ body = $ this -> container -> get (ViewRenderer::class)->render ($ body );
203197 }
204198
205199 Assert::assertStringContainsString ($ search , $ body );
@@ -212,7 +206,7 @@ public function assertNotSee(string $search): self
212206 $ body = $ this ->body ;
213207
214208 if ($ body instanceof View) {
215- $ body = get (ViewRenderer::class)->render ($ body );
209+ $ body = $ this -> container -> get (ViewRenderer::class)->render ($ body );
216210 }
217211
218212 Assert::assertStringNotContainsString ($ search , $ body );
@@ -435,18 +429,21 @@ public function assertJsonContains(array $expected): self
435429 */
436430 public function assertHasJsonValidationErrors (array $ expectedErrors ): self
437431 {
432+ $ this ->assertHasContainer ();
433+
438434 Assert::assertInstanceOf (Invalid::class, $ this ->response );
439435 Assert::assertContains ($ this ->response ->status , [Status::BAD_REQUEST , Status::FOUND ]);
440436 Assert::assertNotNull ($ this ->response ->getHeader ('x-validation ' ));
441437
442- $ session = get (Session::class);
438+ $ session = $ this ->container ->get (Session::class);
439+ $ validator = $ this ->container ->get (Validator::class);
443440 $ validationRules = arr ($ session ->get (Session::VALIDATION_ERRORS ))->dot ();
444441
445- $ dottedExpectedErrors = arr ($ expectedErrors )-> dot ();
446- arr ( $ dottedExpectedErrors )
442+ arr ($ expectedErrors )
443+ -> dot ( )
447444 ->each (fn ($ expectedErrorValue , $ expectedErrorKey ) => Assert::assertEquals (
448- $ expectedErrorValue ,
449- $ validationRules ->get ($ expectedErrorKey )-> message ( ),
445+ expected: $ expectedErrorValue ,
446+ actual: $ validator -> getErrorMessage ( $ validationRules ->get ($ expectedErrorKey )),
450447 ));
451448
452449 return $ this ;
@@ -478,4 +475,11 @@ public function dd(): void
478475 */
479476 dd ($ this ->response ); // @mago-expect best-practices/no-debug-symbols
480477 }
478+
479+ private function assertHasContainer (): void
480+ {
481+ if ($ this ->container === null ) {
482+ Assert::fail ('This assertion requires a container. ' );
483+ }
484+ }
481485}
0 commit comments