|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace LightTest\Unit\App\Handler; |
| 6 | + |
| 7 | +use Fig\Http\Message\StatusCodeInterface; |
| 8 | +use Laminas\Diactoros\Response\HtmlResponse; |
| 9 | +use Light\App\Enum\ContactTopicEnum; |
| 10 | +use Light\App\Handler\GetContactCreateFormHandler; |
| 11 | +use LightTest\Unit\UnitTest; |
| 12 | +use Mezzio\Template\TemplateRendererInterface; |
| 13 | +use PHPUnit\Framework\MockObject\Exception; |
| 14 | +use Psr\Http\Message\ServerRequestInterface; |
| 15 | + |
| 16 | +class GetContactCreateFormHandlerTest extends UnitTest |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @throws Exception |
| 20 | + */ |
| 21 | + public function testHandleRendersTheContactFormWithTheTopics(): void |
| 22 | + { |
| 23 | + $template = $this->createMock(TemplateRendererInterface::class); |
| 24 | + $template |
| 25 | + ->expects($this->once()) |
| 26 | + ->method('render') |
| 27 | + ->with('page::contact', $this->callback( |
| 28 | + fn (array $params): bool => $params['topics'] === ContactTopicEnum::cases() |
| 29 | + && $params['contact_success'] === false |
| 30 | + && $params['query'] === [] |
| 31 | + )) |
| 32 | + ->willReturn('<form></form>'); |
| 33 | + |
| 34 | + $response = (new GetContactCreateFormHandler($template))->handle($this->createRequest([])); |
| 35 | + |
| 36 | + $this->assertInstanceOf(HtmlResponse::class, $response); |
| 37 | + $this->assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); |
| 38 | + $this->assertSame('<form></form>', (string) $response->getBody()); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @throws Exception |
| 43 | + */ |
| 44 | + public function testHandleFlagsSuccessAndKeepsExtraQueryParams(): void |
| 45 | + { |
| 46 | + $template = $this->createMock(TemplateRendererInterface::class); |
| 47 | + $template |
| 48 | + ->expects($this->once()) |
| 49 | + ->method('render') |
| 50 | + ->with('page::contact', $this->callback( |
| 51 | + fn (array $params): bool => $params['contact_success'] === true |
| 52 | + && $params['query'] === ['utm_source' => 'newsletter'] |
| 53 | + )) |
| 54 | + ->willReturn(''); |
| 55 | + |
| 56 | + (new GetContactCreateFormHandler($template))->handle( |
| 57 | + $this->createRequest(['contact' => 'sent', 'utm_source' => 'newsletter']) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param array<string, mixed> $query |
| 63 | + * @throws Exception |
| 64 | + */ |
| 65 | + private function createRequest(array $query): ServerRequestInterface |
| 66 | + { |
| 67 | + $request = $this->createStub(ServerRequestInterface::class); |
| 68 | + $request->method('getQueryParams')->willReturn($query); |
| 69 | + |
| 70 | + return $request; |
| 71 | + } |
| 72 | +} |
0 commit comments