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);
 }
 protected function setUp()
 {
     $this->amqpPublisher = $this->getMock(AMQPPublisherInterface::class);
     $this->domainMessageNormalizer = $this->getMock(DomainMessageNormalizerInterface::class);
     $this->domainMessageNormalizerAMQPPublisherDecorator = new DomainMessageNormalizerAMQPPublisherDecorator($this->amqpPublisher, $this->domainMessageNormalizer);
     $this->domainMessage = new DomainMessage('F68E71A1-DBB0-4542-AEE5-BD937E095F74', 2, new Metadata(), new DummyEvent('F68E71A1-DBB0-4542-AEE5-BD937E095F74', 'test 123 456'), BroadwayDateTime::fromString('2015-01-02T08:40:00+0100'));
 }
 /**
  * @inheritDoc
  */
 public function load($id)
 {
     try {
         $feed = $this->eventStore->openStreamFeed($id);
     } catch (StreamNotFoundException $e) {
         throw new EventStreamNotFoundException($e->getMessage());
     }
     if ($feed->hasLink(LinkRelation::LAST())) {
         $feed = $this->eventStore->navigateStreamFeed($feed, LinkRelation::LAST());
     } else {
         $feed = $this->eventStore->navigateStreamFeed($feed, LinkRelation::FIRST());
     }
     $rel = LinkRelation::PREVIOUS();
     $messages = [];
     $i = 0;
     while ($feed !== null) {
         foreach ($this->sortEntries($feed->getEntries()) as $entry) {
             $event = $this->eventStore->readEvent($entry->getEventUrl());
             $data = $event->getData();
             $recordedOn = DateTime::fromString($data['broadway_recorded_on']);
             unset($data['broadway_recorded_on']);
             $messages[] = new DomainMessage($id, $event->getVersion(), new Metadata($event->getMetadata() ?: []), call_user_func([$event->getType(), 'deserialize'], $data), $recordedOn);
         }
         $feed = $this->eventStore->navigateStreamFeed($feed, $rel);
     }
     return new DomainEventStream($messages);
 }
 /**
  * @test
  */
 public function it_throws_runtime_exception_when_payload_is_not_serializable()
 {
     $this->expectSpecificationIsSatisfied();
     $domainMessage = new DomainMessage('F68E71A1-DBB0-4542-AEE5-BD937E095F74', 2, new Metadata(), new DummyEventNotSerializable('F68E71A1-DBB0-4542-AEE5-BD937E095F74', 'test 123 456'), BroadwayDateTime::fromString('2015-01-02T08:40:00+0100'));
     $this->setExpectedException(\RuntimeException::class, 'Unable to serialize CultuurNet\\BroadwayAMQP\\Dummies\\DummyEventNotSerializable');
     $this->amqpPublisher->handle($domainMessage);
 }
 /**
  * @test
  */
 public function it_can_deserialize_a_domain_message()
 {
     $jsonData = new String(file_get_contents(__DIR__ . '/Dummies/domain-message-dummy-event.json'));
     $expectedDomainMessage = new DomainMessage('message-id-123', 0, new Metadata(), new DummyEvent('foo', 'bla'), DateTime::fromString('2016-03-25'));
     $domainMessage = $this->domainMessageJSONDeserializer->deserialize($jsonData);
     $this->assertEquals($expectedDomainMessage, $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();
 }
Пример #8
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);
 }
 public function current()
 {
     $event = $this->innerIterator->current()->getEvent();
     $data = $event->getData();
     $recordedOn = DateTime::fromString($data['broadway_recorded_on']);
     unset($data['broadway_recorded_on']);
     return new DomainMessage($this->id, $event->getVersion(), new Metadata($event->getMetadata() ?: []), call_user_func([$event->getType(), 'deserialize'], $data), $recordedOn);
 }
 /**
  * @test
  */
 public function it_returns_a_normalized_event_stream_when_normalizers_are_set()
 {
     $normalizedDomainMessage = new DomainMessage('F68E71A1-DBB0-4542-AEE5-BD937E095F74', 2, new Metadata(), new DummyEventSubclass('F68E71A1-DBB0-4542-AEE5-BD937E095F74', 'test 123 456'), BroadwayDateTime::fromString('2015-01-02T08:40:00+0100'));
     $expectedDomainEventStream = new DomainEventStream(array($normalizedDomainMessage));
     $this->dummyNormalizer->expects($this->once())->method('normalize')->with($this->dummyDomainMessage)->willReturn($expectedDomainEventStream);
     $actualEventDomainStream = $this->combinedDomainMessageNormalizer->normalize($this->dummyDomainMessage);
     $this->assertEquals($expectedDomainEventStream, $actualEventDomainStream);
 }
 /**
  * @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);
 }
 /**
  * @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_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);
 }
Пример #18
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_should_deserialize_to_a_DomainMessage()
 {
     $metadata = new Metadata();
     $payload = [];
     $time = DateTime::fromString('2015-01-01');
     $event = new DomainMessage('a', 0, $metadata, $payload, $time);
     $serializer = new ThirdPartyPayloadAndMetadataDomainMessageSerializer($this->payloadSerializer, $this->metadataSerializer);
     $this->metadataSerializer->shouldReceive('deserialize')->with(['metadata'])->andReturn($metadata);
     $this->payloadSerializer->shouldReceive('deserialize')->with(['payload'])->andReturn($payload);
     $serialized = ['id' => 'a', 'playhead' => 0, 'metadata' => ['metadata'], 'payload' => ['payload'], 'recordedOn' => (new \DateTime('2015-01-01'))->format(DateTime::FORMAT_STRING)];
     $deserialized = $serializer->deserialize($serialized);
     $this->assertEquals($event, $deserialized);
 }
 private function rebuildStream($id)
 {
     /** @var \Broadway\EventStore\EventStoreInterface $eventStore */
     $eventStore = app('Broadway\\EventStore\\EventStoreInterface');
     $stream = $eventStore->load($id);
     foreach ($stream->getIterator() as $event) {
         $limit = DateTime::fromString($this->limitDate);
         $recordedOnDate = $event->getRecordedOn();
         if ($recordedOnDate->comesAfter($limit)) {
             continue;
         }
         $this->addEventToBuffer($event);
         $this->guardBufferNotFull();
     }
 }
