1
|
|
|
<?php |
2
|
|
|
namespace Azine\EmailBundle\Tests\Services; |
3
|
|
|
|
4
|
|
|
use Azine\EmailBundle\Services\AzineTemplateProvider; |
5
|
|
|
use Azine\EmailBundle\DependencyInjection\AzineEmailExtension; |
6
|
|
|
use Azine\EmailBundle\Services\AzineTwigSwiftMailer; |
7
|
|
|
|
8
|
|
|
class AzineTwigSwiftMailerTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
private function getMockSetup($sendCallback) |
11
|
|
|
{ |
12
|
|
|
$mocks['mailer'] = $this->getMockBuilder("\Swift_Mailer")->disableOriginalConstructor()->getMock(); |
|
|
|
|
13
|
|
|
$mocks['mailer']->expects($this->once())->method('send')->will($this->returnCallback($sendCallback)); |
14
|
|
|
$mocks['router'] = $this->getMockBuilder("Symfony\Component\Routing\Generator\UrlGeneratorInterface")->disableOriginalConstructor()->getMock(); |
15
|
|
|
$mocks['twig'] = $this->getMockBuilder("\Twig_Environment")->disableOriginalConstructor()->getMock(); |
16
|
|
|
$mocks['baseTemplateMock'] = $this->getMockBuilder("\Twig_Template")->disableOriginalConstructor()->setMethods(array('renderBlock'))->getMockForAbstractClass(); |
17
|
|
|
$mocks['twig']->expects($this->once())->method('loadTemplate')->will($this->returnValue($mocks['baseTemplateMock'])); |
18
|
|
|
|
19
|
|
|
$mocks['logger'] = $this->getMockBuilder("Monolog\Logger")->disableOriginalConstructor()->getMock(); |
20
|
|
|
|
21
|
|
|
$mocks['translator'] = $this->getMockBuilder("Symfony\Bundle\FrameworkBundle\Translation\Translator")->disableOriginalConstructor()->getMock(); |
22
|
|
|
$mocks['translator']->expects($this->any())->method('trans')->will($this->returnValue("azine.translation.mock")); |
23
|
|
|
|
24
|
|
|
$imagesDir = realpath(__DIR__."/../../Resources/htmlTemplateImages/"); |
25
|
|
|
$mocks['templateProvider'] = new AzineTemplateProvider($mocks['router'], $mocks['translator'], array( AzineEmailExtension::ALLOWED_IMAGES_FOLDERS => array($imagesDir), |
26
|
|
|
AzineEmailExtension::TEMPLATE_IMAGE_DIR => $imagesDir, |
27
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_NAME=> "utm_campaign", |
28
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_TERM => "utm_term", |
29
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_SOURCE => "utm_source", |
30
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_MEDIUM => "utm_medium", |
31
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_CONTENT => "utm_content", |
32
|
|
|
)); |
33
|
|
|
$this->getMockBuilder("Azine\EmailBundle\Services\AzineTemplateProvider")->disableOriginalConstructor()->getMock(); |
34
|
|
|
|
35
|
|
|
$mocks['entityManager'] = $this->getMockBuilder("Doctrine\ORM\EntityManager")->disableOriginalConstructor()->getMock(); |
36
|
|
|
$mocks['parameters'] = array( AzineEmailExtension::NO_REPLY => array( |
37
|
|
|
AzineEmailExtension::NO_REPLY_EMAIL_ADDRESS => '[email protected]', |
38
|
|
|
AzineEmailExtension::NO_REPLY_EMAIL_NAME => 'no-reply-name'), |
39
|
|
|
AzineTemplateProvider::CONTENT_ITEMS => array( |
40
|
|
|
0 => array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('notification' => array('title' => 'some title', 'created' => new \DateTime('2 hours ago'), 'content' => "some content"))), |
41
|
|
|
1 => array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('notification' => array('title' => 'some other title', 'created' => new \DateTime('1 hours ago'), 'content' => "some other content"))) |
42
|
|
|
), |
43
|
|
|
'logo_png' => $imagesDir."/logo.png", |
44
|
|
|
'noFile_png' => $imagesDir."/../../../unallowedFolder/logo.png", |
45
|
|
|
'not_allowed_png' => $imagesDir."/inexistentFile.png", |
46
|
|
|
); |
47
|
|
|
$requestContext = $this->getMockBuilder("Symfony\Component\Routing\RequestContext")->disableOriginalConstructor()->getMock(); |
48
|
|
|
$requestContext->expects($this->once())->method("getHost")->will($this->returnValue("azine.test.host")); |
49
|
|
|
$mocks['router']->expects($this->once())->method('getContext')->will($this->returnValue($requestContext)); |
50
|
|
|
|
51
|
|
|
$mocks['trackingCodeImgBuilder'] = $this->getMockBuilder("Azine\EmailBundle\Services\AzineEmailOpenTrackingCodeBuilder")->setConstructorArgs(array("https://www.google-analytics.com/?tid=blabla", array(AzineEmailExtension::ALLOWED_IMAGES_FOLDERS => array($imagesDir), |
52
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_NAME=> "utm_campaign", |
53
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_TERM => "utm_term", |
54
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_SOURCE => "utm_source", |
55
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_MEDIUM => "utm_medium", |
56
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_CONTENT => "utm_content", |
57
|
|
|
)))->getMock(); |
58
|
|
|
$mocks['trackingCodeImgBuilder']->expects($this->any())->method('getTrackingImgCode')->will($this->returnValue("<img src='https://www.google-analytics.com/?tid=blabla&utm_medium=email' style='border:0' alt='' />")); |
59
|
|
|
|
60
|
|
|
return $mocks; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function returnOne(\Swift_Mime_Message $message, &$failedRecipients = null){ |
64
|
|
|
return 1; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function returnOneValidateCampaignUrls(\Swift_Mime_Message $message, &$failedRecipients = null){ |
68
|
|
|
$body = $message->getBody(); |
69
|
|
|
|
70
|
|
|
// has a email-tracking-image at the end |
71
|
|
|
$this->assertContains("<img src='https://www.google-analytics.com/?tid=blabla", $body, "Email open tracking image not found."); |
72
|
|
|
|
73
|
|
|
// links have tracking-parameters |
74
|
|
|
$this->assertContains("&utm_medium=email", $body, "Email links are expected to have tracking parameters attached."); |
75
|
|
|
return 1; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function returnZeroWithFailedAddress(\Swift_Mime_Message $message, &$failedRecipients = null){ |
79
|
|
|
$failedRecipients[] = $message->getTo(); |
80
|
|
|
return 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return \FOS\UserBundle\Model\UserInterface |
85
|
|
|
*/ |
86
|
|
|
private function getUserMock() |
87
|
|
|
{ |
88
|
|
|
$user = $this->getMockBuilder("FOS\UserBundle\Model\UserInterface")->disableOriginalConstructor()->getMock(); |
89
|
|
|
$user->expects($this->once())->method('getEmail')->will($this->returnValue("[email protected]")); |
90
|
|
|
$user->expects($this->any())->method('getConfirmationToken')->will($this->returnValue("aptrqi3o4pte:::token:::zfpguhask5jx0a9xukp")); |
91
|
|
|
|
92
|
|
|
return $user; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function renderBlockCallback($name, $context = array(), $blocks = array()) |
96
|
|
|
{ |
97
|
|
|
if ($name == 'subject') { |
98
|
|
|
return "a subject"; |
99
|
|
|
} elseif ($name == 'body_html') { |
100
|
|
|
$generatedImage = ""; |
101
|
|
|
if (array_key_exists("embededUsedGeneratedImage", $context)) { |
102
|
|
|
$generatedImage = "<img src='".$context['embededUsedGeneratedImage']."' alt='generatedImage'>"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return "<html><body><h1>a html body</h1>$generatedImage<a href='http://some.url.com/' ><img src='".$context['logo_png']."' alt='logo'></a><p>with a paragraph and <a href='https://foo.bar.com/index.php?q=4'>links</a>.</p></body><html>"; |
106
|
|
|
} elseif ($name == 'body_text') { |
107
|
|
|
return "a text body \n \n with new lines."; |
108
|
|
|
} |
109
|
|
|
throw new \Exception("un-known block : '$name'"); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function generateCallback($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) |
113
|
|
|
{ |
114
|
|
|
if ($name == 'fos_user_registration_confirm') { |
115
|
|
|
return "http://azine.bundle.com/confirmation/url/".$parameters['token']; |
116
|
|
|
|
117
|
|
|
} elseif ($name == 'fos_user_resetting_reset') { |
118
|
|
|
return "http://azine.bundle.com/resetting/url/".$parameters['token']; |
119
|
|
|
|
120
|
|
|
} elseif ($name == 'azine_email_serve_template_image') { |
121
|
|
|
return "http://azine.bundle.com/image/url/logo.png"; |
122
|
|
|
} |
123
|
|
|
throw new \Exception("un-expected route for url-generation : '$name'"); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testSendSingleEmail() |
127
|
|
|
{ |
128
|
|
|
$mocks = $this->getMockSetup(array($this, 'returnOneValidateCampaignUrls')); |
129
|
|
|
$mocks['baseTemplateMock']->expects($this->exactly(2))->method('renderBlock')->will($this->returnCallback(array($this, 'renderBlockCallback'))); |
130
|
|
|
$mocks['translator']->expects($this->once())->method('getLocale')->will($this->returnValue("en")); |
131
|
|
|
$mocks['router']->expects($this->exactly(12))->method('generate')->will($this->returnCallback(array($this, 'generateCallback'))); |
132
|
|
|
|
133
|
|
|
$azineMailer = new AzineTwigSwiftMailer($mocks['mailer'], $mocks['router'], $mocks['twig'], $mocks['logger'], $mocks['translator'], $mocks['templateProvider'], $mocks['entityManager'], $mocks['trackingCodeImgBuilder'], $mocks['parameters']); |
134
|
|
|
|
135
|
|
|
$to = "[email protected]"; |
136
|
|
|
$toName = "ToName"; |
137
|
|
|
$params = array("aKey" => "aValue", 'contentItems' => array(array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('someOtherKey' => 'someOtherValue')))); |
138
|
|
|
$template = AzineTemplateProvider::NEWSLETTER_TEMPLATE.".txt.twig"; |
139
|
|
|
$emailLocale = "en"; |
140
|
|
|
$subject = "custom subject"; |
141
|
|
|
$azineMailer->sendSingleEmail($to, $toName, $subject, $params, $template, $emailLocale); |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testSendSingleEmailFails() |
146
|
|
|
{ |
147
|
|
|
$mocks['mailer'] = $this->getMockBuilder("\Swift_Mailer")->disableOriginalConstructor()->getMock(); |
|
|
|
|
148
|
|
|
$mocks['mailer']->expects($this->once())->method('send')->will($this->returnCallback(array($this, 'returnZeroWithFailedAddress'))); |
149
|
|
|
$mocks['router'] = $this->getMockBuilder("Symfony\Component\Routing\Generator\UrlGeneratorInterface")->disableOriginalConstructor()->getMock(); |
150
|
|
|
$mocks['twig'] = $this->getMockBuilder("\Twig_Environment")->disableOriginalConstructor()->getMock(); |
151
|
|
|
$mocks['baseTemplateMock'] = $this->getMockBuilder("\Twig_Template")->disableOriginalConstructor()->setMethods(array('renderBlock'))->getMockForAbstractClass(); |
152
|
|
|
$mocks['twig']->expects($this->once())->method('loadTemplate')->will($this->returnValue($mocks['baseTemplateMock'])); |
153
|
|
|
|
154
|
|
|
$mocks['logger'] = $this->getMockBuilder("Monolog\Logger")->disableOriginalConstructor()->getMock(); |
155
|
|
|
|
156
|
|
|
$mocks['translator'] = $this->getMockBuilder("Symfony\Bundle\FrameworkBundle\Translation\Translator")->disableOriginalConstructor()->getMock(); |
157
|
|
|
$mocks['translator']->expects($this->any())->method('trans')->will($this->returnValue("azine.translation.mock")); |
158
|
|
|
|
159
|
|
|
$imagesDir = realpath(__DIR__."/../../Resources/htmlTemplateImages/"); |
160
|
|
|
$mocks['templateProvider'] = new AzineTemplateProvider($mocks['router'], $mocks['translator'], array( AzineEmailExtension::ALLOWED_IMAGES_FOLDERS => array($imagesDir), |
161
|
|
|
AzineEmailExtension::TEMPLATE_IMAGE_DIR => $imagesDir, |
162
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_NAME=> "utm_campaign", |
163
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_TERM => "utm_term", |
164
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_SOURCE => "utm_source", |
165
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_MEDIUM => "utm_medium", |
166
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_CONTENT => "utm_content", |
167
|
|
|
)); |
168
|
|
|
$this->getMockBuilder("Azine\EmailBundle\Services\AzineTemplateProvider")->disableOriginalConstructor()->getMock(); |
169
|
|
|
|
170
|
|
|
$mocks['entityManager'] = $this->getMockBuilder("Doctrine\ORM\EntityManager")->disableOriginalConstructor()->getMock(); |
171
|
|
|
$mocks['parameters'] = array( AzineEmailExtension::NO_REPLY => array( |
172
|
|
|
AzineEmailExtension::NO_REPLY_EMAIL_ADDRESS => '[email protected]', |
173
|
|
|
AzineEmailExtension::NO_REPLY_EMAIL_NAME => 'no-reply-name'), |
174
|
|
|
AzineTemplateProvider::CONTENT_ITEMS => array( |
175
|
|
|
0 => array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('notification' => array('title' => 'some title', 'created' => new \DateTime('2 hours ago'), 'content' => "some content"))), |
176
|
|
|
1 => array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('notification' => array('title' => 'some other title', 'created' => new \DateTime('1 hours ago'), 'content' => "some other content"))) |
177
|
|
|
), |
178
|
|
|
'logo_png' => $imagesDir."/logo.png", |
179
|
|
|
'noFile_png' => $imagesDir."/../../../unallowedFolder/logo.png", |
180
|
|
|
'not_allowed_png' => $imagesDir."/inexistentFile.png", |
181
|
|
|
); |
182
|
|
|
$requestContext = $this->getMockBuilder("Symfony\Component\Routing\RequestContext")->disableOriginalConstructor()->getMock(); |
183
|
|
|
$requestContext->expects($this->once())->method("getHost")->will($this->returnValue("azine.test.host")); |
184
|
|
|
$mocks['router']->expects($this->once())->method('getContext')->will($this->returnValue($requestContext)); |
185
|
|
|
|
186
|
|
|
$mocks['trackingCodeImgBuilder'] = $this->getMockBuilder("Azine\EmailBundle\Services\AzineEmailOpenTrackingCodeBuilder")->setConstructorArgs(array("https://www.google-analytics.com/?tid=blabla", array(AzineEmailExtension::ALLOWED_IMAGES_FOLDERS => array($imagesDir), |
187
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_NAME=> "utm_campaign", |
188
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_TERM => "utm_term", |
189
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_SOURCE => "utm_source", |
190
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_MEDIUM => "utm_medium", |
191
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_CONTENT => "utm_content", |
192
|
|
|
)))->getMock(); |
193
|
|
|
|
194
|
|
|
$mocks['baseTemplateMock']->expects($this->exactly(2))->method('renderBlock')->will($this->returnCallback(array($this, 'renderBlockCallback'))); |
195
|
|
|
$mocks['translator']->expects($this->once())->method('getLocale')->will($this->returnValue("en")); |
196
|
|
|
//$mocks['router']->expects($this->exactly(12))->method('generate')->will($this->returnCallback(array($this, 'generateCallback'))); |
197
|
|
|
|
198
|
|
|
$azineMailer = new AzineTwigSwiftMailer($mocks['mailer'], $mocks['router'], $mocks['twig'], $mocks['logger'], $mocks['translator'], $mocks['templateProvider'], $mocks['entityManager'], $mocks['trackingCodeImgBuilder'], $mocks['parameters']); |
199
|
|
|
|
200
|
|
|
$to = "[email protected]"; |
201
|
|
|
$toName = "ToName"; |
202
|
|
|
$params = array("aKey" => "aValue", 'contentItems' => array(array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('someOtherKey' => 'someOtherValue')))); |
203
|
|
|
$template = AzineTemplateProvider::NEWSLETTER_TEMPLATE.".txt.twig"; |
204
|
|
|
$emailLocale = "en"; |
205
|
|
|
$subject = "custom subject"; |
206
|
|
|
$result = $azineMailer->sendSingleEmail($to, $toName, $subject, $params, $template, $emailLocale); |
207
|
|
|
$this->assertFalse($result, "expected send to fail"); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testSendEmailWithEmailLocaleAndAttachments() |
211
|
|
|
{ |
212
|
|
|
$mocks = $this->getMockSetup(array($this, 'returnOne')); |
213
|
|
|
$mocks['baseTemplateMock']->expects($this->exactly(2))->method('renderBlock')->will($this->returnCallback(array($this, 'renderBlockCallback'))); |
214
|
|
|
$mocks['translator']->expects($this->once())->method('getLocale')->will($this->returnValue("en")); |
215
|
|
|
$mocks['router']->expects($this->exactly(6))->method('generate')->will($this->returnCallback(array($this, 'generateCallback'))); |
216
|
|
|
|
217
|
|
|
$azineMailer = new AzineTwigSwiftMailer($mocks['mailer'], $mocks['router'], $mocks['twig'], $mocks['logger'], $mocks['translator'], $mocks['templateProvider'], $mocks['entityManager'], $mocks['trackingCodeImgBuilder'], $mocks['parameters']); |
218
|
|
|
|
219
|
|
|
$failedRecipients = array(); |
220
|
|
|
$from = "[email protected]"; |
221
|
|
|
$fromName = "FromName"; |
222
|
|
|
$to = "[email protected]"; |
223
|
|
|
$toName = "ToName"; |
224
|
|
|
$cc = "[email protected]"; |
225
|
|
|
$ccName = "CcName"; |
226
|
|
|
$bcc = "[email protected]"; |
227
|
|
|
$bccName = "BccName"; |
228
|
|
|
$replyTo = "[email protected]"; |
229
|
|
|
$replyToName = "ReplyToName"; |
230
|
|
|
$subject = "some dummy test subject"; |
231
|
|
|
$params = array(); |
232
|
|
|
$generatedImage = imagecreate(100, 100); |
233
|
|
|
$background_color = imagecolorallocate($generatedImage, 0, 0, 0); |
|
|
|
|
234
|
|
|
$text_color = imagecolorallocate($generatedImage, 233, 14, 91); |
235
|
|
|
imagestring($generatedImage, 1, 5, 5, "A Simple Text String", $text_color); |
236
|
|
|
$template = AzineTemplateProvider::NEWSLETTER_TEMPLATE.".txt.twig"; |
237
|
|
|
|
238
|
|
|
// embed a regular file, a generated file and an invalid file |
239
|
|
|
$params['embededUnusedFile'] = __FILE__; |
240
|
|
|
$params['embededUnusedGeneratedFile'] = $generatedImage; |
241
|
|
|
$params['embededUsedGeneratedImage'] = $generatedImage; |
242
|
|
|
$params['embededUnusedInexistentFile'] = __FILE__."not.existent.jpg"; |
243
|
|
|
|
244
|
|
|
// attach a regular file and a generated file |
245
|
|
|
$attachments = array('regularFile' => __FILE__, 'generatedFile' => $generatedImage, "fileWithVeryShortName.replacement.txt" => __DIR__."/a.b"); |
246
|
|
|
$emailLocale = "en"; |
247
|
|
|
|
248
|
|
|
$azineMailer->sendEmail($failedRecipients, $subject, $from, $fromName, $to, $toName, $cc, $ccName, $bcc, $bccName, $replyTo, $replyToName, $params, $template, $attachments, $emailLocale); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @expectedException Symfony\Component\HttpFoundation\File\Exception\FileException |
253
|
|
|
*/ |
254
|
|
|
public function testSendEmailWithEmailLocaleAndInexistentAttachment() |
255
|
|
|
{ |
256
|
|
|
$mocks['mailer'] = $this->getMockBuilder("\Swift_Mailer")->disableOriginalConstructor()->getMock(); |
|
|
|
|
257
|
|
|
$mocks['mailer']->expects($this->never())->method('send'); |
258
|
|
|
|
259
|
|
|
$mocks['router'] = $this->getMockBuilder("Symfony\Component\Routing\Generator\UrlGeneratorInterface")->disableOriginalConstructor()->getMock(); |
260
|
|
|
$mocks['twig'] = $this->getMockBuilder("\Twig_Environment")->disableOriginalConstructor()->getMock(); |
261
|
|
|
$mocks['baseTemplateMock'] = $this->getMockBuilder("\Twig_Template")->disableOriginalConstructor()->setMethods(array('renderBlock'))->getMockForAbstractClass(); |
262
|
|
|
$mocks['twig']->expects($this->once())->method('loadTemplate')->will($this->returnValue($mocks['baseTemplateMock'])); |
263
|
|
|
|
264
|
|
|
$mocks['logger'] = $this->getMockBuilder("Monolog\Logger")->disableOriginalConstructor()->getMock(); |
265
|
|
|
|
266
|
|
|
$mocks['translator'] = $this->getMockBuilder("Symfony\Bundle\FrameworkBundle\Translation\Translator")->disableOriginalConstructor()->getMock(); |
267
|
|
|
$mocks['translator']->expects($this->any())->method('trans')->will($this->returnValue("azine.translation.mock")); |
268
|
|
|
|
269
|
|
|
$imagesDir = realpath(__DIR__."/../../Resources/htmlTemplateImages/"); |
270
|
|
|
$mocks['templateProvider'] = new AzineTemplateProvider($mocks['router'], $mocks['translator'], array( AzineEmailExtension::ALLOWED_IMAGES_FOLDERS => array($imagesDir), |
271
|
|
|
AzineEmailExtension::TEMPLATE_IMAGE_DIR => $imagesDir, |
272
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_NAME=> "utm_campaign", |
273
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_TERM => "utm_term", |
274
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_SOURCE => "utm_source", |
275
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_MEDIUM => "utm_medium", |
276
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_CONTENT => "utm_content", |
277
|
|
|
)); |
278
|
|
|
$this->getMockBuilder("Azine\EmailBundle\Services\AzineTemplateProvider")->disableOriginalConstructor()->getMock(); |
279
|
|
|
|
280
|
|
|
$mocks['entityManager'] = $this->getMockBuilder("Doctrine\ORM\EntityManager")->disableOriginalConstructor()->getMock(); |
281
|
|
|
$mocks['parameters'] = array( AzineEmailExtension::NO_REPLY => array( |
282
|
|
|
AzineEmailExtension::NO_REPLY_EMAIL_ADDRESS => '[email protected]', |
283
|
|
|
AzineEmailExtension::NO_REPLY_EMAIL_NAME => 'no-reply-name'), |
284
|
|
|
AzineTemplateProvider::CONTENT_ITEMS => array( |
285
|
|
|
0 => array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('notification' => array('title' => 'some title', 'created' => new \DateTime('2 hours ago'), 'content' => "some content"))), |
286
|
|
|
1 => array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('notification' => array('title' => 'some other title', 'created' => new \DateTime('1 hours ago'), 'content' => "some other content"))) |
287
|
|
|
), |
288
|
|
|
'logo_png' => $imagesDir."/logo.png", |
289
|
|
|
'noFile_png' => $imagesDir."/../../../unallowedFolder/logo.png", |
290
|
|
|
'not_allowed_png' => $imagesDir."/inexistentFile.png", |
291
|
|
|
); |
292
|
|
|
$requestContext = $this->getMockBuilder("Symfony\Component\Routing\RequestContext")->disableOriginalConstructor()->getMock(); |
293
|
|
|
$requestContext->expects($this->once())->method("getHost")->will($this->returnValue("azine.test.host")); |
294
|
|
|
$mocks['router']->expects($this->once())->method('getContext')->will($this->returnValue($requestContext)); |
295
|
|
|
$mocks['baseTemplateMock']->expects($this->exactly(2))->method('renderBlock')->will($this->returnCallback(array($this, 'renderBlockCallback'))); |
296
|
|
|
$mocks['translator']->expects($this->once())->method('getLocale')->will($this->returnValue("en")); |
297
|
|
|
$mocks['router']->expects($this->never())->method('generate'); |
298
|
|
|
|
299
|
|
|
$mocks['trackingCodeImgBuilder'] = $this->getMockBuilder("Azine\EmailBundle\Services\AzineEmailOpenTrackingCodeBuilder")->setConstructorArgs(array("http://www.google-analytics.com/?", array(AzineEmailExtension::ALLOWED_IMAGES_FOLDERS => array($imagesDir), |
300
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_NAME=> "utm_campaign", |
301
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_TERM => "utm_term", |
302
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_SOURCE => "utm_source", |
303
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_MEDIUM => "utm_medium", |
304
|
|
|
AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_CONTENT => "utm_content", |
305
|
|
|
)))->getMock(); |
306
|
|
|
|
307
|
|
|
|
308
|
|
|
$azineMailer = new AzineTwigSwiftMailer($mocks['mailer'], $mocks['router'], $mocks['twig'], $mocks['logger'], $mocks['translator'], $mocks['templateProvider'], $mocks['entityManager'], $mocks['trackingCodeImgBuilder'], $mocks['parameters']); |
309
|
|
|
|
310
|
|
|
$failedRecipients = array(); |
311
|
|
|
$from = "[email protected]"; |
312
|
|
|
$fromName = "FromName"; |
313
|
|
|
$to = "[email protected]"; |
314
|
|
|
$toName = "ToName"; |
315
|
|
|
$cc = "[email protected]"; |
316
|
|
|
$ccName = "CcName"; |
317
|
|
|
$bcc = "[email protected]"; |
318
|
|
|
$bccName = "BccName"; |
319
|
|
|
$replyTo = "[email protected]"; |
320
|
|
|
$replyToName = "ReplyToName"; |
321
|
|
|
$subject = "some dummy test subject"; |
322
|
|
|
$params = array(); |
323
|
|
|
$template = AzineTemplateProvider::NEWSLETTER_TEMPLATE.".txt.twig"; |
324
|
|
|
|
325
|
|
|
// embed an inexistent file |
326
|
|
|
$params['embededUnusedInexistentFile'] = __FILE__."not.existent.jpg"; |
327
|
|
|
|
328
|
|
|
// attach an inexistent file |
329
|
|
|
$attachments = array(__FILE__."not.existent.jpg"); |
330
|
|
|
$emailLocale = "en"; |
331
|
|
|
|
332
|
|
|
$azineMailer->sendEmail($failedRecipients, $subject, $from, $fromName, $to, $toName, $cc, $ccName, $bcc, $bccName, $replyTo, $replyToName, $params, $template, $attachments, $emailLocale); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function testSendEmailWithOutEmailLocaleAndNoAttachment() |
336
|
|
|
{ |
337
|
|
|
$mocks = $this->getMockSetup(array($this, 'returnOne')); |
338
|
|
|
$mocks['baseTemplateMock']->expects($this->exactly(2))->method('renderBlock')->will($this->returnCallback(array($this, 'renderBlockCallback'))); |
339
|
|
|
$mocks['translator']->expects($this->once())->method('getLocale')->will($this->returnValue("en")); |
340
|
|
|
$mocks['router']->expects($this->exactly(0))->method('generate')->will($this->returnCallback(array($this, 'generateCallback'))); |
341
|
|
|
|
342
|
|
|
$azineMailer = new AzineTwigSwiftMailer($mocks['mailer'], $mocks['router'], $mocks['twig'], $mocks['logger'], $mocks['translator'], $mocks['templateProvider'], $mocks['entityManager'], $mocks['trackingCodeImgBuilder'], $mocks['parameters']); |
343
|
|
|
|
344
|
|
|
$failedRecipients = array(); |
345
|
|
|
$from = "[email protected]"; |
346
|
|
|
$fromName = "FromName"; |
347
|
|
|
$to = "[email protected]"; |
348
|
|
|
$toName = "ToName"; |
349
|
|
|
$cc = "[email protected]"; |
350
|
|
|
$ccName = "CcName"; |
351
|
|
|
$bcc = "[email protected]"; |
352
|
|
|
$bccName = "BccName"; |
353
|
|
|
$replyTo = "[email protected]"; |
354
|
|
|
$replyToName = "ReplyToName"; |
355
|
|
|
$subject = "some dummy test subject"; |
356
|
|
|
$params = array(); |
357
|
|
|
$template = AzineTemplateProvider::BASE_TEMPLATE.".txt.twig"; |
358
|
|
|
$attachments = array(); |
359
|
|
|
$emailLocale = null; |
360
|
|
|
|
361
|
|
|
$sentCount = $azineMailer->sendEmail($failedRecipients, $subject, $from, $fromName, $to, $toName, $cc, $ccName, $bcc, $bccName, $replyTo, $replyToName, $params, $template, $attachments, $emailLocale); |
362
|
|
|
|
363
|
|
|
$this->assertEquals(1, $sentCount, "One email should have been sent."); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
View Code Duplication |
public function testSendConfirmationEmailMessage() |
|
|
|
|
367
|
|
|
{ |
368
|
|
|
$mocks = $this->getMockSetup(array($this, 'returnOne')); |
369
|
|
|
$user = $this->getUserMock(); |
370
|
|
|
|
371
|
|
|
// as the subject from FOS-templates is embeded in the twig-template, the render-block is called 3 instead of only 2 times |
372
|
|
|
$mocks['baseTemplateMock']->expects($this->exactly(3))->method('renderBlock')->will($this->returnCallback(array($this, 'renderBlockCallback'))); |
373
|
|
|
|
374
|
|
|
$mocks['parameters']['template'] = array(); |
375
|
|
|
$mocks['parameters']['template']['confirmation'] = AzineTemplateProvider::FOS_USER_REGISTRATION_TEMPLATE.".txt.twig"; |
376
|
|
|
$mocks['parameters']['from_email'] = array(); |
377
|
|
|
$mocks['parameters']['from_email']['confirmation'] = '[email protected]'; |
378
|
|
|
|
379
|
|
|
$mocks['router']->expects($this->once())->method('generate')->will($this->returnCallback(array($this, 'generateCallback'))); |
380
|
|
|
|
381
|
|
|
$mocks['translator']->expects($this->exactly(2))->method('getLocale')->will($this->returnValue("en")); |
382
|
|
|
|
383
|
|
|
$azineMailer = new AzineTwigSwiftMailer($mocks['mailer'], $mocks['router'], $mocks['twig'], $mocks['logger'], $mocks['translator'], $mocks['templateProvider'], $mocks['entityManager'], $mocks['trackingCodeImgBuilder'], $mocks['parameters']); |
384
|
|
|
|
385
|
|
|
$azineMailer->sendConfirmationEmailMessage($user); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
View Code Duplication |
public function testSendResettingEmailMessage() |
|
|
|
|
389
|
|
|
{ |
390
|
|
|
$mocks = $this->getMockSetup(array($this, 'returnOne')); |
391
|
|
|
$user = $this->getUserMock(); |
392
|
|
|
|
393
|
|
|
// as the subject from FOS-templates is embeded in the twig-template, the render-block is called 3 instead of only 2 times |
394
|
|
|
$mocks['baseTemplateMock']->expects($this->exactly(3))->method('renderBlock')->will($this->returnCallback(array($this, 'renderBlockCallback'))); |
395
|
|
|
|
396
|
|
|
$mocks['parameters']['template'] = array(); |
397
|
|
|
$mocks['parameters']['template']['resetting'] = AzineTemplateProvider::FOS_USER_PWD_RESETTING_TEMPLATE.".txt.twig"; |
398
|
|
|
$mocks['parameters']['from_email'] = array(); |
399
|
|
|
$mocks['parameters']['from_email']['resetting'] = '[email protected]'; |
400
|
|
|
|
401
|
|
|
$mocks['translator']->expects($this->exactly(2))->method('getLocale')->will($this->returnValue("en")); |
402
|
|
|
|
403
|
|
|
$mocks['router']->expects($this->once())->method('generate')->will($this->returnCallback(array($this, 'generateCallback'))); |
404
|
|
|
|
405
|
|
|
$azineMailer = new AzineTwigSwiftMailer($mocks['mailer'], $mocks['router'], $mocks['twig'], $mocks['logger'], $mocks['translator'], $mocks['templateProvider'], $mocks['entityManager'], $mocks['trackingCodeImgBuilder'], $mocks['parameters']); |
406
|
|
|
|
407
|
|
|
$azineMailer->sendResettingEmailMessage($user); |
408
|
|
|
|
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.