1 | <?php |
||
2 | |||
3 | |||
4 | namespace Bummzack\SwiftMailer\EmogrifyPlugin\Tests; |
||
5 | |||
6 | use Bummzack\SwiftMailer\EmogrifyPlugin\EmogrifierPlugin; |
||
7 | use Pelago\Emogrifier; |
||
8 | |||
9 | class EmogrifierPluginTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | /** |
||
12 | */ |
||
13 | public function testGetterSetter() |
||
14 | { |
||
15 | $plugin = new EmogrifierPlugin(); |
||
16 | |||
17 | // The default is an emogrifier instance |
||
18 | $this->assertInstanceOf(Emogrifier::class, $plugin->getEmogrifier()); |
||
19 | |||
20 | $newInstance = new Emogrifier(); |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
![]() |
|||
21 | $plugin->setEmogrifier($newInstance); |
||
22 | $this->assertEquals($newInstance, $plugin->getEmogrifier()); |
||
23 | |||
24 | $plugin = new EmogrifierPlugin($newInstance); |
||
25 | $this->assertEquals($newInstance, $plugin->getEmogrifier()); |
||
26 | } |
||
27 | |||
28 | public function testBodyOnly() |
||
29 | { |
||
30 | $html = '<style>.test { color: red; }</style><p class="test">Hello World</p>'; |
||
31 | |||
32 | $message = $this->createMockedMessage($html); |
||
33 | $event = $this->createMockedSendEvent($message); |
||
34 | |||
35 | $plugin = new EmogrifierPlugin(); |
||
36 | $emogrifier = $this->getMockBuilder(Emogrifier::class) |
||
37 | ->disableOriginalConstructor() |
||
38 | ->getMock(); |
||
39 | $emogrifier->expects($this->once()) |
||
40 | ->method('setHtml') |
||
41 | ->with( |
||
42 | $this->equalTo($html) |
||
43 | ); |
||
44 | $emogrifier->expects($this->once()) |
||
45 | ->method('emogrify'); |
||
46 | |||
47 | $plugin->setEmogrifier($emogrifier); |
||
48 | $plugin->beforeSendPerformed($event); |
||
49 | } |
||
50 | |||
51 | public function testWithMessagePart() |
||
52 | { |
||
53 | $htmlBody = '<p>MessageBody</p>'; |
||
54 | $htmlPart = '<p>MessagePart</p>'; |
||
55 | |||
56 | $part1 = $this->createMockedMessagePart($htmlPart); |
||
57 | $part2 = $this->createMockedMessagePart('Plain text', 'text/plain'); |
||
58 | |||
59 | $message = $this->createMockedMessage($htmlBody, 'text/html', [$part1, $part2]); |
||
60 | $event = $this->createMockedSendEvent($message); |
||
61 | |||
62 | $plugin = new EmogrifierPlugin(); |
||
63 | |||
64 | $emogrifier = $this->getMockBuilder(Emogrifier::class) |
||
65 | ->disableOriginalConstructor() |
||
66 | ->getMock(); |
||
67 | |||
68 | $emogrifier->expects($this->any()) |
||
69 | ->method('setHtml') |
||
70 | ->withConsecutive( |
||
71 | $this->equalTo($htmlBody), |
||
72 | $this->equalTo($htmlPart) |
||
73 | ); |
||
74 | |||
75 | $emogrifier->expects($this->exactly(2)) |
||
76 | ->method('emogrify'); |
||
77 | |||
78 | |||
79 | $plugin->setEmogrifier($emogrifier); |
||
80 | $plugin->beforeSendPerformed($event); |
||
81 | } |
||
82 | |||
83 | public function testEmptyBodies() |
||
84 | { |
||
85 | $message = $this->createMockedMessage('', 'text/html', [ |
||
86 | $this->createMockedMessagePart('') |
||
87 | ]); |
||
88 | |||
89 | $event = $this->createMockedSendEvent($message); |
||
90 | |||
91 | $plugin = new EmogrifierPlugin(); |
||
92 | |||
93 | $emogrifier = $this->getMockBuilder(Emogrifier::class) |
||
94 | ->disableOriginalConstructor() |
||
95 | ->getMock(); |
||
96 | |||
97 | $emogrifier->expects($this->never()) |
||
98 | ->method('setHtml'); |
||
99 | |||
100 | $emogrifier->expects($this->never()) |
||
101 | ->method('emogrify'); |
||
102 | |||
103 | |||
104 | $plugin->setEmogrifier($emogrifier); |
||
105 | $plugin->beforeSendPerformed($event); |
||
106 | } |
||
107 | |||
108 | public function testSendPerformed() |
||
109 | { |
||
110 | $event = $this->createMockedSendEvent($this->createMockedMessage('')); |
||
111 | |||
112 | $plugin = new EmogrifierPlugin(); |
||
113 | $this->assertEmpty($plugin->sendPerformed($event)); |
||
0 ignored issues
–
show
Are you sure the usage of
$plugin->sendPerformed($event) targeting Bummzack\SwiftMailer\Emo...Plugin::sendPerformed() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Build a mocked send-event that will return the given message |
||
118 | * @param $message |
||
119 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
120 | */ |
||
121 | protected function createMockedSendEvent($message) |
||
122 | { |
||
123 | $event = $this->getMockBuilder('Swift_Events_SendEvent') |
||
124 | ->disableOriginalConstructor() |
||
125 | ->getMock(); |
||
126 | |||
127 | $event->expects($this->any()) |
||
128 | ->method('getMessage') |
||
129 | ->will($this->returnValue($message)); |
||
130 | |||
131 | return $event; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Build a mocked message part |
||
136 | * @param $body |
||
137 | * @param string $contentType |
||
138 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
139 | */ |
||
140 | protected function createMockedMessagePart($body, $contentType = 'text/html') |
||
141 | { |
||
142 | $messagePart = $this->getMockBuilder('Swift_Mime_MimeEntity') |
||
143 | ->disableOriginalConstructor() |
||
144 | ->getMock(); |
||
145 | |||
146 | $messagePart->expects($this->any()) |
||
147 | ->method('getBody') |
||
148 | ->will($this->returnValue($body)); |
||
149 | |||
150 | $messagePart->expects($this->any()) |
||
151 | ->method('getContentType') |
||
152 | ->will($this->returnValue($contentType)); |
||
153 | |||
154 | return $messagePart; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Build a mocked swift message |
||
159 | * @param $body |
||
160 | * @param string $contentType |
||
161 | * @param array $messageParts |
||
162 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
163 | */ |
||
164 | protected function createMockedMessage($body, $contentType = 'text/html', array $messageParts = []) |
||
165 | { |
||
166 | $message = $this->getMockBuilder('Swift_Mime_Message') |
||
167 | ->disableOriginalConstructor() |
||
168 | ->getMock(); |
||
169 | |||
170 | $message->expects($this->any()) |
||
171 | ->method('getBody') |
||
172 | ->will($this->returnValue($body)); |
||
173 | |||
174 | $message->expects($this->any()) |
||
175 | ->method('getContentType') |
||
176 | ->will($this->returnValue($contentType)); |
||
177 | |||
178 | $message->expects($this->any()) |
||
179 | ->method('getChildren') |
||
180 | ->will($this->returnValue($messageParts)); |
||
181 | |||
182 | return $message; |
||
183 | } |
||
184 | } |
||
185 |