Skip to content

Commit c8e59a3

Browse files
committed
add assertHasJsonValidationError assertion
Signed-off-by: Tonko Mulder <[email protected]>
1 parent de0edd5 commit c8e59a3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Closure;
88
use Generator;
9+
use JsonException;
910
use PHPUnit\Framework\Assert;
1011
use Tempest\Http\Cookie\CookieManager;
1112
use Tempest\Http\Response;
@@ -255,6 +256,29 @@ function ($carry) use ($keys) {
255256
return $this;
256257
}
257258

259+
public function assertHasJsonValidationError(string $key, ?Closure $test = null): self
260+
{
261+
Assert::assertInstanceOf(Invalid::class, $this->response);
262+
263+
$validationErrors = array_map(function ($failingRules) use ($key) {
264+
try {
265+
$errors = json_decode($failingRules, true, 512, JSON_THROW_ON_ERROR);
266+
} catch (JsonException) {
267+
$errors = [];
268+
}
269+
270+
return arr($errors)->filter(fn (array $_error, $errorKey) => $errorKey === $key)->flatten()->first();
271+
}, $this->response->getHeader('x-validation')->values);
272+
273+
Assert::assertNotEmpty($validationErrors, message: 'no validation errors found');
274+
275+
if ($test !== null) {
276+
$test($validationErrors);
277+
}
278+
279+
return $this;
280+
}
281+
258282
public function assertHasNoJsonValidationErrors(): self
259283
{
260284
Assert::assertNotInstanceOf(Invalid::class, $this->response);

0 commit comments

Comments
 (0)