Skip to content

Commit 5cc0b94

Browse files
author
olaru
committed
Add tests
1 parent b5dcd29 commit 5cc0b94

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LightTest\Unit\App\Enum;
6+
7+
use Light\App\Enum\ContactTopicEnum;
8+
use LightTest\Unit\UnitTest;
9+
10+
use function array_map;
11+
12+
class ContactTopicEnumTest extends UnitTest
13+
{
14+
public function testCasesKeepTheValuesPostedByTheForm(): void
15+
{
16+
$this->assertSame(
17+
['migration', 'project', 'oss', 'other'],
18+
array_map(fn (ContactTopicEnum $topic): string => $topic->value, ContactTopicEnum::cases())
19+
);
20+
}
21+
22+
public function testLabel(): void
23+
{
24+
$this->assertSame('Migration', ContactTopicEnum::Migration->label());
25+
$this->assertSame('Project work', ContactTopicEnum::Project->label());
26+
$this->assertSame('Open source', ContactTopicEnum::Oss->label());
27+
$this->assertSame('Something else', ContactTopicEnum::Other->label());
28+
}
29+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)