-
-
Notifications
You must be signed in to change notification settings - Fork 655
Expand file tree
/
Copy pathUpdateEmailTemplateHandlerTest.php
More file actions
181 lines (149 loc) · 6.08 KB
/
UpdateEmailTemplateHandlerTest.php
File metadata and controls
181 lines (149 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace Tests\Unit\Services\Application\Handlers\EmailTemplate;
use HiEvents\DomainObjects\EmailTemplateDomainObject;
use HiEvents\DomainObjects\Enums\EmailTemplateType;
use HiEvents\Repository\Interfaces\EmailTemplateRepositoryInterface;
use HiEvents\Services\Application\Handlers\EmailTemplate\DTO\UpsertEmailTemplateDTO;
use HiEvents\Services\Application\Handlers\EmailTemplate\UpdateEmailTemplateHandler;
use HiEvents\Services\Domain\Email\EmailTemplateService;
use HiEvents\Services\Infrastructure\HtmlPurifier\HtmlPurifierService;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class UpdateEmailTemplateHandlerTest extends TestCase
{
use MockeryPHPUnitIntegration;
private EmailTemplateRepositoryInterface $repository;
private EmailTemplateService $emailTemplateService;
private HtmlPurifierService $purifier;
private UpdateEmailTemplateHandler $handler;
protected function setUp(): void
{
parent::setUp();
$this->repository = m::mock(EmailTemplateRepositoryInterface::class);
$this->emailTemplateService = m::mock(EmailTemplateService::class);
$this->purifier = m::mock(HtmlPurifierService::class);
$this->handler = new UpdateEmailTemplateHandler(
$this->repository,
$this->emailTemplateService,
$this->purifier,
);
}
public function test_attendee_ticket_template_keeps_ticket_url_token_when_caller_passes_no_token(): void
{
$existing = (new EmailTemplateDomainObject)
->setId(42)
->setAccountId(1)
->setTemplateType(EmailTemplateType::ATTENDEE_TICKET->value);
$this->emailTemplateService
->shouldReceive('validateTemplate')
->andReturn(['valid' => true, 'errors' => []]);
$this->repository
->shouldReceive('findFirstWhere')
->andReturn($existing);
$this->emailTemplateService
->shouldReceive('getDefaultTemplate')
->with(EmailTemplateType::ATTENDEE_TICKET)
->andReturn([
'subject' => 's',
'body' => 'b',
'cta' => ['label' => 'View Ticket', 'url_token' => 'ticket.url'],
]);
$this->purifier->shouldReceive('purify')->andReturnUsing(fn ($v) => $v);
$this->repository
->shouldReceive('updateFromArray')
->once()
->with(42, m::on(function (array $payload): bool {
return $payload['cta'] === ['label' => 'My Custom Label', 'url_token' => 'ticket.url'];
}))
->andReturn($existing);
$this->handler->handle(new UpsertEmailTemplateDTO(
account_id: 1,
template_type: EmailTemplateType::ORDER_CONFIRMATION,
subject: 'subj',
body: 'body',
id: 42,
cta: ['label' => 'My Custom Label'],
));
}
public function test_attendee_ticket_template_overrides_order_url_token_passed_in(): void
{
$existing = (new EmailTemplateDomainObject)
->setId(42)
->setAccountId(1)
->setTemplateType(EmailTemplateType::ATTENDEE_TICKET->value);
$this->emailTemplateService
->shouldReceive('validateTemplate')
->andReturn(['valid' => true, 'errors' => []]);
$this->repository
->shouldReceive('findFirstWhere')
->andReturn($existing);
$this->emailTemplateService
->shouldReceive('getDefaultTemplate')
->with(EmailTemplateType::ATTENDEE_TICKET)
->andReturn([
'subject' => 's',
'body' => 'b',
'cta' => ['label' => 'View Ticket', 'url_token' => 'ticket.url'],
]);
$this->purifier->shouldReceive('purify')->andReturnUsing(fn ($v) => $v);
$this->repository
->shouldReceive('updateFromArray')
->once()
->with(42, m::on(function (array $payload): bool {
return $payload['cta']['url_token'] === 'ticket.url';
}))
->andReturn($existing);
$this->handler->handle(new UpsertEmailTemplateDTO(
account_id: 1,
template_type: EmailTemplateType::ORDER_CONFIRMATION,
subject: 'subj',
body: 'body',
id: 42,
cta: ['label' => 'View Ticket', 'url_token' => 'order.url'],
));
}
public function test_order_confirmation_template_uses_order_url_token(): void
{
$existing = (new EmailTemplateDomainObject)
->setId(43)
->setAccountId(1)
->setTemplateType(EmailTemplateType::ORDER_CONFIRMATION->value);
$this->emailTemplateService
->shouldReceive('validateTemplate')
->andReturn(['valid' => true, 'errors' => []]);
$this->repository
->shouldReceive('findFirstWhere')
->andReturn($existing);
$this->emailTemplateService
->shouldReceive('getDefaultTemplate')
->with(EmailTemplateType::ORDER_CONFIRMATION)
->andReturn([
'subject' => 's',
'body' => 'b',
'cta' => ['label' => 'View Order & Tickets', 'url_token' => 'order.url'],
]);
$this->purifier->shouldReceive('purify')->andReturnUsing(fn ($v) => $v);
$this->repository
->shouldReceive('updateFromArray')
->once()
->with(43, m::on(function (array $payload): bool {
return $payload['cta']['url_token'] === 'order.url'
&& $payload['cta']['label'] === 'View Order';
}))
->andReturn($existing);
$this->handler->handle(new UpsertEmailTemplateDTO(
account_id: 1,
template_type: EmailTemplateType::ORDER_CONFIRMATION,
subject: 'subj',
body: 'body',
id: 43,
cta: ['label' => 'View Order'],
));
}
protected function tearDown(): void
{
m::close();
parent::tearDown();
}
}