|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2014 Roave, LLC. |
|
4
|
|
|
* All rights reserved. |
|
5
|
|
|
* |
|
6
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
7
|
|
|
* modification, are permitted provided that the following conditions |
|
8
|
|
|
* are met: |
|
9
|
|
|
* |
|
10
|
|
|
* * Redistributions of source code must retain the above copyright |
|
11
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
12
|
|
|
* |
|
13
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
|
14
|
|
|
* notice, this list of conditions and the following disclaimer in |
|
15
|
|
|
* the documentation and/or other materials provided with the |
|
16
|
|
|
* distribution. |
|
17
|
|
|
* |
|
18
|
|
|
* * Neither the names of the copyright holders nor the names of the |
|
19
|
|
|
* contributors may be used to endorse or promote products derived |
|
20
|
|
|
* from this software without specific prior written permission. |
|
21
|
|
|
* |
|
22
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
23
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
24
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
25
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
26
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
27
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
28
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
29
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
30
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
31
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
32
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
33
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
34
|
|
|
* |
|
35
|
|
|
* @author Antoine Hedgecock |
|
36
|
|
|
* |
|
37
|
|
|
* @copyright 2014 Roave, LLC |
|
38
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
namespace EmailTemplatesTest\Service; |
|
42
|
|
|
|
|
43
|
|
|
use DateTime; |
|
44
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
45
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
46
|
|
|
use PHPUnit_Framework_TestCase; |
|
47
|
|
|
use Roave\EmailTemplates\Entity\TemplateEntity; |
|
48
|
|
|
use Roave\EmailTemplates\Hydrator\TemplateHydrator; |
|
49
|
|
|
use Roave\EmailTemplates\InputFilter\TemplateInputFilter; |
|
50
|
|
|
use Roave\EmailTemplates\Options\TemplateServiceOptions; |
|
51
|
|
|
use Roave\EmailTemplates\Repository\TemplateRepositoryInterface; |
|
52
|
|
|
use Roave\EmailTemplates\Service\Exception\FailedDataValidationException; |
|
53
|
|
|
use Roave\EmailTemplates\Service\Template\Engine\EngineInterface; |
|
54
|
|
|
use Roave\EmailTemplates\Service\Template\EnginePluginManager; |
|
55
|
|
|
use Roave\EmailTemplates\Service\TemplateService; |
|
56
|
|
|
use Zend\EventManager\EventManagerInterface; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Class TemplateServiceTest |
|
60
|
|
|
* |
|
61
|
|
|
* @coversDefaultClass \Roave\EmailTemplates\Service\TemplateService |
|
62
|
|
|
* @covers ::<!public> |
|
63
|
|
|
* |
|
64
|
|
|
* @group service |
|
65
|
|
|
*/ |
|
66
|
|
|
class TemplateServiceTest extends PHPUnit_Framework_TestCase |
|
67
|
|
|
{ |
|
68
|
|
|
/** |
|
69
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $objectManager; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $repository; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $inputFilter; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $hydrator; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject |
|
90
|
|
|
*/ |
|
91
|
|
|
protected $engineManager; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject |
|
95
|
|
|
*/ |
|
96
|
|
|
protected $eventManager; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @var TemplateServiceOptions |
|
100
|
|
|
*/ |
|
101
|
|
|
protected $options; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @var TemplateService |
|
105
|
|
|
*/ |
|
106
|
|
|
protected $templateService; |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @covers ::__construct |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function setUp() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->objectManager = $this->getMock(ObjectManager::class); |
|
114
|
|
|
$this->repository = $this->getMock(TemplateRepositoryInterface::class); |
|
115
|
|
|
$this->inputFilter = $this->getMock(TemplateInputFilter::class); |
|
116
|
|
|
$this->hydrator = $this->getMock(TemplateHydrator::class); |
|
117
|
|
|
$this->engineManager = $this->getMock(EnginePluginManager::class); |
|
118
|
|
|
$this->eventManager = $this->getMock(EventManagerInterface::class); |
|
119
|
|
|
$this->options = new TemplateServiceOptions(); |
|
120
|
|
|
|
|
121
|
|
|
$this->templateService = new TemplateService( |
|
122
|
|
|
$this->objectManager, |
|
123
|
|
|
$this->repository, |
|
124
|
|
|
$this->inputFilter, |
|
125
|
|
|
$this->hydrator, |
|
126
|
|
|
$this->engineManager, |
|
127
|
|
|
$this->options |
|
128
|
|
|
); |
|
129
|
|
|
|
|
130
|
|
|
$this->templateService->setEventManager($this->eventManager); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @covers ::render |
|
135
|
|
|
*/ |
|
136
|
|
|
public function testRender() |
|
137
|
|
|
{ |
|
138
|
|
|
$locale = 'sv_SE'; |
|
139
|
|
|
$templateId = 'test:template:id'; |
|
140
|
|
|
$parameters = ['company' => 'Roave']; |
|
141
|
|
|
|
|
142
|
|
|
$template = new TemplateEntity(); |
|
143
|
|
|
$template->setSubject('subject'); |
|
144
|
|
|
$template->setTextBody('text'); |
|
145
|
|
|
$template->setHtmlBody('html'); |
|
146
|
|
|
|
|
147
|
|
|
$engine = $this->getMock(EngineInterface::class); |
|
148
|
|
|
|
|
149
|
|
|
// First run is the subject |
|
150
|
|
|
$engine |
|
151
|
|
|
->expects($this->at(0)) |
|
152
|
|
|
->method('render') |
|
153
|
|
|
->with($template->getSubject(), $parameters) |
|
154
|
|
|
->will($this->returnValue($template->getSubject())); |
|
155
|
|
|
|
|
156
|
|
|
// Second run is the html body |
|
157
|
|
|
$engine |
|
158
|
|
|
->expects($this->at(1)) |
|
159
|
|
|
->method('render') |
|
160
|
|
|
->with($template->getHtmlBody(), $parameters) |
|
161
|
|
|
->will($this->returnValue($template->getHtmlBody())); |
|
162
|
|
|
|
|
163
|
|
|
// Third run is the text body |
|
164
|
|
|
$engine |
|
165
|
|
|
->expects($this->at(2)) |
|
166
|
|
|
->method('render') |
|
167
|
|
|
->with($template->getTextBody(), $parameters) |
|
168
|
|
|
->will($this->returnValue($template->getTextBody())); |
|
169
|
|
|
|
|
170
|
|
|
$this->repository |
|
171
|
|
|
->expects($this->once()) |
|
172
|
|
|
->method('getByIdAndLocale') |
|
173
|
|
|
->with($templateId, $locale) |
|
174
|
|
|
->will($this->returnValue($template)); |
|
175
|
|
|
|
|
176
|
|
|
$this->eventManager |
|
177
|
|
|
->expects($this->once()) |
|
178
|
|
|
->method('trigger') |
|
179
|
|
|
->with( |
|
180
|
|
|
TemplateService::EVENT_RENDER, |
|
181
|
|
|
$this->templateService, |
|
182
|
|
|
[ |
|
183
|
|
|
'template' => $template, |
|
184
|
|
|
'parameters' => $parameters |
|
185
|
|
|
] |
|
186
|
|
|
); |
|
187
|
|
|
|
|
188
|
|
|
$this->engineManager |
|
189
|
|
|
->expects($this->once()) |
|
190
|
|
|
->method('get') |
|
191
|
|
|
->will($this->returnValue($engine)); |
|
192
|
|
|
|
|
193
|
|
|
list ($subject, $html, $text) = $this->templateService->render($templateId, $locale, $parameters); |
|
194
|
|
|
|
|
195
|
|
|
$this->assertEquals($template->getSubject(), $subject); |
|
196
|
|
|
$this->assertEquals($template->getHtmlBody(), $html); |
|
197
|
|
|
$this->assertEquals($template->getTextBody(), $text); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @covers ::render |
|
202
|
|
|
* @covers ::create |
|
203
|
|
|
*/ |
|
204
|
|
|
public function testRenderWithMissingTemplate() |
|
205
|
|
|
{ |
|
206
|
|
|
$locale = 'sv_SE'; |
|
207
|
|
|
$templateId = 'test:template:id'; |
|
208
|
|
|
$parameters = ['company' => 'Roave']; |
|
209
|
|
|
|
|
210
|
|
|
$template = new TemplateEntity(); |
|
211
|
|
|
$template->setSubject($this->options->getDefaultSubject()); |
|
212
|
|
|
$template->setTextBody(sprintf($this->options->getDefaultBody(), $templateId, $locale)); |
|
213
|
|
|
$template->setHtmlBody(sprintf($this->options->getDefaultBody(), $templateId, $locale)); |
|
214
|
|
|
|
|
215
|
|
|
$engine = $this->getMock(EngineInterface::class); |
|
216
|
|
|
|
|
217
|
|
|
// First run is the subject |
|
218
|
|
|
$engine |
|
219
|
|
|
->expects($this->at(0)) |
|
220
|
|
|
->method('render') |
|
221
|
|
|
->with($template->getSubject(), $parameters) |
|
222
|
|
|
->will($this->returnValue($template->getSubject())); |
|
223
|
|
|
|
|
224
|
|
|
// Second run is the html body |
|
225
|
|
|
$engine |
|
226
|
|
|
->expects($this->at(1)) |
|
227
|
|
|
->method('render') |
|
228
|
|
|
->with($template->getHtmlBody(), $parameters) |
|
229
|
|
|
->will($this->returnValue($template->getHtmlBody())); |
|
230
|
|
|
|
|
231
|
|
|
// Third run is the text body |
|
232
|
|
|
$engine |
|
233
|
|
|
->expects($this->at(2)) |
|
234
|
|
|
->method('render') |
|
235
|
|
|
->with($template->getTextBody(), $parameters) |
|
236
|
|
|
->will($this->returnValue($template->getTextBody())); |
|
237
|
|
|
|
|
238
|
|
|
$this->repository |
|
239
|
|
|
->expects($this->once()) |
|
240
|
|
|
->method('getByIdAndLocale') |
|
241
|
|
|
->with($templateId, $locale) |
|
242
|
|
|
->will($this->returnValue(null)); |
|
243
|
|
|
|
|
244
|
|
|
$this->eventManager |
|
245
|
|
|
->expects($this->at(0)) |
|
246
|
|
|
->method('trigger') |
|
247
|
|
|
->with(TemplateService::EVENT_CREATE, $this->templateService, $this->callback(function ($parameters) { |
|
248
|
|
|
return ($parameters['template'] instanceof TemplateEntity); |
|
249
|
|
|
})); |
|
250
|
|
|
|
|
251
|
|
|
$this->eventManager |
|
252
|
|
|
->expects($this->at(1)) |
|
253
|
|
|
->method('trigger') |
|
254
|
|
|
->with(TemplateService::EVENT_RENDER); |
|
255
|
|
|
|
|
256
|
|
|
$this->engineManager |
|
257
|
|
|
->expects($this->once()) |
|
258
|
|
|
->method('get') |
|
259
|
|
|
->will($this->returnValue($engine)); |
|
260
|
|
|
|
|
261
|
|
|
list ($subject, $html, $text) = $this->templateService->render($templateId, $locale, $parameters); |
|
262
|
|
|
|
|
263
|
|
|
$this->assertEquals($template->getSubject(), $subject); |
|
264
|
|
|
$this->assertEquals($template->getHtmlBody(), $html); |
|
265
|
|
|
$this->assertEquals($template->getTextBody(), $text); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @covers ::update |
|
270
|
|
|
*/ |
|
271
|
|
|
public function testUpdateWithIncorrectData() |
|
272
|
|
|
{ |
|
273
|
|
|
$this->setExpectedException(FailedDataValidationException::class); |
|
274
|
|
|
|
|
275
|
|
|
$this->hydrator |
|
276
|
|
|
->expects($this->once()) |
|
277
|
|
|
->method('extract') |
|
278
|
|
|
->will($this->returnValue([])); |
|
279
|
|
|
|
|
280
|
|
|
$this->inputFilter |
|
281
|
|
|
->expects($this->once()) |
|
282
|
|
|
->method('getMessages') |
|
283
|
|
|
->will($this->returnValue([])); |
|
284
|
|
|
|
|
285
|
|
|
$this->templateService->update([], new TemplateEntity()); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @covers ::update |
|
290
|
|
|
*/ |
|
291
|
|
|
public function testUpdate() |
|
292
|
|
|
{ |
|
293
|
|
|
$data = ['foo' => 'bar']; |
|
294
|
|
|
$template = $this->getMock(TemplateEntity::class); |
|
295
|
|
|
$template |
|
296
|
|
|
->expects($this->once()) |
|
297
|
|
|
->method('setUpdatedAt') |
|
298
|
|
|
->with($this->isInstanceOf(DateTime::class)); |
|
299
|
|
|
|
|
300
|
|
|
$this->hydrator |
|
301
|
|
|
->expects($this->once()) |
|
302
|
|
|
->method('extract') |
|
303
|
|
|
->will($this->returnValue([])); |
|
304
|
|
|
|
|
305
|
|
|
$this->inputFilter |
|
306
|
|
|
->expects($this->once()) |
|
307
|
|
|
->method('isValid') |
|
308
|
|
|
->will($this->returnValue(true)); |
|
309
|
|
|
|
|
310
|
|
|
$this->inputFilter |
|
311
|
|
|
->expects($this->once()) |
|
312
|
|
|
->method('getValues') |
|
313
|
|
|
->will($this->returnValue($data)); |
|
314
|
|
|
|
|
315
|
|
|
$this->hydrator |
|
316
|
|
|
->expects($this->once()) |
|
317
|
|
|
->method('hydrate') |
|
318
|
|
|
->with($data, $template); |
|
319
|
|
|
|
|
320
|
|
|
$this->objectManager |
|
321
|
|
|
->expects($this->once()) |
|
322
|
|
|
->method('flush'); |
|
323
|
|
|
|
|
324
|
|
|
$this->templateService->update($data, $template); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* @covers ::render |
|
329
|
|
|
*/ |
|
330
|
|
|
public function testRenderWillMergePredefinedParametersWithParameters() |
|
331
|
|
|
{ |
|
332
|
|
|
$locale = 'sv_SE'; |
|
333
|
|
|
$templateId = 'test:template:id'; |
|
334
|
|
|
$parameters = ['company' => 'Roave']; |
|
335
|
|
|
$predefinedParameters = ['company' => 'ShouldNotBeThisOne', 'anotherOneThatShouldBeIncluded' => 'value']; |
|
336
|
|
|
$expectedParameters = ['company' => 'Roave', 'anotherOneThatShouldBeIncluded' => 'value']; |
|
337
|
|
|
|
|
338
|
|
|
$this->options->setPredefinedParams($predefinedParameters); |
|
339
|
|
|
|
|
340
|
|
|
$template = new TemplateEntity(); |
|
341
|
|
|
$template->setSubject($this->options->getDefaultSubject()); |
|
342
|
|
|
$template->setTextBody(sprintf($this->options->getDefaultBody(), $templateId, $locale)); |
|
343
|
|
|
$template->setHtmlBody(sprintf($this->options->getDefaultBody(), $templateId, $locale)); |
|
344
|
|
|
|
|
345
|
|
|
$engine = $this->getMock(EngineInterface::class); |
|
346
|
|
|
|
|
347
|
|
|
// First run is the subject |
|
348
|
|
|
$engine |
|
349
|
|
|
->expects($this->at(0)) |
|
350
|
|
|
->method('render') |
|
351
|
|
|
->with($template->getSubject(), $expectedParameters) |
|
352
|
|
|
->will($this->returnValue($template->getSubject())); |
|
353
|
|
|
|
|
354
|
|
|
// Second run is the html body |
|
355
|
|
|
$engine |
|
356
|
|
|
->expects($this->at(1)) |
|
357
|
|
|
->method('render') |
|
358
|
|
|
->with($template->getHtmlBody(), $expectedParameters) |
|
359
|
|
|
->will($this->returnValue($template->getHtmlBody())); |
|
360
|
|
|
|
|
361
|
|
|
// Third run is the text body |
|
362
|
|
|
$engine |
|
363
|
|
|
->expects($this->at(2)) |
|
364
|
|
|
->method('render') |
|
365
|
|
|
->with($template->getTextBody(), $expectedParameters) |
|
366
|
|
|
->will($this->returnValue($template->getTextBody())); |
|
367
|
|
|
|
|
368
|
|
|
$this->repository |
|
369
|
|
|
->expects($this->once()) |
|
370
|
|
|
->method('getByIdAndLocale') |
|
371
|
|
|
->with($templateId, $locale) |
|
372
|
|
|
->will($this->returnValue($template)); |
|
373
|
|
|
|
|
374
|
|
|
$this->engineManager |
|
375
|
|
|
->expects($this->once()) |
|
376
|
|
|
->method('get') |
|
377
|
|
|
->will($this->returnValue($engine)); |
|
378
|
|
|
|
|
379
|
|
|
list ($subject, $html, $text) = $this->templateService->render($templateId, $locale, $parameters); |
|
380
|
|
|
|
|
381
|
|
|
$this->assertEquals($template->getSubject(), $subject); |
|
382
|
|
|
$this->assertEquals($template->getHtmlBody(), $html); |
|
383
|
|
|
$this->assertEquals($template->getTextBody(), $text); |
|
384
|
|
|
} |
|
385
|
|
|
} |
|
386
|
|
|
|