イベントによる、ふるまい的結合の解消
イベントの発行と副作用のテストを分けることができ、テストが書きやすくなる
#[Test]
public function ユーザーを作成する()
{
// ユーザー作成時にイベントが発行されることを確認
$this->mock(DomainEventPublisher::class)
->expects($this->once())
->method('publish');
app()->make(UserCreateUsecase::class)->exec($input);
}
#[Test]
public function ウェルカムメールを送信する(): void
{
$mailService = $this->mock(MailService::class);
$mailService->expects($this->once())
->method('sendWelcomeMail')
->with($this->equalTo(new Email('
[email protected]')));
$event = new UserCreatedEvent(
new UserId('123'),
new Email('
[email protected]')
);
app()->make(WelcomeMailSubscriber::class)->handle($event);
}
23/44