public function it_can_be_compared_with_another_contract(Contract $contract) { $contract->toString()->shouldBeCalledTimes(2); $contract->toString()->willReturn('stdClass'); $this->equals($contract)->shouldReturn(false); $contract->toString()->willReturn($this->className); $this->equals($contract)->shouldReturn(true); }
/** * @param Contract $aggregateType * @param AggregateRootIdentifier $aggregateRootIdentifier * * @return EventStream */ public function read(Contract $aggregateType, AggregateRootIdentifier $aggregateRootIdentifier) { $statement = $this->selectQuery->prepare(); $statement->bindValue(':aggregate_id', $aggregateRootIdentifier->toString()); $statement->bindValue(':aggregate_type', $aggregateType->toString()); $statement->execute(); $records = $statement->fetchAll(); $statement->closeCursor(); return new PdoReadRecordEventStream($aggregateRootIdentifier, $records, $this->eventSerializer); }
public function testRetrieveAllEventsForAnAggregateRoot() { $aggregateRootType = Contract::createFromClassName('stdClass'); $aggregateRootIdentifier = new AggregateRootIdentifierStub(); $this->statement->method('fetchAll')->willReturn([]); $this->assertInstanceOf('RayRutjes\\DomainFoundation\\Domain\\Event\\Stream\\EventStream', $this->eventStore->read($aggregateRootType, $aggregateRootIdentifier)); }
public function testDeserializesMetadata() { $metadata = new Metadata(); $contract = Contract::createFromClassName('stdClass'); $this->serializer->expects($this->once())->method('deserialize')->with('{}', $contract)->willReturn($metadata); $this->assertSame($metadata, $this->eventSerializer->deserializeMetadata('{}', $contract)); }
/** * @param AggregateRoot $aggregateRoot */ private function ensureAggregateRootIsSupported(AggregateRoot $aggregateRoot) { $supportedClassName = $this->aggregateRootType->className(); if (!$aggregateRoot instanceof $supportedClassName) { throw new \InvalidArgumentException('Unsupported aggregate type.'); } }
public function testSkipsUnknownPropertiesWhenDeserializing() { $serializer = new JsonSerializer(); $stub = new JsonSerializableStub(); $contract = Contract::createFromObject($stub); $this->assertEquals($stub, $serializer->deserialize('{"publicValue":"public","privateValue":3, "unknown":99}', $contract)); }
/** * @param $object * @param Contract $type * * @return Serializable */ public function deserialize($object, Contract $type) { $reflectionClass = new \ReflectionClass($type->className()); $data = json_decode($object, true); $instance = $reflectionClass->newInstanceWithoutConstructor(); foreach ($data as $propertyName => $propertyValue) { if (!$reflectionClass->hasProperty($propertyName)) { continue; } $reflectionProperty = $reflectionClass->getProperty($propertyName); if (!$reflectionProperty->isPublic()) { $reflectionProperty->setAccessible(true); } $reflectionProperty->setValue($instance, $propertyValue); } return $instance; }
public function setUp() { $this->unitOfWork = $this->getMock('RayRutjes\\DomainFoundation\\UnitOfWork\\UnitOfWork'); $this->aggregateRootType = Contract::createFromClassName('RayRutjes\\DomainFoundation\\Test\\Unit\\Domain\\AggregateRoot\\EventSourcedAggregateRootStub'); $this->eventStore = $this->getMock('RayRutjes\\DomainFoundation\\EventStore\\EventStore'); $this->eventBus = $this->getMock('RayRutjes\\DomainFoundation\\EventBus\\EventBus'); $this->repository = new AggregateRootRepository($this->unitOfWork, $this->aggregateRootType, $this->eventStore, $this->eventBus); }
/** * @param MessageIdentifier $identifier * @param Serializable $payload * @param Metadata $metadata */ public function __construct(MessageIdentifier $identifier, Serializable $payload, Metadata $metadata = null) { $this->identifier = $identifier; $this->payload = $payload; $this->payloadType = Contract::createFromObject($this->payload); $this->metadata = null === $metadata ? new Metadata() : $metadata; $this->metadataType = Contract::createFromObject($this->metadata); }
/** * @param array $record * * @return Event */ private function translateRecordIntoEventMessage(array $record) { $identifier = new MessageIdentifier($record['event_id']); $payload = $this->eventSerializer->deserializePayload($record['event_payload'], Contract::createFromClassName($record['event_payload_type'])); $metadata = $this->eventSerializer->deserializeMetadata($record['event_metadata'], Contract::createFromClassName($record['event_metadata_type'])); $sequenceNumber = (int) $record['aggregate_version']; $eventMessage = new GenericEvent($this->aggregateRootIdentifier, $sequenceNumber, $identifier, $payload, $metadata); return $eventMessage; }
/** * @expectedException \RayRutjes\DomainFoundation\Repository\ConcurrencyException */ public function testDetectsConcurrencyAndThrowsTheAccordingException() { $aggregateRootIdentifier = new AggregateRootIdentifierStub(Uuid::NIL); $aggregateRootType = Contract::createFromClassName('Product'); $messageIdentifier = new MessageIdentifier(UUID::NIL); $payload = new PayloadStub(); $event = new GenericEvent($aggregateRootIdentifier, 1, $messageIdentifier, $payload); $eventStream = new GenericEventStream([$event]); $this->eventStore->append($aggregateRootType, $eventStream); }
/** * @depends testCanBeCreatedFromAClassName */ public function testCanDetermineIfItIsEqualToAnotherContract(Contract $contract) { $this->assertTrue($contract->equals($contract)); $this->assertFalse($contract->equals(Contract::createFromClassName('AnotherClassName'))); }
/** * @param UnitOfWork $unitOfWork * @param \PDO $pdo * @param EventBus $eventBus */ public function __construct(UnitOfWork $unitOfWork, \PDO $pdo, EventBus $eventBus) { $eventStore = new PdoEventStore($pdo); $aggregateRootType = Contract::createFromClassName(User::class); $this->repository = new AggregateRootRepository($unitOfWork, $aggregateRootType, $eventStore, $eventBus); }
/** * @param Contract $aggregateRootType * @param EventStream $eventStream * * @return AggregateRoot */ public function loadFromHistory(Contract $aggregateRootType, EventStream $eventStream) { $className = $aggregateRootType->className(); return $className::loadFromHistory($eventStream); }
public function it_can_load_an_aggregate_root_from_its_history(Contract $contract, EventStream $eventStream) { $contract->className()->willReturn('\\RayRutjes\\DomainFoundation\\Stub\\Domain\\AggregateRoot\\EventSourcedAggregateRootStub'); $this->loadFromHistory($contract, $eventStream)->shouldHaveType('RayRutjes\\DomainFoundation\\Stub\\Domain\\AggregateRoot\\EventSourcedAggregateRootStub'); }
/** * @param UnitOfWork $unitOfWork * @param Connection $connection */ public function __construct(UnitOfWork $unitOfWork, Connection $connection, EventBus $eventBus) { $eventStore = new DoctrineEventStore($connection); $aggregateRootType = Contract::createFromClassName(User::class); $this->repository = new AggregateRootRepository($unitOfWork, $aggregateRootType, $eventStore, $eventBus); }
public function it_should_ignore_non_existent_parameter(Contract $contract) { $contract->className()->shouldBeCalledTimes(1)->willReturn('RayRutjes\\DomainFoundation\\Stub\\Serializer\\SerializableStub'); $object = $this->deserialize('{"name":"test","surname":"test","firstname":"test"}', $contract); $this->serialize($object)->shouldEqual('{"name":"test","surname":"test"}'); }