/** * Test ShouldCreateSnapshot method. */ public function testShouldCreateSnapshot() { $this->given($policy = new CompositeSnapshottingPolicy([new AllwaysSnapshottingPolicy(), new EventsBasedSnapshottingPolicy()]))->and($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->then()->boolean($policy->shouldCreateSnapshot($post))->isTrue(); $this->given($policy = new CompositeSnapshottingPolicy([new AllwaysSnapshottingPolicy(), new EventsBasedSnapshottingPolicy()]))->and($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->and($post->clearEvents())->then()->boolean($policy->shouldCreateSnapshot($post))->isFalse(); $this->exception(function () { new CompositeSnapshottingPolicy([new AllwaysSnapshottingPolicy(), 'foo']); })->isInstanceOf(\InvalidArgumentException::class); }
/** * Test Remove method. */ public function testRemove() { $this->given($repository = $this->createRepository())->and($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->and($post->changeTitle($this->faker->sentence))->when($repository->persist($post))->then()->object($repository->get($post->id()))->isEqualTo($post)->and()->when($repository->remove($post))->then()->exception(function () use($repository, $post) { $repository->get($post->id()); })->isInstanceOf(\RuntimeException::class); $this->given($repository = $this->createRepository())->and($post = new Post(PostId::fromNative(md5(rand())), $this->faker->sentence, $this->faker->paragraph))->then()->exception(function () use($repository, $post) { $repository->remove($post); })->isInstanceOf(\InvalidArgumentException::class); $this->given($repository = $this->createRepository())->and($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->and($repository->persist($post))->and($preRemoveSubscriber = new PreRemoveSubscriber(42))->and($postRemoveSubscriber = new PostRemoveSubscriber())->and(DomainEventPublisher::subscribe($preRemoveSubscriber))->and(DomainEventPublisher::subscribe($postRemoveSubscriber))->when($repository->remove($post))->then()->integer($post->version()->patch())->isEqualTo(21); }
/** * Test validation. */ public function testValidation() { $this->given($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->when($post->changeTitle($this->faker->sentence()))->then()->array($post->recordedEvents())->hasSize(2)->and()->exception(function () use($post) { $post->changeTitle(''); })->isInstanceOf(ValidationException::class)->exception(function () use($post) { $post->changeTitle(10); })->isInstanceOf(ValidationException::class); $this->exception(function () { PostEventSourcedFactory::create('', $this->faker->paragraph); })->isInstanceOf(ValidationException::class); }
/** * Test ShouldCreateSnapshot method. */ public function testShouldCreateSnapshot() { $this->given($policy = new NoSnapshottingPolicy())->and($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->then()->boolean($policy->shouldCreateSnapshot($post))->isFalse(); }
/** * Test Aggregate method. */ public function testAggregate() { $this->given($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->and($event = new PostPersistEvent($post))->then()->object($event->aggregate())->isEqualTo($post); }
/** * Test Persist method. */ public function testPersist() { parent::testPersist(); $this->given($repository = $this->createRepository(new InMemorySnapshotStore(), new EventsBasedSnapshottingPolicy()))->and($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->and($version = new Version(0, 0, 231))->and($post->setVersion($version))->and($post->changeTitle($this->faker->sentence))->when($repository->persist($post))->then()->object($repository->get($post->id()))->isEqualTo($post); }
/** * Test Remove method. */ public function testRemove() { $this->given($store = $this->createStore())->and($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->and($snapshot = new Snapshot('posts', $post, new \DateTime()))->and($applicationVersion = Version::fromString('0.1.0'))->when($store->persist($snapshot, $applicationVersion))->and($store->remove('blogs', $post->id(), $post->version(), $applicationVersion))->then()->object($store->load('posts', $post->id(), $post->version(), $applicationVersion))->isEqualTo($snapshot)->and()->when($store->remove('posts', $post->id(), $post->version(), $applicationVersion))->then()->variable($store->load('posts', $post->id(), $post->version(), $applicationVersion))->isNull(); $this->given($store = $this->createStore())->and($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->and($snapshot = new Snapshot('posts', $post, new \DateTime()))->and($applicationVersion = Version::fromString('0.1.0'))->when($store->persist($snapshot, $applicationVersion))->then()->object($store->load('posts', $post->id(), $post->version(), $applicationVersion))->isEqualTo($snapshot)->and()->when($applicationVersion2 = Version::fromString('2.0.0'))->and($store->remove('posts', $post->id(), $post->version(), $applicationVersion2))->then()->object($store->load('posts', $post->id(), $post->version(), $applicationVersion))->isEqualTo($snapshot)->and()->when($store->remove('posts', $post->id(), $post->version(), $applicationVersion))->then()->variable($store->load('posts', $post->id(), $post->version(), $applicationVersion))->isNull(); }
/** * Test CreatedAt method. */ public function testCreatedAt() { $this->given($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->and($createdAt = new \DateTime())->and($snapshot = new Snapshot('posts', $post, $createdAt))->then()->object($snapshot->createdAt())->isEqualTo($createdAt); }
/** * Test persistVersionOf method. */ public function testPersistVersionOf() { $this->given($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->and($version = VersionManager::versionOf($post))->then()->object($version)->isEqualTo(Version::fromString('0.0.0'))->and()->when($post->version()->setMinor(23))->and($post->version()->setPatch(45))->and(VersionManager::persistVersionOf($post))->then()->object(VersionManager::versionOf($post))->isEqualTo(new Version(0, 23, 0))->object(VersionManager::versionOf($post, Version::fromString('1.1.0')))->isEqualTo(new Version(0, 0, 0)); }
/** * Test ShouldCreateSnapshot method. */ public function testShouldCreateSnapshot() { $this->given($snapshotStore = new InMemorySnapshotStore())->and($policy = new TimeBasedSnapshottingPolicy($snapshotStore, PostEventSourced::class, '1 hour'))->and($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->and($createdAt = date_create()->modify('-2 hours'))->and($snapshot = new Snapshot(NameResolver::resolve(get_class($post)), $post, $createdAt))->and($applicationVersion = Version::fromString('0.0.0'))->and($snapshotStore->persist($snapshot, $applicationVersion))->then()->boolean($policy->shouldCreateSnapshot($post))->isTrue()->and()->when($post->clearEvents())->then()->boolean($policy->shouldCreateSnapshot($post))->isFalse(); $this->given($snapshotStore = new InMemorySnapshotStore())->and($policy = new TimeBasedSnapshottingPolicy($snapshotStore, PostEventSourced::class, '1 hour'))->and($post = PostEventSourcedFactory::create($this->faker->sentence, $this->faker->paragraph))->and($createdAt = new \DateTime())->and($snapshot = new Snapshot(NameResolver::resolve(get_class($post)), $post, $createdAt))->and($applicationVersion = Version::fromString('0.1.0'))->and($snapshotStore->persist($snapshot, $applicationVersion))->then()->boolean($policy->shouldCreateSnapshot($post))->isFalse(); }