/** * @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 it_should_throw_the_exception_if_it_catches_one() { $stream = new DomainEventStream([new DomainMessage('a', 0, new Metadata(), [], DateTime::now())]); $this->serializer->shouldReceive('serialize')->with($stream)->andReturn('serialized'); $this->queuePublisher->shouldReceive('publish')->andThrow(\Exception::class); $this->setExpectedException(\Exception::class); $eventBus = new QueuePublishingEventBus($this->serializer, $this->queuePublisher); $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->queue[] = $domainMessages; if (!$this->isPublishing) { $this->isPublishing = true; try { parent::publish($domainMessages); while ($domainMessageStream = array_shift($this->queue)) { $serializedDomainMessages = $this->serializer->serialize($domainMessages); $this->queuePublisher->publish($serializedDomainMessages); } } catch (\Exception $e) { throw $e; } finally { $this->isPublishing = false; } } }
/** * Consumes a message * * @param string $message * @return string|null|void */ public function consume($message) { $eventStream = $this->serializer->deserialize($message); $this->eventBus->publish($eventStream); }