|
9 | 9 | use PHPUnit\Framework\Assert; |
10 | 10 | use Tempest\Http\Cookie\CookieManager; |
11 | 11 | use Tempest\Http\Response; |
| 12 | +use Tempest\Http\Responses\Invalid; |
12 | 13 | use Tempest\Http\Session\Session; |
13 | 14 | use Tempest\Http\Status; |
14 | 15 | use Tempest\Validation\Rule; |
@@ -329,9 +330,152 @@ public function assertViewModel(string $expected, ?Closure $callback = null): se |
329 | 330 | return $this; |
330 | 331 | } |
331 | 332 |
|
| 333 | + /** |
| 334 | + * Assert the response body is an exact match to the given array. |
| 335 | + * |
| 336 | + * The keys can also be specified using dot notation. |
| 337 | + * |
| 338 | + * ### Example |
| 339 | + * ``` |
| 340 | + * // build the expected array with dot notation |
| 341 | + * $this->http->get(uri([BookController::class, 'index'])) |
| 342 | + * ->assertJson([ |
| 343 | + * 'id' => 1, |
| 344 | + * 'title' => 'Timeline Taxi', |
| 345 | + * 'author.name' => 'Brent', |
| 346 | + * ]); |
| 347 | + * |
| 348 | + * // build the expected array with a normal array |
| 349 | + * $this->http->get(uri([BookController::class, 'index'])) |
| 350 | + * ->assertJson([ |
| 351 | + * 'id' => 1, |
| 352 | + * 'title' => 'Timeline Taxi', |
| 353 | + * 'author' => [ |
| 354 | + * 'name' => 'Brent', |
| 355 | + * ], |
| 356 | + * ]); |
| 357 | + * ``` |
| 358 | + * |
| 359 | + * @param array<string, mixed> $expected |
| 360 | + */ |
| 361 | + public function assertJson(array $expected): self |
| 362 | + { |
| 363 | + Assert::assertEquals( |
| 364 | + expected: arr($expected)->undot()->toArray(), |
| 365 | + actual: $this->response->body, |
| 366 | + ); |
| 367 | + |
| 368 | + return $this; |
| 369 | + } |
| 370 | + |
| 371 | + /** |
| 372 | + * Asserts the response contains the given keys. |
| 373 | + * |
| 374 | + * The keys can also be specified using dot notation. |
| 375 | + * |
| 376 | + * ### Example |
| 377 | + * ``` |
| 378 | + * $this->http->get(uri([BookController::class, 'index'])) |
| 379 | + * ->assertJsonHasKeys('id', 'title', 'author.name'); |
| 380 | + * ``` |
| 381 | + */ |
| 382 | + public function assertJsonHasKeys(string ...$keys): self |
| 383 | + { |
| 384 | + foreach ($keys as $key) { |
| 385 | + Assert::assertArrayHasKey($key, arr($this->response->body)->dot()); |
| 386 | + } |
| 387 | + |
| 388 | + return $this; |
| 389 | + } |
| 390 | + |
| 391 | + /** |
| 392 | + * Asserts the response contains the given keys and values. |
| 393 | + * |
| 394 | + * The keys can also be specified using dot notation. |
| 395 | + * |
| 396 | + * ### Example |
| 397 | + * ``` |
| 398 | + * $this->http->get(uri([BookController::class, 'index'])) |
| 399 | + * ->assertJsonContains([ |
| 400 | + * 'id' => 1, |
| 401 | + * 'title' => 'Timeline Taxi', |
| 402 | + * ]) |
| 403 | + * ->assertJsonContains(['author' => ['name' => 'Brent']]) |
| 404 | + * ->assertJsonContains(['author.name' => 'Brent']); |
| 405 | + * ``` |
| 406 | + * |
| 407 | + * @template TKey of array-key |
| 408 | + * @template TValue |
| 409 | + * |
| 410 | + * @param array<TKey, TValue> $expected |
| 411 | + */ |
| 412 | + public function assertJsonContains(array $expected): self |
| 413 | + { |
| 414 | + foreach (arr($expected)->undot() as $key => $value) { |
| 415 | + Assert::assertEquals($this->response->body[$key], $value); |
| 416 | + } |
| 417 | + |
| 418 | + return $this; |
| 419 | + } |
| 420 | + |
| 421 | + /** |
| 422 | + * Asserts the response contains the given JSON validation errors. |
| 423 | + * |
| 424 | + * The keys can also be specified using dot notation. |
| 425 | + * |
| 426 | + * ### Example |
| 427 | + * ``` |
| 428 | + * $this->http->get(uri([BookController::class, 'index'])) |
| 429 | + * ->assertJsonValidationErrors([ |
| 430 | + * 'title' => 'The title field is required.', |
| 431 | + * ]); |
| 432 | + * ``` |
| 433 | + * |
| 434 | + * @param array<string, string|string[]> $expectedErrors |
| 435 | + */ |
| 436 | + public function assertHasJsonValidationErrors(array $expectedErrors): self |
| 437 | + { |
| 438 | + Assert::assertInstanceOf(Invalid::class, $this->response); |
| 439 | + Assert::assertContains($this->response->status, [Status::BAD_REQUEST, Status::FOUND]); |
| 440 | + Assert::assertNotNull($this->response->getHeader('x-validation')); |
| 441 | + |
| 442 | + $session = get(Session::class); |
| 443 | + $validationRules = arr($session->get(Session::VALIDATION_ERRORS))->dot(); |
| 444 | + |
| 445 | + $dottedExpectedErrors = arr($expectedErrors)->dot(); |
| 446 | + arr($dottedExpectedErrors) |
| 447 | + ->each(fn ($expectedErrorValue, $expectedErrorKey) => Assert::assertEquals( |
| 448 | + $expectedErrorValue, |
| 449 | + $validationRules->get($expectedErrorKey)->message(), |
| 450 | + )); |
| 451 | + |
| 452 | + return $this; |
| 453 | + } |
| 454 | + |
| 455 | + /** |
| 456 | + * Asserts the response does not contain any JSON validation errors. |
| 457 | + * |
| 458 | + * ### Example |
| 459 | + * ``` |
| 460 | + * $this->http->get(uri([BookController::class, 'index'])) |
| 461 | + * ->assertHasNoJsonValidationErrors(); |
| 462 | + * ``` |
| 463 | + */ |
| 464 | + public function assertHasNoJsonValidationErrors(): self |
| 465 | + { |
| 466 | + Assert::assertNotContains($this->response->status, [Status::BAD_REQUEST, Status::FOUND]); |
| 467 | + Assert::assertNotInstanceOf(Invalid::class, $this->response); |
| 468 | + Assert::assertNull($this->response->getHeader('x-validation')); |
| 469 | + |
| 470 | + return $this; |
| 471 | + } |
| 472 | + |
332 | 473 | public function dd(): void |
333 | 474 | { |
334 | | - // @phpstan-ignore disallowed.function |
| 475 | + /** |
| 476 | + * @noinspection ForgottenDebugOutputInspection |
| 477 | + * @phpstan-ignore disallowed.function |
| 478 | + */ |
335 | 479 | dd($this->response); // @mago-expect best-practices/no-debug-symbols |
336 | 480 | } |
337 | 481 | } |
0 commit comments