/**
  * @test
  */
 public function it_sends_an_event_to_queue_pulls_it_with_consumer_and_forwards_it_to_event_bus()
 {
     $event = new SomethingDone(['data' => 'test event']);
     //The message dispatcher works with a ready-to-use bernard producer and one queue
     $messageProducer = new BernardMessageProducer($this->bernardProducer, 'test-queue');
     //Normally you would send the event on a event bus. We skip this step here cause we are only
     //interested in the function of the message dispatcher
     $messageProducer($event);
     //Set up event bus which will receive the event message from the bernard consumer
     $consumerEventBus = new EventBus();
     $somethingDoneListener = new MessageHandler();
     $consumerEventBus->utilize(new EventRouter([$event->messageName() => [$somethingDoneListener]]));
     //We use a special bernard router which forwards all messages to a command bus or event bus depending on the
     //Prooph\ServiceBus\Message\MessageHeader::TYPE
     $bernardRouter = new BernardRouter(new CommandBus(), $consumerEventBus);
     $bernardConsumer = new Consumer($bernardRouter, new EventDispatcher());
     //We use the same queue name here as we've defined for the message dispatcher above
     $bernardConsumer->tick($this->persistentFactory->create('test-queue'));
     $this->assertNotNull($somethingDoneListener->getLastMessage());
     $this->assertEquals($event->payload(), $somethingDoneListener->getLastMessage()->payload());
 }
Esempio n. 2
0
 /**
  * @test
  */
 public function it_invokes_all_listeners()
 {
     $handler = new MessageHandler();
     $this->eventBus->getActionEventEmitter()->attachListener(MessageBus::EVENT_ROUTE, function (ActionEvent $e) use($handler) {
         if ($e->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME) === CustomMessage::class) {
             $e->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, [$handler, $handler]);
         }
     });
     $customMessage = new CustomMessage("foo");
     $this->eventBus->dispatch($customMessage);
     $this->assertSame($customMessage, $handler->getLastMessage());
     $this->assertEquals(2, $handler->getInvokeCounter());
 }