public function testSendNotificationsExampleNotifier()
 {
     $failedAddresses = array();
     $recipientIds = array(11, 12, 13, 14);
     $mocks = $this->getMockSetup();
     $this->mockRecipients($mocks['recipientProvider'], $recipientIds);
     $mocks['mailer']->expects($this->exactly(sizeof($recipientIds)))->method("sendSingleEmail")->will($this->returnCallback(array($this, 'sendSingleEmailCallBack')));
     $notification = new Notification();
     $notification->setContent("bla bla");
     $notification->setCreated(new \DateTime());
     $notification->setImportance(0);
     $notification->setTemplate(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE);
     $notification->setVariables(array('blabla' => 'blablaValue'));
     $notification->setTitle("a title");
     $notificationsQueryBuilderMock = $this->getMockBuilder("Doctrine\\ORM\\QueryBuilder")->disableOriginalConstructor()->getMock();
     $notificationsQueryBuilderMock->expects($this->exactly(4))->method("getQuery")->will($this->returnValue(new AzineQueryMock(array($notification))));
     $notificationsQueryBuilderMock->expects($this->any())->method("from")->will($this->returnSelf());
     $notificationsQueryBuilderMock->expects($this->any())->method("andWhere")->will($this->returnSelf());
     $notificationsQueryBuilderMock->expects($this->any())->method("setParameter")->will($this->returnSelf());
     $notificationsQueryBuilderMock->expects($this->any())->method("orderBy")->will($this->returnSelf());
     $notificationsRecipientQueryBuilderMock = $this->getMockBuilder("Doctrine\\ORM\\QueryBuilder")->disableOriginalConstructor()->getMock();
     $recipientQueryResult = array(array('recipient_id' => 11), array('recipient_id' => 12), array('recipient_id' => 13), array('recipient_id' => 14));
     $notificationsRecipientQueryBuilderMock->expects($this->once())->method("getQuery")->will($this->returnValue(new AzineQueryMock($recipientQueryResult)));
     $notificationsRecipientQueryBuilderMock->expects($this->any())->method("from")->will($this->returnSelf());
     $notificationsRecipientQueryBuilderMock->expects($this->any())->method("andWhere")->will($this->returnSelf());
     $notificationsRecipientQueryBuilderMock->expects($this->any())->method("distinct")->will($this->returnSelf());
     $maxSentQueryBuilderMock = $this->getMockBuilder("Doctrine\\ORM\\QueryBuilder")->disableOriginalConstructor()->getMock();
     $maxSentQueryResult = array(array(1 => "@0"));
     $maxSentQueryBuilderMock->expects($this->exactly(4))->method("getQuery")->will($this->returnValue(new AzineQueryMock($maxSentQueryResult)));
     $maxSentQueryBuilderMock->expects($this->any())->method("from")->will($this->returnSelf());
     $maxSentQueryBuilderMock->expects($this->any())->method("andWhere")->will($this->returnSelf());
     $maxSentQueryBuilderMock->expects($this->any())->method("setParameter")->will($this->returnSelf());
     $queryBuilderMock = $this->getMockBuilder("Doctrine\\ORM\\QueryBuilder")->disableOriginalConstructor()->getMock();
     $queryBuilderMock->expects($this->any())->method("select")->will($this->returnValueMap(array(array("max(n.sent)", $maxSentQueryBuilderMock), array("n.recipient_id", $notificationsRecipientQueryBuilderMock), array("n", $notificationsQueryBuilderMock))));
     $mocks['entityManager']->expects($this->exactly(9))->method("createQueryBuilder")->will($this->returnValue($queryBuilderMock));
     $mocks['mailer']->expects($this->exactly(sizeof($recipientIds)))->method("sendSingleEmail");
     $mocks['logger']->expects($this->never())->method("warning");
     $mocks['logger']->expects($this->once())->method("error");
     // see sendSingleEmailCallBack, one mail-address fails
     $notifier = new ExampleNotifierService($mocks['mailer'], $mocks['twig'], $mocks['logger'], $mocks['router'], $mocks['entityManager'], $mocks['templateProvider'], $mocks['recipientProvider'], $mocks['translator'], $mocks['parameters']);
     $sentMails = $notifier->sendNotifications($failedAddresses);
     $this->assertEquals(1, sizeof($failedAddresses));
     $this->assertEquals(sizeof($recipientIds) - 1, $sentMails);
 }
 public function testSendNotificationsExampleNotifier()
 {
     $failedAddresses = array();
     $recipientIds = array(11, 12, 13, 14);
     $mocks = $this->getMockSetup();
     $this->mockRecipients($mocks['recipientProvider'], $recipientIds);
     $mocks['mailer']->expects($this->exactly(sizeof($recipientIds)))->method("sendSingleEmail")->will($this->returnCallback(array($this, 'sendSingleEmailCallBack')));
     $notification = new Notification();
     $notification->setContent("bla bla");
     $notification->setCreated(new \DateTime());
     $notification->setImportance(0);
     $notification->setTemplate(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE);
     $notification->setVariables(array('blabla' => 'blablaValue'));
     $notification->setTitle("a title");
     $mocks['notificationRepository']->expects($this->once())->method('getNotificationRecipientIds')->will($this->returnValue($recipientIds));
     $mocks['notificationRepository']->expects($this->exactly(4))->method('getNotificationsToSend')->will($this->returnValue(array($notification)));
     $mocks['notificationRepository']->expects($this->never())->method('getNotificationsToSendImmediately');
     $mocks['notificationRepository']->expects($this->never())->method('markAllNotificationsAsSentFarInThePast');
     $mocks['notificationRepository']->expects($this->exactly(4))->method('getLastNotificationDate')->will($this->returnValue(new \DateTime("@0")));
     $mocks['mailer']->expects($this->exactly(sizeof($recipientIds)))->method("sendSingleEmail");
     $notifier = new ExampleNotifierService($mocks['mailer'], $mocks['twig'], $mocks['router'], $mocks['managerRegistry'], $mocks['templateProvider'], $mocks['recipientProvider'], $mocks['translator'], $mocks['parameters']);
     $sentMails = $notifier->sendNotifications($failedAddresses);
     $this->assertEquals(1, sizeof($failedAddresses));
     $this->assertEquals(sizeof($recipientIds) - 1, $sentMails);
 }