Skip to content

Commit 81ee7d6

Browse files
innocenzibrendt
authored andcommitted
refactor: clean up http testing
1 parent 6f9a5fe commit 81ee7d6

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

src/Tempest/Framework/Testing/Http/HttpRouterTester.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Tempest\Http\Request;
1414
use Tempest\Router\RouteConfig;
1515
use Tempest\Router\Router;
16+
use Tempest\Validation\Validator;
1617

1718
use function Tempest\map;
1819

@@ -140,7 +141,8 @@ public function sendRequest(Request $request): TestResponseHelper
140141
$this->container->get(RouteConfig::class)->throwHttpExceptions = $this->throwHttpExceptions;
141142

142143
return new TestResponseHelper(
143-
$router->dispatch(map($request)->with(RequestToPsrRequestMapper::class)->do()),
144+
response: $router->dispatch(map($request)->with(RequestToPsrRequestMapper::class)->do()),
145+
container: $this->container,
144146
);
145147
}
146148

src/Tempest/Framework/Testing/Http/TestResponseHelper.php

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use JsonSerializable;
1010
use PHPUnit\Framework\Assert;
1111
use PHPUnit\Framework\ExpectationFailedException;
12+
use Tempest\Container\Container;
1213
use Tempest\Cryptography\Encryption\Encrypter;
1314
use Tempest\Http\Cookie\Cookie;
1415
use Tempest\Http\Response;
@@ -17,6 +18,7 @@
1718
use Tempest\Http\Status;
1819
use Tempest\Support\Arr;
1920
use Tempest\Validation\Rule;
21+
use Tempest\Validation\Validator;
2022
use Tempest\View\View;
2123
use 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
}

tests/Integration/Http/ValidationResponseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function test_failing_post_request(): void
9696
uri([ValidationController::class, 'updateBook'], book: 1),
9797
body: ['book' => ['title' => 1]],
9898
)
99-
->assertHasJsonValidationErrors(['title' => ['Value should be between 1 and 120']]);
99+
->assertHasJsonValidationErrors(['title' => ['Value must be between 1 and 120']]);
100100

101101
$this->assertSame('Timeline Taxi', Book::find(id: 1)->first()->title);
102102
}

0 commit comments

Comments
 (0)