Пример #21
0
 /**
  * @return boolean
  */
 public function equals(DateTime $dateTime)
 {
     return $this->toString() === $dateTime->toString();
 }
 /**
  * Deserialize a result row to a DomainMessage
  *
  * @author Dennis Schepers
  *
  * @param $row
  *
  * @return \Broadway\Domain\DomainMessage
  */
 private function deserializeEvent($event)
 {
     return new DomainMessage($event->uuid, (int) $event->playhead, $this->deserialize($event->metadata), $this->deserialize($event->payload), DateTime::fromString($event->recorded_on));
 }
Пример #23
0
 private function deserializeEvent($row)
 {
     return new DomainMessage($row['uuid'], $row['playhead'], $this->metadataSerializer->deserialize(json_decode($row['metadata'], true)), $this->payloadSerializer->deserialize(json_decode($row['payload'], true)), DateTime::fromString($row['recorded_on']));
 }
 /**
  * @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));
 }
 /**
  * {@inheritdoc}
  */
 public function deserialize(array $serializedObject)
 {
     return new DomainMessage($serializedObject['uuid'], $serializedObject['playhead'], $this->metadataSerializer->deserialize(json_decode($serializedObject['metadata'], true)), $this->payloadSerializer->deserialize(json_decode($serializedObject['payload'], true)), DateTime::fromString($serializedObject['recorded_on']));
 }
Пример #26
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())))));
 }
 /**
  * @test
  */
 public function it_satisfies_payload_is_a_subclass_of()
 {
     $domainMessage = new DomainMessage('F68E71A1-DBB0-4542-AEE5-BD937E095F74', 2, new Metadata(), new DummyEventSubclass('F68E71A1-DBB0-4542-AEE5-BD937E095F74', 'test 123 456'), BroadwayDateTime::fromString('2015-01-02T08:40:00+0100'));
     $payloadIsInstanceOf = new PayloadIsInstanceOf(DummyEvent::class);
     $this->assertTrue($payloadIsInstanceOf->isSatisfiedBy($domainMessage));
 }
Пример #28
0
 /**
  * @param $row
  * @return DomainMessage
  */
 protected function deserializeEvent($row)
 {
     $metadata = json_decode($row['metadata'], true);
     $payload = json_decode($row['payload'], true);
     return new DomainMessage($row['uuid'], $row['playhead'], $this->metadataSerializer->deserialize($metadata, $metadata['payload']), $this->payloadSerializer->deserialize($payload, $payload['payload']), DateTime::fromString($row['recorded_on']));
 }
 /**
  * @test
  */
 public function it_throws_serialization_exception_when_payload_is_not_serializable()
 {
     $this->setExpectedException(SerializationException::class, 'Unable to serialize CultuurNet\\BroadwayAMQP\\Dummies\\DummyEventNotSerializable');
     $domainMessage = new DomainMessage('F68E71A1-DBB0-4542-AEE5-BD937E095F74', 2, new Metadata(), new DummyEventNotSerializable('F68E71A1-DBB0-4542-AEE5-BD937E095F74', 'test 123 456'), BroadwayDateTime::fromString('2015-01-02T08:40:00+0100'));
     $this->entireDomainMessageBodyFactory->createBody($domainMessage);
 }
 protected function createDomainMessage($id, $playhead, $recordedOn = null)
 {
     return new DomainMessage($id, $playhead, new MetaData(array()), new Event(), $recordedOn ? $recordedOn : DateTime::now());
 }