public function testPublishDomainMessage()
 {
     $payload = new stdClass();
     $domainMessage = new DomainMessage('id', 1, new Metadata(array()), $payload, DateTime::now());
     $this->publisher->expects($this->once())->method('publish')->with($this->isInstanceOf(DomainEventStream::class));
     $this->createListener()->handle($domainMessage);
 }
 /**
  * @test
  * @dataProvider contentTypeDataProvider
  *
  * @param mixed $payload
  * @param string $expectedContentType
  */
 public function it_determines_content_type_by_payload_class($payload, $expectedContentType)
 {
     $domainMessage = new DomainMessage('097c36dc-6019-44e2-b6e0-c57d32d8f97c', 0, new Metadata(), $payload, DateTime::now());
     $expectedProperties = ['content_type' => $expectedContentType];
     $actualProperties = $this->contentTypePropertiesFactory->createProperties($domainMessage);
     $this->assertEquals($expectedProperties, $actualProperties);
 }
 public function setUp()
 {
     $this->now = DateTime::now();
     $this->eventStore = $this->createEventStore();
     $this->createAndInsertEventFixtures();
     $this->eventVisitor = new RecordingEventVisitor();
 }
Пример #4
0
 /**
  * @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("[]");
 }
 private function createDomainMessageForEvent($event, DateTime $occurredOn = null) : DomainMessage
 {
     $this->playhead++;
     if (null === $occurredOn) {
         $occurredOn = DateTime::now();
     }
     return new DomainMessage($this->aggregateId, $this->playhead, new Metadata(), $event, $occurredOn);
 }
 /**
  * @test
  */
 public function it_combines_properties_from_all_injected_property_factories()
 {
     $domainMessage = new DomainMessage('7a8ccbc5-d802-46c8-b9ec-7a286bc7653b', 0, new Metadata(), new \stdClass(), DateTime::now());
     $this->mockFactory1->expects($this->once())->method('createProperties')->with($domainMessage)->willReturn(['correlation_id' => '123456', 'content_type' => 'text/plain']);
     $this->mockFactory2->expects($this->once())->method('createProperties')->with($domainMessage)->willReturn(['content_type' => 'application/json+ld', 'delivery_mode' => 2]);
     $expectedProperties = ['correlation_id' => '123456', 'content_type' => 'application/json+ld', 'delivery_mode' => 2];
     $actualProperties = $this->compositeFactory->createProperties($domainMessage);
     $this->assertEquals($expectedProperties, $actualProperties);
 }
 /**
  * @test
  */
 public function it_should_deserialize_an_array_of_domain_messages_and_return_a_stream()
 {
     $event = new DomainMessage('a', 0, new Metadata(), [], DateTime::now());
     $stream = new DomainEventStream([$event]);
     $this->serializer->shouldReceive('deserialize')->with(['serialized'])->andReturn($event);
     $serializer = new JsonDomainEventStreamSerializer($this->serializer);
     $deserialized = $serializer->deserialize('[["serialized"]]');
     $this->assertEquals($stream, $deserialized);
 }
 /**
  * @test
  */
 public function it_determines_correlation_id_based_on_message_id_and_playhead()
 {
     $id = 'effa2456-de78-480c-90ef-eb0a02b687c8';
     $playhead = 3;
     $domainMessage = new DomainMessage($id, $playhead, new Metadata(), new \stdClass(), DateTime::now());
     $expectedProperties = ['correlation_id' => 'effa2456-de78-480c-90ef-eb0a02b687c8-3'];
     $actualProperties = $this->factory->createProperties($domainMessage);
     $this->assertEquals($expectedProperties, $actualProperties);
 }
 /**
  * @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);
 }
Пример #10
0
 public function __construct(PHPUnit_Framework_TestCase $testCase, RepositoryInterface $repository, ProjectorInterface $projector)
 {
     $this->testCase = $testCase;
     $this->repository = $repository;
     $this->projector = $projector;
     $this->playhead = -1;
     $this->aggregateId = 1;
     $this->dateTimeGenerator = function ($event) {
         return DateTime::now();
     };
 }
 /**
  * @test
  */
 public function it_delegates_body_and_properties_creation_to_the_respective_injected_factories()
 {
     $domainMessage = new DomainMessage('06d0906d-e235-40d2-b9f3-1fa6aebc9e00', 1, new Metadata(), new DummyEvent('06d0906d-e235-40d2-b9f3-1fa6aebc9e00', 'foo'), DateTime::now());
     $body = '{"foo":"bar"}';
     $properties = ['deliver_mode' => 2];
     $expectedAMQPMessage = new AMQPMessage($body, $properties);
     $this->bodyFactory->expects($this->once())->method('createBody')->with($domainMessage)->willReturn($body);
     $this->propertiesFactory->expects($this->once())->method('createProperties')->with($domainMessage)->willReturn($properties);
     $actualAMQPMessage = $this->messageFactory->createAMQPMessage($domainMessage);
     $this->assertEquals($expectedAMQPMessage, $actualAMQPMessage);
 }
 public function testHandleThrowAExceptionWhenAHandlerHasNoEventMethod()
 {
     $this->setExpectedException(CanNotInvokeHandlerException::class);
     $event = new \stdClass();
     $domainMessage = new DomainMessage(12, 1, new Metadata(), $event, DateTime::now());
     $badHandler = new \stdClass();
     $this->nameExtractor->expects(self::atLeastOnce())->method('extract')->with($event)->willReturn('Finished');
     $this->handlerLocator->expects(self::atLeastOnce())->method('getHandlersForEvent')->with('Finished')->willReturn([$badHandler]);
     $this->methodNameInflector->expects(self::any())->method('inflect')->with($event, $badHandler)->willReturn('aEventMethod');
     $this->eventListener->handle($domainMessage);
 }
