1 | <?php |
||||||||
2 | |||||||||
3 | namespace Azine\EmailBundle\Tests\Services; |
||||||||
4 | |||||||||
5 | use Azine\EmailBundle\DependencyInjection\AzineEmailExtension; |
||||||||
6 | use Azine\EmailBundle\Entity\Notification; |
||||||||
7 | use Azine\EmailBundle\Entity\RecipientInterface; |
||||||||
8 | use Azine\EmailBundle\Services\AzineNotifierService; |
||||||||
9 | use Azine\EmailBundle\Services\AzineTemplateProvider; |
||||||||
10 | use Azine\EmailBundle\Services\ExampleNotifierService; |
||||||||
11 | |||||||||
12 | class AzineNotifierServiceTest extends \PHPUnit\Framework\TestCase |
||||||||
13 | { |
||||||||
14 | private function getMockSetup() |
||||||||
15 | { |
||||||||
16 | $mocks = array(); |
||||||||
17 | $mocks['mailer'] = $this->getMockBuilder("Azine\EmailBundle\Services\TemplateTwigSwiftMailerInterface")->disableOriginalConstructor()->getMock(); |
||||||||
18 | $mocks['twig'] = $this->getMockBuilder("\Twig_Environment")->disableOriginalConstructor()->getMock(); |
||||||||
19 | $mocks['router'] = $this->getMockBuilder("Symfony\Component\Routing\Generator\UrlGeneratorInterface")->disableOriginalConstructor()->getMock(); |
||||||||
20 | $mocks['entityManager'] = $this->getMockBuilder("Doctrine\ORM\EntityManager")->disableOriginalConstructor()->getMock(); |
||||||||
21 | $mocks['notificationRepository'] = $this->getMockBuilder("Azine\EmailBundle\Entity\Repositories\NotificationRepository")->disableOriginalConstructor()->getMock(); |
||||||||
22 | $mocks['managerRegistry'] = $this->getMockBuilder("Doctrine\Common\Persistence\ManagerRegistry")->disableOriginalConstructor()->getMock(); |
||||||||
23 | $mocks['managerRegistry']->expects($this->any())->method('getManager')->will($this->returnValue($mocks['entityManager'])); |
||||||||
24 | $mocks['managerRegistry']->expects($this->any())->method('getRepository')->will($this->returnValue($mocks['notificationRepository'])); |
||||||||
25 | $mocks['templateProvider'] = $this->getMockBuilder("Azine\EmailBundle\Services\TemplateProviderInterface")->disableOriginalConstructor()->getMock(); |
||||||||
26 | $mocks['recipientProvider'] = $this->getMockBuilder("Azine\EmailBundle\Services\RecipientProviderInterface")->disableOriginalConstructor()->getMock(); |
||||||||
27 | $mocks['translator'] = $this->getMockBuilder("Symfony\Bundle\FrameworkBundle\Translation\Translator")->disableOriginalConstructor()->getMock(); |
||||||||
28 | $mocks['parameters'] = array( |
||||||||
29 | AzineEmailExtension::NEWSLETTER.'_'.AzineEmailExtension::NEWSLETTER_INTERVAL => '7', |
||||||||
30 | AzineEmailExtension::NEWSLETTER.'_'.AzineEmailExtension::NEWSLETTER_SEND_TIME => '09:00', |
||||||||
31 | AzineEmailExtension::TEMPLATES.'_'.AzineEmailExtension::NEWSLETTER_TEMPLATE => AzineTemplateProvider::NEWSLETTER_TEMPLATE, |
||||||||
32 | AzineEmailExtension::TEMPLATES.'_'.AzineEmailExtension::NOTIFICATIONS_TEMPLATE => AzineTemplateProvider::NOTIFICATIONS_TEMPLATE, |
||||||||
33 | AzineEmailExtension::TEMPLATES.'_'.AzineEmailExtension::CONTENT_ITEM_TEMPLATE => AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE, |
||||||||
34 | ); |
||||||||
35 | |||||||||
36 | return $mocks; |
||||||||
37 | } |
||||||||
38 | |||||||||
39 | public function testAddNotification() |
||||||||
40 | { |
||||||||
41 | $mocks = $this->getMockSetup(); |
||||||||
42 | $mocks['entityManager']->expects($this->once())->method('persist'); |
||||||||
43 | $notifier = new AzineNotifierService($mocks['mailer'], $mocks['twig'], $mocks['router'], $mocks['managerRegistry'], $mocks['templateProvider'], $mocks['recipientProvider'], $mocks['translator'], $mocks['parameters']); |
||||||||
44 | |||||||||
45 | $n = $notifier->addNotification('12', 'title', 'content', 'template', array('templateVars'), 1, false); |
||||||||
46 | |||||||||
47 | $this->assertSame('12', $n->getRecipientId()); |
||||||||
48 | $this->assertSame('title', $n->getTitle()); |
||||||||
49 | $this->assertSame('template', $n->getTemplate()); |
||||||||
50 | $this->assertSame(array('templateVars'), $n->getVariables()); |
||||||||
51 | } |
||||||||
52 | |||||||||
53 | public function testAddNotificationMessage() |
||||||||
54 | { |
||||||||
55 | $goToUrl = 'http://azine.email/this/is/a/url'; |
||||||||
56 | $templateVars = array('logo_png' => '/some/directory/logo.png', 'mainColor' => 'green'); |
||||||||
57 | $mocks = $this->getMockSetup(); |
||||||||
58 | $mocks['entityManager']->expects($this->once())->method('persist'); |
||||||||
59 | $mocks['templateProvider']->expects($this->once())->method('addTemplateVariablesFor')->with(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE, array('goToUrl' => $goToUrl))->will($this->returnValue(array_merge(array('goToUrl' => $goToUrl), $templateVars))); |
||||||||
60 | $notifier = new AzineNotifierService($mocks['mailer'], $mocks['twig'], $mocks['router'], $mocks['managerRegistry'], $mocks['templateProvider'], $mocks['recipientProvider'], $mocks['translator'], $mocks['parameters']); |
||||||||
61 | |||||||||
62 | $notifier->addNotificationMessage('12', 'some title', "some content with \nline breaks.", $goToUrl); |
||||||||
63 | |||||||||
64 | $mocks = $this->getMockSetup(); |
||||||||
65 | $mocks['entityManager']->expects($this->once())->method('persist'); |
||||||||
66 | $mocks['templateProvider']->expects($this->once())->method('addTemplateVariablesFor')->with(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE, array())->will($this->returnValue($templateVars)); |
||||||||
67 | $notifier = new AzineNotifierService($mocks['mailer'], $mocks['twig'], $mocks['router'], $mocks['managerRegistry'], $mocks['templateProvider'], $mocks['recipientProvider'], $mocks['translator'], $mocks['parameters']); |
||||||||
68 | |||||||||
69 | $notifier->addNotificationMessage('12', 'some title', "some content with \nline breaks."); |
||||||||
70 | } |
||||||||
71 | |||||||||
72 | public function testSendNewsletter() |
||||||||
73 | { |
||||||||
74 | $failedAddresses = array(); |
||||||||
75 | $recipientIds = array(11, 12, 13, 14); |
||||||||
76 | $mocks = $this->getMockSetup(); |
||||||||
77 | $this->mockRecipients($mocks['recipientProvider'], $recipientIds); |
||||||||
78 | $mocks['recipientProvider']->expects($this->once())->method('getNewsletterRecipientIDs')->will($this->returnValue($recipientIds)); |
||||||||
79 | $mocks['mailer']->expects($this->exactly(sizeof($recipientIds)))->method('sendSingleEmail')->will($this->returnCallback(array($this, 'sendSingleEmailCallBack'))); |
||||||||
80 | |||||||||
81 | $mocks['mailer']->expects($this->exactly(sizeof($recipientIds)))->method('sendSingleEmail'); |
||||||||
82 | |||||||||
83 | $notifier = new ExampleNotifierService($mocks['mailer'], $mocks['twig'], $mocks['router'], $mocks['managerRegistry'], $mocks['templateProvider'], $mocks['recipientProvider'], $mocks['translator'], $mocks['parameters']); |
||||||||
84 | $sentMails = $notifier->sendNewsletter($failedAddresses); |
||||||||
85 | $this->assertSame(1, sizeof($failedAddresses)); |
||||||||
86 | $this->assertSame(sizeof($recipientIds) - 1, $sentMails); |
||||||||
87 | } |
||||||||
88 | |||||||||
89 | public function sendSingleEmailCallBack($email, $displayName, $params, $wrapperTemplate, $locale) |
||||||||
0 ignored issues
–
show
The parameter
$displayName is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$locale is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$params is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||||
90 | { |
||||||||
91 | if ('[email protected]' == $email) { |
||||||||
92 | return false; |
||||||||
93 | } |
||||||||
94 | |||||||||
95 | return true; |
||||||||
96 | } |
||||||||
97 | |||||||||
98 | public function testSendNewsletter_NoContent() |
||||||||
99 | { |
||||||||
100 | $failedAddresses = array(); |
||||||||
101 | $recipientIds = array(11, 12, 13, 14); |
||||||||
102 | $mocks = $this->getMockSetup(); |
||||||||
103 | $this->mockRecipients($mocks['recipientProvider'], $recipientIds); |
||||||||
104 | $mocks['recipientProvider']->expects($this->once())->method('getNewsletterRecipientIDs')->will($this->returnValue($recipientIds)); |
||||||||
105 | |||||||||
106 | $mocks['mailer']->expects($this->never())->method('sendSingleEmail'); |
||||||||
107 | |||||||||
108 | $notifier = new AzineNotifierService($mocks['mailer'], $mocks['twig'], $mocks['router'], $mocks['managerRegistry'], $mocks['templateProvider'], $mocks['recipientProvider'], $mocks['translator'], $mocks['parameters']); |
||||||||
109 | $sentMails = $notifier->sendNewsletter($failedAddresses); |
||||||||
110 | $this->assertSame(4, sizeof($failedAddresses), 'Email-addresses failed unexpectedly:'.print_r($failedAddresses, true)); |
||||||||
111 | $this->assertSame(0, $sentMails, 'Not the expected number of sent emails.'); |
||||||||
112 | } |
||||||||
113 | |||||||||
114 | public function testSendNotificationsAzineNotifierService() |
||||||||
115 | { |
||||||||
116 | $failedAddresses = array(); |
||||||||
117 | $recipientIds = array(11, 12, 13, 14); |
||||||||
118 | $mocks = $this->getMockSetup(); |
||||||||
119 | $this->mockRecipients($mocks['recipientProvider'], $recipientIds); |
||||||||
120 | $mocks['mailer']->expects($this->exactly(sizeof($recipientIds)))->method('sendSingleEmail')->will($this->returnCallback(array($this, 'sendSingleEmailCallBack'))); |
||||||||
121 | |||||||||
122 | $notification = new Notification(); |
||||||||
123 | $notification->setContent('bla bla'); |
||||||||
124 | $notification->setCreated(new \DateTime()); |
||||||||
125 | $notification->setImportance(0); |
||||||||
126 | $notification->setTemplate(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE); |
||||||||
127 | $notification->setVariables(array('blabla' => 'blablaValue')); |
||||||||
128 | $notification->setTitle('a title'); |
||||||||
129 | |||||||||
130 | $mocks['notificationRepository']->expects($this->once())->method('getNotificationRecipientIds')->will($this->returnValue($recipientIds)); |
||||||||
131 | $mocks['notificationRepository']->expects($this->exactly(4))->method('getNotificationsToSend')->will($this->returnValue(array($notification))); |
||||||||
132 | $mocks['notificationRepository']->expects($this->never())->method('getNotificationsToSendImmediately'); |
||||||||
133 | $mocks['notificationRepository']->expects($this->never())->method('markAllNotificationsAsSentFarInThePast'); |
||||||||
134 | $mocks['notificationRepository']->expects($this->exactly(4))->method('getLastNotificationDate')->will($this->returnValue(new \DateTime('@0'))); |
||||||||
135 | |||||||||
136 | $notifier = new AzineNotifierService($mocks['mailer'], $mocks['twig'], $mocks['router'], $mocks['managerRegistry'], $mocks['templateProvider'], $mocks['recipientProvider'], $mocks['translator'], $mocks['parameters']); |
||||||||
137 | $sentMails = $notifier->sendNotifications($failedAddresses); |
||||||||
138 | $this->assertSame(1, sizeof($failedAddresses), 'One failed address was expected.'); |
||||||||
139 | $sentMailCount = count($recipientIds) - count($failedAddresses); |
||||||||
140 | $this->assertSame($sentMailCount, $sentMails, "Not the right number of emails has been sent successfully. Expected $sentMailCount"); |
||||||||
141 | } |
||||||||
142 | |||||||||
143 | public function testSendNotificationsAzineNotifierService_NoNotifications() |
||||||||
144 | { |
||||||||
145 | $failedAddresses = array(); |
||||||||
146 | $recipientIds = array(11, 12, 13, 14); |
||||||||
147 | $mocks = $this->getMockSetup(); |
||||||||
148 | $this->mockRecipients($mocks['recipientProvider'], $recipientIds); |
||||||||
149 | $mocks['mailer']->expects($this->never())->method('sendSingleEmail')->will($this->returnCallback(array($this, 'sendSingleEmailCallBack'))); |
||||||||
150 | |||||||||
151 | $mocks['notificationRepository']->expects($this->once())->method('getNotificationRecipientIds')->will($this->returnValue($recipientIds)); |
||||||||
152 | $mocks['notificationRepository']->expects($this->exactly(4))->method('getNotificationsToSend')->will($this->returnValue(array())); |
||||||||
153 | $mocks['notificationRepository']->expects($this->never())->method('getNotificationsToSendImmediately'); |
||||||||
154 | $mocks['notificationRepository']->expects($this->never())->method('markAllNotificationsAsSentFarInThePast'); |
||||||||
155 | $mocks['notificationRepository']->expects($this->exactly(4))->method('getLastNotificationDate')->will($this->returnValue(new \DateTime('@0'))); |
||||||||
156 | |||||||||
157 | $notifier = new AzineNotifierService($mocks['mailer'], $mocks['twig'], $mocks['router'], $mocks['managerRegistry'], $mocks['templateProvider'], $mocks['recipientProvider'], $mocks['translator'], $mocks['parameters']); |
||||||||
158 | $sentMails = $notifier->sendNotifications($failedAddresses); |
||||||||
159 | $this->assertSame(0, sizeof($failedAddresses), 'Email-addresses failed unexpectedly:'.print_r($failedAddresses, true)); |
||||||||
160 | $this->assertSame(4, $sentMails, 'Not the expected number of sent emails.'); |
||||||||
161 | } |
||||||||
162 | |||||||||
163 | public function testSendNotificationsExampleNotifier() |
||||||||
164 | { |
||||||||
165 | $failedAddresses = array(); |
||||||||
166 | $recipientIds = array(11, 12, 13, 14); |
||||||||
167 | $mocks = $this->getMockSetup(); |
||||||||
168 | $this->mockRecipients($mocks['recipientProvider'], $recipientIds); |
||||||||
169 | $mocks['mailer']->expects($this->exactly(sizeof($recipientIds)))->method('sendSingleEmail')->will($this->returnCallback(array($this, 'sendSingleEmailCallBack'))); |
||||||||
170 | |||||||||
171 | $notification = new Notification(); |
||||||||
172 | $notification->setContent('bla bla'); |
||||||||
173 | $notification->setCreated(new \DateTime()); |
||||||||
174 | $notification->setImportance(0); |
||||||||
175 | $notification->setTemplate(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE); |
||||||||
176 | $notification->setVariables(array('blabla' => 'blablaValue')); |
||||||||
177 | $notification->setTitle('a title'); |
||||||||
178 | |||||||||
179 | $mocks['notificationRepository']->expects($this->once())->method('getNotificationRecipientIds')->will($this->returnValue($recipientIds)); |
||||||||
180 | $mocks['notificationRepository']->expects($this->exactly(4))->method('getNotificationsToSend')->will($this->returnValue(array($notification))); |
||||||||
181 | $mocks['notificationRepository']->expects($this->never())->method('getNotificationsToSendImmediately'); |
||||||||
182 | $mocks['notificationRepository']->expects($this->never())->method('markAllNotificationsAsSentFarInThePast'); |
||||||||
183 | $mocks['notificationRepository']->expects($this->exactly(4))->method('getLastNotificationDate')->will($this->returnValue(new \DateTime('@0'))); |
||||||||
184 | |||||||||
185 | $mocks['mailer']->expects($this->exactly(sizeof($recipientIds)))->method('sendSingleEmail'); |
||||||||
186 | |||||||||
187 | $notifier = new ExampleNotifierService($mocks['mailer'], $mocks['twig'], $mocks['router'], $mocks['managerRegistry'], $mocks['templateProvider'], $mocks['recipientProvider'], $mocks['translator'], $mocks['parameters']); |
||||||||
188 | $sentMails = $notifier->sendNotifications($failedAddresses); |
||||||||
189 | $this->assertSame(1, sizeof($failedAddresses)); |
||||||||
190 | $this->assertSame(sizeof($recipientIds) - 1, $sentMails); |
||||||||
191 | } |
||||||||
192 | |||||||||
193 | private function mockRecipients(\PHPUnit_Framework_MockObject_MockObject $mock, array $ids) |
||||||||
194 | { |
||||||||
195 | $notificationType = 0; |
||||||||
196 | $notificationTypes = array(RecipientInterface::NOTIFICATION_MODE_IMMEDIATELY, RecipientInterface::NOTIFICATION_MODE_HOURLY, RecipientInterface::NOTIFICATION_MODE_DAYLY); |
||||||||
197 | $valueMap = array(); |
||||||||
198 | foreach ($ids as $id) { |
||||||||
199 | $recipientMock = $this->getMockBuilder("Azine\EmailBundle\Entity\RecipientInterface")->disableOriginalConstructor()->getMock(); |
||||||||
200 | $recipientMock->expects($this->any())->method('getEmail')->will($this->returnValue($id.'[email protected]')); |
||||||||
201 | $recipientMock->expects($this->any())->method('getDisplayName')->will($this->returnValue("DisplayName of $id")); |
||||||||
202 | $recipientMock->expects($this->any())->method('getPreferredLocale')->will($this->returnValue('en')); |
||||||||
203 | $recipientMock->expects($this->any())->method('getNotificationMode')->will($this->returnValue($notificationTypes[$notificationType % sizeof($notificationTypes)])); |
||||||||
204 | ++$notificationType; |
||||||||
205 | $valueMap[] = array($id, $recipientMock); |
||||||||
206 | } |
||||||||
207 | |||||||||
208 | $mock->expects($this->exactly(sizeof($ids)))->method('getRecipient')->will($this->returnValueMap($valueMap)); |
||||||||
209 | } |
||||||||
210 | |||||||||
211 | public function testProtectedMethods() |
||||||||
212 | { |
||||||||
213 | // create service-instance |
||||||||
214 | $mocks = $this->getMockSetup(); |
||||||||
215 | |||||||||
216 | $recipientIds = array(11, 12, 13, 14); |
||||||||
217 | $mocks['recipientProvider']->expects($this->once())->method('getNewsletterRecipientIDs')->will($this->returnValue($recipientIds)); |
||||||||
218 | |||||||||
219 | $notifier = new AzineNotifierService($mocks['mailer'], $mocks['twig'], $mocks['router'], $mocks['managerRegistry'], $mocks['templateProvider'], $mocks['recipientProvider'], $mocks['translator'], $mocks['parameters']); |
||||||||
220 | |||||||||
221 | // access the protected method and execute it |
||||||||
222 | $returnValue = self::getMethod('getDateTimeOfLastNewsletter')->invokeArgs($notifier, array()); |
||||||||
223 | $this->assertInstanceOf('DateTime', $returnValue); |
||||||||
224 | $lastDate = new \DateTime('7 days ago'); |
||||||||
225 | $lastDate->setTime(9, 0); |
||||||||
226 | $this->assertSame($lastDate->getTimestamp(), $returnValue->getTimestamp()); |
||||||||
227 | |||||||||
228 | $returnValue = self::getMethod('getDateTimeOfNextNewsletter')->invokeArgs($notifier, array()); |
||||||||
229 | $this->assertInstanceOf('DateTime', $returnValue); |
||||||||
230 | $nextDate = new \DateTime('7 days'); |
||||||||
231 | $nextDate->setTime(9, 0); |
||||||||
232 | $this->assertSame($nextDate->getTimestamp(), $returnValue->getTimestamp()); |
||||||||
233 | |||||||||
234 | $returnValue = self::getMethod('getHourInterval')->invokeArgs($notifier, array()); |
||||||||
235 | $this->assertSame((60 * 60 - 3 * 60), $returnValue); |
||||||||
236 | |||||||||
237 | $returnValue = self::getMethod('getGeneralVarsForNewsletter')->invokeArgs($notifier, array()); |
||||||||
238 | $this->assertSame(sizeof($recipientIds), $returnValue['recipientCount']); |
||||||||
239 | |||||||||
240 | $recipientMock = $this->getMockBuilder("Azine\EmailBundle\Entity\RecipientInterface")->disableOriginalConstructor()->getMock(); |
||||||||
241 | $recipientMock->expects($this->any())->method('getId')->will($this->returnValue(11)); |
||||||||
242 | |||||||||
243 | $returnValue = self::getMethod('markAllNotificationsAsSentFarInThePast')->invokeArgs($notifier, array($recipientMock)); |
||||||||
0 ignored issues
–
show
|
|||||||||
244 | } |
||||||||
245 | |||||||||
246 | /** |
||||||||
247 | * @param string $name |
||||||||
248 | */ |
||||||||
249 | private static function getMethod($name) |
||||||||
250 | { |
||||||||
251 | $class = new \ReflectionClass("Azine\EmailBundle\Services\AzineNotifierService"); |
||||||||
252 | $method = $class->getMethod($name); |
||||||||
253 | $method->setAccessible(true); |
||||||||
254 | |||||||||
255 | return $method; |
||||||||
256 | } |
||||||||
257 | } |
||||||||
258 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.