1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MtMail - e-mail module for Zend Framework 2 |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/mtymek/MtMail |
6
|
|
|
* @copyright Copyright (c) 2013-2014 Mateusz Tymek |
7
|
|
|
* @license BSD 2-Clause |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace MtMailTest\Service; |
11
|
|
|
|
12
|
|
|
use MtMail\Event\ComposerEvent; |
13
|
|
|
use MtMail\Service\Composer; |
14
|
|
|
use MtMailTest\Test\HtmlTemplate; |
15
|
|
|
use MtMailTest\Test\TextTemplate; |
16
|
|
|
use Prophecy\Argument; |
17
|
|
|
use Zend\EventManager\EventManager; |
18
|
|
|
use Zend\View\Model\ViewModel; |
19
|
|
|
|
20
|
|
|
class ComposerTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Composer |
24
|
|
|
*/ |
25
|
|
|
protected $service; |
26
|
|
|
|
27
|
|
|
public function setUp() |
28
|
|
|
{ |
29
|
|
|
$renderer = $this->getMock('MtMail\Renderer\RendererInterface'); |
30
|
|
|
$this->service = new Composer($renderer); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testRendererIsMutable() |
34
|
|
|
{ |
35
|
|
|
$renderer = $this->getMock('MtMail\Renderer\RendererInterface'); |
36
|
|
|
$this->assertEquals($renderer, $this->service->setRenderer($renderer)->getRenderer()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testComposeRendersViewModelAndAssignsResultToMailBody() |
40
|
|
|
{ |
41
|
|
|
$template = new TextTemplate(); |
42
|
|
|
|
43
|
|
|
$renderer = $this->getMock('MtMail\Renderer\RendererInterface', ['render']); |
44
|
|
|
$renderer->expects($this->once())->method('render')->with($this->isInstanceOf('Zend\View\Model\ModelInterface')) |
45
|
|
|
->will($this->returnValue('MAIL_BODY')); |
46
|
|
|
|
47
|
|
|
$service = new Composer($renderer); |
48
|
|
|
$message = $service->compose([], $template, new ViewModel()); |
49
|
|
|
$this->assertEquals('MAIL_BODY', $message->getBody()->getPartContent(0)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testComposeRendersViewModelAndAssignsSubjectIfProvidedByViewModel() |
53
|
|
|
{ |
54
|
|
|
$template = new HtmlTemplate(); |
55
|
|
|
|
56
|
|
|
$renderer = $this->getMock('MtMail\Renderer\RendererInterface', ['render']); |
57
|
|
|
$renderer->expects($this->once())->method('render')->with($this->isInstanceOf('Zend\View\Model\ModelInterface')) |
58
|
|
|
->will($this->returnValue('MAIL_BODY')); |
59
|
|
|
|
60
|
|
|
$service = new Composer($renderer); |
61
|
|
|
$message = $service->compose(['subject' => 'MAIL_SUBJECT'], $template, new ViewModel()); |
62
|
|
|
$this->assertEquals('MAIL_BODY', $message->getBody()->getPartContent(0)); |
63
|
|
|
$this->assertEquals('MAIL_SUBJECT', $message->getSubject()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testServiceIsEventManagerAware() |
67
|
|
|
{ |
68
|
|
|
$em = new EventManager(); |
69
|
|
|
$this->service->setEventManager($em); |
70
|
|
|
$this->assertEquals($em, $this->service->getEventManager()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testComposeTriggersEvents() |
74
|
|
|
{ |
75
|
|
|
$renderer = $this->getMock('MtMail\Renderer\RendererInterface', ['render']); |
76
|
|
|
$renderer->expects($this->once())->method('render')->with($this->isInstanceOf('Zend\View\Model\ModelInterface')) |
77
|
|
|
->will($this->returnValue('MAIL_BODY')); |
78
|
|
|
|
79
|
|
|
$em = $this->getMock('Zend\EventManager\EventManager', ['trigger']); |
80
|
|
|
$em->expects($this->at(0))->method('trigger') |
81
|
|
|
->with(ComposerEvent::EVENT_COMPOSE_PRE, $this->isInstanceOf('MtMail\Event\ComposerEvent')); |
82
|
|
|
$em->expects($this->at(1))->method('trigger') |
83
|
|
|
->with(ComposerEvent::EVENT_HEADERS_PRE, $this->isInstanceOf('MtMail\Event\ComposerEvent')); |
84
|
|
|
$em->expects($this->at(2))->method('trigger') |
85
|
|
|
->with(ComposerEvent::EVENT_HEADERS_POST, $this->isInstanceOf('MtMail\Event\ComposerEvent')); |
86
|
|
|
$em->expects($this->at(3))->method('trigger') |
87
|
|
|
->with(ComposerEvent::EVENT_HTML_BODY_PRE, $this->isInstanceOf('MtMail\Event\ComposerEvent')); |
88
|
|
|
$em->expects($this->at(4))->method('trigger') |
89
|
|
|
->with(ComposerEvent::EVENT_HTML_BODY_POST, $this->isInstanceOf('MtMail\Event\ComposerEvent')); |
90
|
|
|
$em->expects($this->at(5))->method('trigger') |
91
|
|
|
->with(ComposerEvent::EVENT_COMPOSE_POST, $this->isInstanceOf('MtMail\Event\ComposerEvent')); |
92
|
|
|
|
93
|
|
|
$service = new Composer($renderer); |
94
|
|
|
$service->setEventManager($em); |
95
|
|
|
$template = new HtmlTemplate(); |
96
|
|
|
$service->compose([], $template, new ViewModel()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testHtmlBodyPreEventAllowsReplacingViewModel() |
100
|
|
|
{ |
101
|
|
|
$replacement = new ViewModel(); |
102
|
|
|
$replacement->setTemplate('some_template.phtml'); |
103
|
|
|
$renderer = $this->getMock('MtMail\Renderer\RendererInterface', ['render']); |
104
|
|
|
$renderer->expects($this->once())->method('render')->with($this->equalTo($replacement)) |
105
|
|
|
->will($this->returnValue('MAIL_BODY')); |
106
|
|
|
|
107
|
|
|
$service = new Composer($renderer); |
108
|
|
|
$template = new HtmlTemplate(); |
109
|
|
|
|
110
|
|
|
$service->getEventManager()->attach(ComposerEvent::EVENT_HTML_BODY_PRE, function ($event) use ($replacement) { |
111
|
|
|
$event->setViewModel($replacement); |
112
|
|
|
}); |
113
|
|
|
|
114
|
|
|
$service->compose([], $template, new ViewModel()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testTextTemplateHasCorrectCharset() |
118
|
|
|
{ |
119
|
|
|
$viewModel = new ViewModel(); |
120
|
|
|
$template = new TextTemplate(); |
121
|
|
|
$renderer = $this->prophesize('MtMail\Renderer\RendererInterface'); |
122
|
|
|
$renderer->render(Argument::type('Zend\View\Model\ViewModel'))->willReturn('BODY'); |
123
|
|
|
$service = new Composer($renderer->reveal()); |
124
|
|
|
|
125
|
|
|
$message = $service->compose([], $template, $viewModel); |
126
|
|
|
|
127
|
|
|
$parts = $message->getBody()->getParts(); |
128
|
|
|
$textPart = $parts[0]; |
129
|
|
|
$this->assertTrue(isset($textPart->charset)); |
130
|
|
|
$this->assertSame($message->getHeaders()->getEncoding(), $textPart->charset); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|