Skip to content

Commit 55c0862

Browse files
committed
Add docblock for every view assertion
1 parent 740ca13 commit 55c0862

File tree

1 file changed

+55
-17
lines changed

1 file changed

+55
-17
lines changed

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

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,22 @@ public function assertStatus(Status $expected): self
107107
return $this;
108108
}
109109

110-
public function assertHasCookie(string $key, ?Closure $test = null): self
110+
public function assertHasCookie(string $key, ?Closure $callback = null): self
111111
{
112112
$cookies = get(CookieManager::class);
113113

114114
$cookie = $cookies->get($key);
115115

116116
Assert::assertNotNull($cookie);
117117

118-
if ($test !== null) {
119-
$test($cookie);
118+
if ($callback !== null) {
119+
$callback($cookie);
120120
}
121121

122122
return $this;
123123
}
124124

125-
public function assertHasSession(string $key, ?Closure $test = null): self
125+
public function assertHasSession(string $key, ?Closure $callback = null): self
126126
{
127127
/** @var Session $session */
128128
$session = get(Session::class);
@@ -138,14 +138,14 @@ public function assertHasSession(string $key, ?Closure $test = null): self
138138
),
139139
);
140140

141-
if ($test !== null) {
142-
$test($session, $data);
141+
if ($callback !== null) {
142+
$callback($session, $data);
143143
}
144144

145145
return $this;
146146
}
147147

148-
public function assertHasValidationError(string $key, ?Closure $test = null): self
148+
public function assertHasValidationError(string $key, ?Closure $callback = null): self
149149
{
150150
/** @var Session $session */
151151
$session = get(Session::class);
@@ -162,8 +162,8 @@ public function assertHasValidationError(string $key, ?Closure $test = null): se
162162
),
163163
);
164164

165-
if ($test !== null) {
166-
$test($validationErrors);
165+
if ($callback !== null) {
166+
$callback($validationErrors);
167167
}
168168

169169
return $this;
@@ -219,7 +219,14 @@ public function assertNotSee(string $search): self
219219
return $this;
220220
}
221221

222-
public function assertViewData(string $key, ?Closure $test = null): self
222+
/**
223+
* Asserts view data key exists and optionally assert the value
224+
*
225+
* ->assertViewData('name', fn (array $data, mixed $value) => Assert::assertEquals('Brent', $value));
226+
*
227+
* @param Closure(array<string, mixed>, mixed): (void|bool)|null $callback
228+
*/
229+
public function assertViewData(string $key, ?Closure $callback = null): self
223230
{
224231
$data = $this->body->data;
225232
$value = $data[$key];
@@ -234,13 +241,18 @@ public function assertViewData(string $key, ?Closure $test = null): self
234241
),
235242
);
236243

237-
if ($test !== null) {
238-
$test($data, $value);
244+
if ($callback !== null && $callback($data, $value) === false) {
245+
Assert::fail(sprintf('Failed validating view data for [%s]', $key));
239246
}
240247

241248
return $this;
242249
}
243250

251+
/**
252+
* Asserts view data key doesn't exist
253+
*
254+
* ->assertViewDataMissing('email');
255+
*/
244256
public function assertViewDataMissing(string $key): self
245257
{
246258
$data = $this->body->data;
@@ -254,15 +266,31 @@ public function assertViewDataMissing(string $key): self
254266
return $this;
255267
}
256268

257-
public function assertViewDataAll(Closure $test): self
269+
/**
270+
* Asserts all view data
271+
*
272+
* ->assertViewDataAll(fn (array $data) => Assert::assertEquals(['name' => 'Brent'], $data));
273+
* ->assertViewDataAll(fn (array $data) => Assert::assertEquals(['name', 'email'], array_keys($data)));
274+
*
275+
* @param Closure(array<string, mixed>): (void|bool) $callback
276+
*/
277+
public function assertViewDataAll(Closure $callback): self
258278
{
259279
$data = $this->body->data;
260280

261-
$test($data);
281+
if ($callback($data) === false) {
282+
Assert::fail('Failed validating all view data');
283+
}
262284

263285
return $this;
264286
}
265287

288+
/**
289+
* Asserts the view path
290+
*
291+
* ->assertView('./view.php');
292+
* ->assertView(__DIR__ . '/../view.php');
293+
*/
266294
public function assertView(string $view): self
267295
{
268296
if (! ($this->body instanceof View)) {
@@ -277,15 +305,25 @@ public function assertView(string $view): self
277305
return $this;
278306
}
279307

280-
public function assertViewModel(string $expected, ?Closure $test = null): self
308+
/**
309+
* Asserts the view model object
310+
*
311+
* ->assertViewModel(CustomViewModel::class);
312+
* ->assertViewModel(CustomViewModel::class, fn (CustomViewModel $viewModel) => Assert::assertEquals('Brent', $viewModel->name));
313+
*
314+
* @template T of View
315+
* @param class-string<T> $expected
316+
* @param Closure(T): (void|bool)|null $callback
317+
*/
318+
public function assertViewModel(string $expected, ?Closure $callback = null): self
281319
{
282320
Assert::assertInstanceOf(
283321
expected: $expected,
284322
actual: $this->body,
285323
);
286324

287-
if ($test !== null) {
288-
$test($this->body);
325+
if ($callback !== null && $callback($this->body) === false) {
326+
Assert::fail('Failed validating view model');
289327
}
290328

291329
return $this;

0 commit comments

Comments
 (0)