Skip to content

Commit 428df47

Browse files
committed
refactor: clean up http testing
1 parent f8207d6 commit 428df47

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
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: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
use Closure;
88
use Generator;
99
use PHPUnit\Framework\Assert;
10+
use Tempest\Container\Container;
1011
use Tempest\Http\Cookie\CookieManager;
1112
use Tempest\Http\Response;
1213
use Tempest\Http\Responses\Invalid;
1314
use Tempest\Http\Session\Session;
1415
use Tempest\Http\Status;
1516
use Tempest\Validation\Rule;
17+
use Tempest\Validation\Validator;
1618
use Tempest\View\View;
1719
use 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
}

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)