Use this factory when the aggregate type is not equal to the aggregate root class
public static fromString ( $aggregateTypeString ) : |
||
$aggregateTypeString | ||
리턴 |
/** * @test */ public function it_uses_custom_snapshot_grid_fs_map_and_write_concern() { $this->adapter = new MongoDbAdapter($this->client, 'test', ['w' => 'majority', 'j' => true], ['foo' => 'bar']); $aggregateType = AggregateType::fromString('foo'); $aggregateRoot = new \stdClass(); $aggregateRoot->foo = 'bar'; $time = microtime(true); if (false === strpos($time, '.')) { $time .= '.0000'; } $now = \DateTimeImmutable::createFromFormat('U.u', $time); $snapshot = new Snapshot($aggregateType, 'id', $aggregateRoot, 1, $now); $this->adapter->add($snapshot); $gridFs = $this->client->selectDB('test')->getGridFS('bar'); $file = $gridFs->findOne(); $this->assertNotNull($file); }
/** * @test */ public function it_saves_and_reads() { $m = new \Memcached(); $m->addServer('localhost', 11211); $adapter = new MemcachedSnapshotAdapter($m); $aggregateType = AggregateType::fromString('foo'); $aggregateRoot = new \stdClass(); $aggregateRoot->foo = 'bar'; $time = microtime(true); if (false === strpos($time, '.')) { $time .= '.0000'; } $now = \DateTimeImmutable::createFromFormat('U.u', $time, new \DateTimeZone('UTC')); $snapshot = new Snapshot($aggregateType, 'id', $aggregateRoot, 1, $now); $adapter->save($snapshot); $this->assertNull($adapter->get($aggregateType, 'invalid')); $readSnapshot = $adapter->get($aggregateType, 'id'); $this->assertEquals($snapshot, $readSnapshot); }
/** * @test */ public function it_uses_custom_snapshot_table_map() { $schema = new Schema(); SnapshotStoreSchema::create($schema, 'bar'); foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) { $this->connection->executeQuery($sql); } $adapter = new DoctrineSnapshotAdapter($this->connection, ['foo' => 'bar']); $aggregateType = AggregateType::fromString('foo'); $aggregateRoot = new \stdClass(); $aggregateRoot->foo = 'bar'; $time = microtime(true); if (false === strpos($time, '.')) { $time .= '.0000'; } $now = \DateTimeImmutable::createFromFormat('U.u', $time); $snapshot = new Snapshot($aggregateType, 'id', $aggregateRoot, 1, $now); $adapter->add($snapshot); $queryBuilder = $this->connection->createQueryBuilder(); $queryBuilder->select('*')->from('bar', 'bar')->setMaxResults(1); $stmt = $queryBuilder->execute(); $this->assertNotNull($stmt->fetch(\PDO::FETCH_ASSOC)); }
/** * @test * @expectedException \InvalidArgumentException */ public function it_does_not_allow_to_add_stream_events_with_wrong_repository_aggregate_type() { $this->eventStore->beginTransaction(); $user = User::create("John Doe", "*****@*****.**"); $aggregateType = AggregateType::fromAggregateRoot($user); $aggregateId = Uuid::uuid4()->toString(); $streamEvents = [UserCreated::with(['user_id' => $aggregateId], 1)]; $this->strategy->addEventsForNewAggregateRoot($aggregateType, $aggregateId, $streamEvents, $user); $this->eventStore->commit(); $this->eventStore->beginTransaction(); $streamEvents = [UsernameChanged::with(['name' => 'John Doe'], 2)]; $this->strategy->appendEvents(AggregateType::fromString("Product"), $aggregateId, $streamEvents, $user); }
/** * @test * @expectedException Prooph\EventStore\Aggregate\Exception\InvalidArgumentException * @expectedExceptionMessage AggregateType must be a non empty string */ public function it_throws_exception_when_trying_to_create_from_empty_string() { AggregateType::fromString(null); }
/** * @test */ public function it_deals_with_resources_on_serialized_aggregate_roots() { /** @var Connection|\PHPUnit_Framework_MockObject_MockObject $connection */ $connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock(); /** @var QueryBuilder|\PHPUnit_Framework_MockObject_MockObject $queryBuilder */ $queryBuilder = $this->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()->getMock(); /** @var Statement|\PHPUnit_Framework_MockObject_MockObject $stmt */ $stmt = $this->getMockForAbstractClass(Statement::class); $aggregateRoot = new \stdClass(); $aggregateRoot->data = 'AggregateRoot'; $resource = fopen('php://temp', 'r+'); fwrite($resource, serialize($aggregateRoot)); fseek($resource, 0); $connection->expects($this->once())->method('createQueryBuilder')->willReturn($queryBuilder); $queryBuilder->expects($this->once())->method('select')->willReturnSelf(); $queryBuilder->expects($this->once())->method('from')->willReturnSelf(); $queryBuilder->expects($this->any())->method('where')->willReturnSelf(); $queryBuilder->expects($this->any())->method('andWhere')->willReturnSelf(); $queryBuilder->expects($this->any())->method('orderBy')->willReturnSelf(); $queryBuilder->expects($this->any())->method('setParameter')->willReturnSelf(); $queryBuilder->expects($this->any())->method('setMaxResults')->willReturnSelf(); $queryBuilder->expects($this->once())->method('execute')->willReturn($stmt); $stmt->expects($this->once())->method('fetch')->with(\PDO::FETCH_ASSOC)->willReturn(['aggregate_root' => $resource, 'last_version' => 3, 'created_at' => '2016-01-21T09:33:00.000']); $adapter = new DoctrineSnapshotAdapter($connection, ['foo' => 'bar']); $snapshot = $adapter->get(AggregateType::fromString('foo'), 'some-uuid-non-important-here'); $this->assertEquals('AggregateRoot', $snapshot->aggregateRoot()->data); }
/** * @test * @expectedException \Prooph\EventStore\Aggregate\Exception\AggregateTranslationFailedException */ public function it_throws_exception_if_reconstitute_form_history_method_name_does_not_return_an_instance_of_aggregate_type() { $translator = new ConfigurableAggregateTranslator(); $translator->reconstituteAggregateFromHistory(AggregateType::fromString(FaultyAggregateRoot::class), []); }
/** * @test * @expectedException Prooph\EventStore\Exception\RuntimeException * @expectedExceptionMessage The aggregate type cannot be detected */ public function it_throws_exception_when_aggregate_type_cannot_be_detected_on_get_aggregate_root_type() { $aggregateType = AggregateType::fromString('Object'); $streamEvent = $this->prophesize(Message::class); $streamEvent->metadata()->willReturn(['foo' => 'bar'])->shouldBeCalled(); $streamEvents = [$streamEvent->reveal()]; $this->strategy->getAggregateRootType($aggregateType, $streamEvents); }