function it_does_not_add_user_when_the_user_already_exists(AddUserCommand $command, UserRepository $repository, User $user) { $command->userId()->shouldBeCalled()->willReturn('user-id'); $repository->userOfId(UserId::generate('user-id'))->shouldBeCalled()->willReturn($user); $user->id()->shouldBeCalled()->willReturn(UserId::generate('user-id')); $this->shouldThrow(UserAlreadyExistsException::class)->during__invoke($command); }
public function __invoke(AddUserCommand $command) { $userId = UserId::generate($command->userId()); $user = $this->repository->userOfId($userId); if ($user instanceof User) { throw new UserAlreadyExistsException($user->id()); } $this->repository->persist(new User($userId)); }