我正在以某种方式测试我的命令处理程序:
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
但是我在and之间切换时遇到问题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
and InMemoryEventStore
?