示例#1
0
 /**
  * Run receiver instance
  *
  * @throws \RuntimeException
  * @throws InvalidMessageException
  */
 public function runReceiver()
 {
     if (null === $this->adapter) {
         throw new \RuntimeException('Can\'t run receiver. Adapter not found.');
     }
     if (null === $this->notification) {
         throw new \RuntimeException('Can\'t run receiver. Notification not found.');
     }
     while ($this->adapter->isNextReceive()) {
         if ($message = $this->adapter->getMessage()) {
             if (!$message instanceof MessageInterface) {
                 throw new InvalidMessageException(sprintf('Message must be instance MessageInterface, "%s" given.', is_object($message) ? get_class($message) : gettype($message)));
             }
             try {
                 $this->notification->send($message);
             } catch (SendException $e) {
                 if ($this->notificationErrorHandler) {
                     call_user_func($this->notificationErrorHandler, $e);
                 }
             }
         }
     }
 }
示例#2
0
 /**
  * Test run receiver and control control notification error
  */
 public function testRunReceiverControlNotificationError()
 {
     $iterations = 0;
     $existsControlError = false;
     $this->adapter->expects($this->any())->method('isNextReceive')->with()->will($this->returnCallback(function () use(&$iterations) {
         if ($iterations > 0) {
             return false;
         }
         $iterations++;
         return true;
     }));
     $message = new Message();
     $this->adapter->expects($this->any())->method('getMessage')->with()->will($this->returnValue($message));
     $this->notification->expects($this->any())->method('send')->with($message)->will($this->returnCallback(function () {
         throw new SendException(SendException::ERROR_UNPACK_RESPONSE, null, null);
     }));
     $controlError = function (SendException $error) use(&$existsControlError) {
         $existsControlError = true;
     };
     $queue = new Queue($this->adapter, $this->notification);
     $queue->setNotificationErrorHandler($controlError);
     $queue->runReceiver();
     $this->assertTrue($existsControlError, 'Not call to callback for control notification error.');
 }