我正在测试我的命令处理程序,类似于这样:
public function testHandle_ShouldPublishFooEvent(): void
{
$fooCommand = $this->givenFooCommand();
$this->whenHandleCommand($fooCommand);
$this->thenFooEventIsPublished();
}基本上,当命令处理程序中没有验证逻辑时,我只是在测试愉快的场景。我通过检查预期事件是否发布到事件总线来测试它。在用whenHandleCommand方法将经过测试的命令发送到命令总线之前,我开始记录分派的事件,如下所示:
$eventBus->attach(
EventBus::EVENT_DISPATCH,
function (ActionEvent $actionEvent) use ($events): void {
$event = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE);
$events->addEvent($event);
},
-1000
);最后,我只是检查记录的事件,并断言这是我所期望的。但是,在MysqlEventStore和InMemoryEventStore之间切换存在问题,因为MysqlEventStore不是事务性的(从而在saveAggregateRoot方法中释放事件),而不是事务性的InMemoryEventStore (从而在提交方法中释放事件)。
我的存储库保存方法非常简单,如:
class ProophFooRepository extends AggregateRepository implements FooRepository
{
public function save(FooAggregate $foo): void
{
$this->saveAggregateRoot($foo);
}
...
}如何使它,使我可以改变任何事件存储(内存或pdo),我想使用,它将工作?为此,我的存储库中是否应该有条件if($this->isTransactionalEventStore()) (然后开始事务并提交)?我不喜欢那样。:(
为什么只有InMemoryEventStore是事务性的?同时拥有InMemoryTransactionalEventStore和InMemoryEventStore难道不是更好吗?因为我希望在InMemoryEventStore中运行我的测试,而通常我使用的是PdoEventStore。
编辑:当我将InMemoryEventStoreFactory中的第100行更改为
$wrapper = new ActionEventEmitterEventStore($eventStore, $eventEmitter);让InMemoryEventStore实现EventStore而不是TransactionalEventStore,一切都很好。因此,要么我不正确地使用prooph,而且它是不需要的,要么可以通过PR将ImMemoryEventStore分割成InMemoryTransactionalEventStore和InMemoryEventStore来轻松地修复它。
发布于 2017-10-18 08:40:39
这是个很有道理的问题。问题是,在处理v7事件存储实现时,我没有考虑到这个用例。解决方法之一是使用ActionEventEmitterEventStore,正如您所描述的。然而,内存事件存储中的非事务性将是更好的选择(此时不存在此选项)。
目前InMemoryEventStore是事务性的,因此删除该功能并将其放入TransactionalInMemoryEventStore将是BC中断,因此没有一个新的主要版本,我们就无法做到这一点。这就是为什么我认为,我们应该创建一个名为NonTransactionalInMemoryEventStore的新实现。我在这里创建了一张票:https://github.com/prooph/event-store/issues/307。想接手做个公关吗?
https://stackoverflow.com/questions/46769347
复制相似问题