/**
  * @test
  */
 public function it_deserialize_the_message_before_emitting()
 {
     $stream = new DomainEventStream([new DomainMessage('a', 0, new Metadata(), [], DateTime::now())]);
     $consumer = new EventBusConsumer($this->serializer, $this->eventBus);
     $this->serializer->shouldReceive('deserialize')->with("[]")->andReturn($stream);
     $this->eventBus->shouldReceive('publish')->with($stream)->once();
     $consumer->consume("[]");
 }
 /**
  * @test
  */
 public function itShouldRollbackAfterPublishingFailed()
 {
     $stream = \Mockery::mock(DomainEventStreamInterface::class);
     $this->transactionManager->shouldReceive('beginTransaction')->once();
     $this->transactionManager->shouldReceive('rollback')->once();
     $this->eventBus->shouldReceive('publish')->with($stream)->andThrow(\Exception::class);
     $this->setExpectedException(\Exception::class);
     $eventBus = new TransactionalEventBus($this->eventBus, $this->transactionManager);
     $eventBus->publish($stream);
 }
 /**
  * Publishes the events from the domain event stream to the listeners.
  *
  * @param DomainEventStreamInterface $domainMessages
  * @throws \Exception
  */
 public function publish(DomainEventStreamInterface $domainMessages)
 {
     $this->transactionManager->beginTransaction();
     try {
         $this->eventBus->publish($domainMessages);
         $this->transactionManager->commit();
     } catch (\Exception $e) {
         $this->transactionManager->rollback();
         throw $e;
     }
 }
 /**
  * @test
  */
 public function handleShouldPublishADomainMessage()
 {
     $error = \Mockery::mock(EventInterface::class);
     $this->eventBus->shouldReceive('publish')->with(\Mockery::on(function (DomainEventStream $eventStream) use($error) {
         $this->assertInstanceOf(DomainEventStream::class, $eventStream);
         $streamIterator = $eventStream->getIterator();
         $this->assertEquals(1, count($streamIterator));
         $message = $streamIterator[0];
         $this->assertEquals($error, $message->getPayload());
         return true;
     }))->once();
     $handler = new DomainMessageErrorEventHandler($this->eventBus);
     $handler->handle($error);
 }
 /**
  * @param string $exchangeName
  * @param string $queueName
  */
 public function listen($queueName)
 {
     $adapter = $this->queueFactory->create($queueName);
     $eventBus = $this->eventBus;
     $callback = function ($msg) use($eventBus, $adapter) {
         if ($msg->body == 'finished') {
             $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
             $adapter->delete();
             exit;
         }
         $this->eventBus->publish($this->getDomainEventStream($msg->body));
     };
     $adapter->listen($callback);
 }
 /**
  * @test
  */
 public function it_can_publish_the_message_on_the_event_bus()
 {
     $context = [];
     $context['correlation_id'] = 'my-correlation-id-123';
     $expectedMetadata = new Metadata($context);
     $expectedPayload = '';
     $this->eventBus->expects($this->once())->method('publish')->with($this->callback(function ($domainEventStream) use($expectedMetadata, $expectedPayload) {
         /** @var DomainEventStream $domainEventStream */
         $iterator = $domainEventStream->getIterator();
         $domainMessage = $iterator->offsetGet(0);
         $actualMetadata = $domainMessage->getMetadata();
         $actualPayload = $domainMessage->getPayload();
         if ($actualMetadata == $expectedMetadata && $actualPayload == $expectedPayload) {
             return true;
         } else {
             return false;
         }
     }));
     $this->deserializerLocator->expects($this->once())->method('getDeserializerForContentType')->with(new StringLiteral('application/vnd.cultuurnet.udb3-events.dummy-event+json'))->willReturn($this->deserializer);
     $this->deserializer->expects($this->once())->method('deserialize')->with(new StringLiteral(''))->willReturn('');
     $this->channel->expects($this->once())->method('basic_ack')->with('my-delivery-tag');
     $messageProperties = ['content_type' => 'application/vnd.cultuurnet.udb3-events.dummy-event+json', 'correlation_id' => 'my-correlation-id-123'];
     $messageBody = '';
     $message = new AMQPMessage($messageBody, $messageProperties);
     $message->delivery_info['channel'] = $this->channel;
     $message->delivery_info['delivery_tag'] = 'my-delivery-tag';
     $this->eventBusForwardingConsumer->consume($message);
 }
 /**
  * @param AMQPMessage $message
  */
 public function consume(AMQPMessage $message)
 {
     $context = [];
     if ($message->has('correlation_id')) {
         $context['correlation_id'] = $message->get('correlation_id');
     }
     if ($this->logger) {
         $this->logger->info('received message with content-type ' . $message->get('content_type'), $context);
     }
     $contentType = new StringLiteral($message->get('content_type'));
     try {
         $deserializer = $this->deserializerLocator->getDeserializerForContentType($contentType);
         $domainMessage = $deserializer->deserialize(new StringLiteral($message->body));
         // If the deserializer did not return a DomainMessage yet, then
         // consider the returned value as the payload, and wrap it in a
         // DomainMessage.
         if (!$domainMessage instanceof DomainMessage) {
             $domainMessage = new DomainMessage(UUID::generateAsString(), 0, new Metadata($context), $domainMessage, DateTime::now());
         }
         $this->delayIfNecessary();
         if ($this->logger) {
             $this->logger->info('passing on message to event bus', $context);
         }
         $this->eventBus->publish(new DomainEventStream([$domainMessage]));
     } catch (\Exception $e) {
         if ($this->logger) {
             $this->logger->error($e->getMessage(), $context + ['exception' => $e]);
         }
         $message->delivery_info['channel']->basic_reject($message->delivery_info['delivery_tag'], false);
         if ($this->logger) {
             $this->logger->info('message rejected', $context);
         }
         return;
     }
     $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
     if ($this->logger) {
         $this->logger->info('message acknowledged', $context);
     }
 }
 /**
  * Handles an error
  *
  * @param  EventInterface $error
  * @param  mixed          $context
  *
  * @return void
  */
 public function handle(EventInterface $error, $context = null)
 {
     $this->eventBus->publish(new DomainEventStream([DomainMessage::recordNow(null, null, new Metadata([ContextEnricher::CONTEXT => $context]), $error)]));
 }
 private function publishEvents()
 {
     $this->eventBus->publish(new DomainEventStream($this->eventBuffer));
     $this->clearEventBuffer();
 }
 /**
  * Consumes a message
  *
  * @param  string $message
  * @return string|null|void
  */
 public function consume($message)
 {
     $eventStream = $this->serializer->deserialize($message);
     $this->eventBus->publish($eventStream);
 }
 /**
  * @return void
  */
 protected function publishEvents()
 {
     $this->eventBus->publish(new DomainEventStream($this->eventBuffer));
     $this->eventBuffer = [];
 }
 /**
  * @param object $payload
  * @param Metadata $metadata
  */
 private function publish($payload, Metadata $metadata)
 {
     $message = new DomainMessage(UUID::generateAsString(), 1, $metadata, $payload, DateTime::now());
     $domainEventStream = new DomainEventStream([$message]);
     $this->eventBus->publish($domainEventStream);
 }