/** * @return DomainEventPublisher */ private static function instance() { if (static::$instance === null) { static::$instance = new static(); static::$instance->setEventBus(EventBus::create()); } return static::$instance; }
/** * 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); }
/** * @param callable|ApplicationConfig $config * * @return ConsoleApplication */ protected function createApplication($config) { $commandBus = CommandBus::create(); $eventBus = DomainEventPublisher::eventBus(); $migrationsService = new MigrationsService(new Migrator($this->getClassMetadataFactory(), new InMemoryMigrationStore(), new InMemoryEventStore(), $this->migrationsDirectory)); $commandBus->addHandler(MigrationsGenerateCommand::class, $migrationsService); $commandBus->addHandler(MigrationsMigrateCommand::class, $migrationsService); $commandBus->addHandler(MigrationsStatusCommand::class, $migrationsService); return new ConsoleApplication($config, $commandBus, $eventBus); }
/** * Save the aggregate history. * * @param EventSourcedAggregateRootInterface $aggregateRoot */ protected function saveHistory(EventSourcedAggregateRootInterface $aggregateRoot) { $recordedEvents = $aggregateRoot->recordedEvents(); if (count($recordedEvents) > 0) { DomainEventPublisher::publish(new PrePersistEvent($aggregateRoot)); // clear events $aggregateRoot->clearEvents(); // create the eventStream and persist it $applicationVersion = VersionManager::currentApplicationVersion(); $eventStream = new EventStream($this->streamName(), $aggregateRoot->id(), $recordedEvents); $this->eventStore->persist($eventStream, $aggregateRoot->version(), $applicationVersion); DomainEventPublisher::publish(new PostPersistEvent($aggregateRoot)); } }
/** * @param DomainEventInterface $event */ protected function publishEvent(DomainEventInterface $event) { DomainEventPublisher::publish($event); }
/** * Test eventBus method. */ public function testEventBus() { $this->given($eventBus = EventBus::create())->when(DomainEventPublisher::set($eventBus))->then()->object(DomainEventPublisher::eventBus())->isEqualTo($eventBus); }