Exemplo n.º 1
0
 public function testSetCreated()
 {
     $n = new Notification();
     $c1 = $n->getCreated();
     $n->setCreatedValue();
     $c2 = $n->getCreated();
     sleep(1);
     $this->assertNull($c1);
     $this->assertNotNull($c2);
     $this->assertGreaterThanOrEqual($c2, new \DateTime());
 }
 /**
  * Convenience-function to add and save a Notification-entity
  *
  * @param  integer          $recipientId     the ID of the recipient of this notification => see RecipientProvider.getRecipient($id)
  * @param  string           $title           the title of the notification. depending on the recipients settings, multiple notifications are sent in one email.
  * @param  string           $content         the content of the notification
  * @param  NotificationType $type        the twig-template to render the notification with
  * @param  array            $templateVars    the parameters used in the twig-template, 'notification' => Notification and 'recipient' => RecipientInterface will be added to this array when rendering the twig-template.
  * @param  integer          $importance      important messages are at the top of the notification-emails, un-important at the bottom.
  * @param  boolean          $sendImmediately whether or not to ignore the recipients mailing-preference and send the notification a.s.a.p.
  * @return Notification
  */
 public function addNotification($recipientId, $title, $content, $type, $templateVars, $importance, $sendImmediately)
 {
     $notification = new Notification();
     $notification->setRecipientId($recipientId);
     $notification->setTitle($title);
     $notification->setContent($content);
     $notification->setType($type);
     $notification->setImportance($importance);
     $notification->setSendImmediately($sendImmediately);
     $notification->setVariables($templateVars);
     $this->em->persist($notification);
     $this->em->flush($notification);
     return $notification;
 }
 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);
 }