Пример #13
0
 /**
  * @test
  * @dataProvider providesLocales
  */
 public function it_creates_now($locale)
 {
     $this->setLocale(LC_ALL, $locale);
     $this->assertInstanceOf('Broadway\\Domain\\DateTime', DateTime::now());
 }
 /**
  * @test
  */
 public function it_sets_delivery_mode_based_on_the_injected_mode()
 {
     $domainMessage = new DomainMessage('af2e7491-9e40-45e0-8a09-22b5c4f3e366', 1, new Metadata(), new \stdClass(), DateTime::now());
     $nonPersistentFactory = new DeliveryModePropertiesFactory(AMQPMessage::DELIVERY_MODE_NON_PERSISTENT);
     $this->assertEquals(['delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT], $nonPersistentFactory->createProperties($domainMessage));
 }
 protected function createDomainMessage($id, $playhead, $recordedOn = null)
 {
     return new DomainMessage($id, $playhead, new MetaData(array()), new Event(), $recordedOn ? $recordedOn : DateTime::now());
 }
Пример #16
0
 public function serializeTargetProvider()
 {
     return array(array(new DomainMessage('foo', 1, new Metadata(array()), new stdClass(), DateTime::now())), array(new DomainEventStreamMessage(new DomainEventStream(array(new stdClass())))));
 }
Пример #17
0
 /**
  * @test
  */
 public function it_creates_now()
 {
     $this->assertInstanceOf('Broadway\\Domain\\DateTime', DateTime::now());
 }
 /**
  * @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);
     }
 }
 public function testReflectorWithArrays()
 {
     $reflector = new DomainMessageReflector(new DomainMessage('id', 1, new Metadata(array()), new DummyClass(), DateTime::now()));
     $reflections = $reflector->reflect(DomainMessageReflector::PAYLOAD);
     $this->assertEquals(array("property1" => "private", "property2" => "protected", "property3" => "public"), $reflections);
 }
 /**
  * @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);
 }
 private function createDomainMessageWithMetadata($id, $playhead, array $metadata, $recordedOn = null, $title = '')
 {
     return new DomainMessage($id, $playhead, new Metadata($metadata), new Event($title), $recordedOn ? $recordedOn : DateTime::now());
 }