示例#1
0
 public function it_should_configure_connection(Connection $connection, Channel $channel, Exchange $exchange, Queue $queue)
 {
     $connection->connect()->shouldBeCalled();
     $channel->isInitialized()->willReturn(false);
     $channel->initialize()->shouldBeCalled();
     $exchange->isInitialized()->willReturn(false);
     $exchange->initialize()->shouldBeCalled();
     $queue->isInitialized()->willReturn(false);
     $queue->initialize()->shouldBeCalled();
     $this->initialize();
 }
示例#2
0
 public function it_should_initialize(\AMQPQueue $queue, Exchange $exchange, Context $context)
 {
     $exchange->getName()->willReturn('exchange');
     $context->getFlags()->willReturn(1234);
     $context->getArguments()->willReturn(['foo' => 'bar']);
     $queue->setName('queue')->shouldBeCalled();
     $queue->bind('exchange')->shouldBeCalled();
     $queue->setFlags(1234)->shouldBeCalled();
     $queue->setArguments(['foo' => 'bar'])->shouldBeCalled();
     $queue->declareQueue()->shouldBeCalled();
     $this->initialize($queue);
     $this->isInitialized()->shouldReturn(true);
 }
示例#3
0
 public function initialize()
 {
     $this->connection->connect();
     if (false === $this->channel->isInitialized()) {
         $this->channel->initialize();
     }
     if (false === $this->exchange->isInitialized()) {
         $this->exchange->initialize();
     }
     if (false === $this->queue->isInitialized()) {
         $this->queue->initialize();
     }
     $this->initialized = true;
 }
示例#4
0
 public function it_should_check_if_contain_exchange(Exchange $a, Exchange $b, Channel $channel)
 {
     $a->getFingerPrint()->willReturn('a');
     $b->getFingerPrint()->willReturn('b');
     $this->addExchange($a);
     $this->addExchange($b);
     $a->isEqual('b', $channel)->willReturn(false);
     $a->isEqual('a', $channel)->willReturn(true);
     $a->isEqual('c', $channel)->willReturn(false);
     $b->isEqual('b', $channel)->willReturn(true);
     $b->isEqual('a', $channel)->willReturn(false);
     $b->isEqual('c', $channel)->willReturn(false);
     $this->hasExchange('a', $channel)->shouldReturn(true);
     $this->hasExchange('b', $channel)->shouldReturn(true);
     $this->hasExchange('c', $channel)->shouldReturn(false);
 }
示例#5
0
文件: Queue.php 项目: Evaneos/Hector
 /**
  * @param \AMQPQueue|null $queue
  *
  * @throws HectorException
  */
 public function initialize(\AMQPQueue $queue = null)
 {
     if (true === $this->isInitialized()) {
         throw new HectorException('Queue already initialized');
     }
     if (null === $queue) {
         $queue = new \AMQPQueue($this->channel->getWrappedChannel());
     }
     $this->queue = $queue;
     $this->queue->setName($this->getName());
     $this->queue->bind($this->exchange->getName());
     $this->queue->setFlags($this->context->getFlags());
     $this->queue->setArguments($this->context->getArguments());
     $this->queue->declareQueue();
     $this->initialized = true;
 }
示例#6
0
 /**
  * @param string $message
  * @param null   $routingKeyArgument::type(Publisher::class)
  * @param int    $flags
  * @param array  $attributes
  *
  * @throws \Exception
  *
  * @return bool
  */
 public function publish($message, $routingKey = null, $flags = AMQP_NOPARAM, array $attributes = [])
 {
     $hasEventDispatcher = null !== $this->eventDispatcher;
     if (!empty($this->routingKeyPrefix)) {
         $routingKey = $this->routingKeyPrefix . $routingKey;
     }
     if (!$this->isInitialized()) {
         $this->initialize();
     }
     if (true === $hasEventDispatcher) {
         $event = new PublisherEvent($message, $routingKey, $flags, $attributes, $this->exchange);
     }
     try {
         if (true === $hasEventDispatcher) {
             $this->eventDispatcher->dispatch(PublisherEvents::PRE_PUBLISH, $event);
             $result = $this->exchange->getWrappedExchange()->publish($event->getMessage(), $event->getRoutingKey(), $event->getFlags(), $event->getAttributes());
         } else {
             $result = $this->exchange->getWrappedExchange()->publish($message, $routingKey, $flags, $attributes);
         }
         if (!$result) {
             if (true === $hasEventDispatcher) {
                 $this->eventDispatcher->dispatch(PublisherEvents::FAIL_PUBLISH, new FailedPublisherEvent($event, null, $this));
             }
         } else {
             if (true === $hasEventDispatcher) {
                 $this->eventDispatcher->dispatch(PublisherEvents::SUCCESS_PUBLISH, new SuccessPublisherEvent($event));
             }
         }
     } catch (\Exception $e) {
         if (true === $hasEventDispatcher) {
             $this->eventDispatcher->dispatch(PublisherEvents::FAIL_PUBLISH, new FailedPublisherEvent($event, $e, $this));
         }
         throw $e;
     }
     return $result;
 }
示例#7
0
 public function it_should_publish_message_with_routing_key_null(EventDispatcher $eventDispatcher, Exchange $exchange, Channel $channel, \AMQPExchange $AMQPExchange, Connection $connection, Identity $identity)
 {
     $message = 'foo.bar';
     $routingKey = null;
     $flags = AMQP_NOPARAM;
     $attributes = ['x-expires' => 1000];
     $event = new PublisherEvent($message, $routingKey, $flags, $attributes, $exchange->getWrappedObject());
     $successEvent = new SuccessPublisherEvent($event);
     $connection->connect()->shouldBeCalled();
     $channel->isInitialized()->willReturn(true);
     $exchange->isInitialized()->willReturn(true);
     $eventDispatcher->dispatch(PublisherEvents::PRE_PUBLISH, $event)->shouldBeCalled();
     $AMQPExchange->publish($message, $routingKey, $flags, $attributes)->willReturn(true);
     $exchange->getWrappedExchange()->willReturn($AMQPExchange);
     $eventDispatcher->dispatch(PublisherEvents::SUCCESS_PUBLISH, $successEvent)->shouldBeCalled();
     $this->publish($message, $routingKey, $flags, $attributes, false)->shouldReturn(true);
 }
示例#8
0
 public function it_should_throw_exception_if_no_queue(Channel $channel, Exchange $exchange)
 {
     $channel->getIdentity()->willReturn('bar');
     $exchange->getName()->willReturn('baz');
     $this->shouldThrow(new NotFoundException('Unable to find queue foo for channel bar and exchange baz'))->during('getQueue', ['foo', $channel, $exchange]);
 }
示例#9
0
 /**
  * @param Exchange $exchange
  */
 public function addExchange(Exchange $exchange)
 {
     $this->exchanges[$exchange->getFingerPrint()] = $exchange;
 }