protected function execute(InputInterface $input, OutputInterface $output) { try { $userId = new UserId($input->getArgument('id')); $newEmail = new EmailAddress($input->getArgument('email')); $command = new ChangeUserEmail($userId, $newEmail); $this->getCommandBus()->handle($command); } catch (\Exception $e) { if ($e instanceof UserNotFoundException || $e instanceof InvalidEmailAddressException || $e instanceof InvalidValueForUserIdException) { $output->writeln($e->getMessage()); return 1; } } $output->writeln(sprintf('Change email command for user %d executed successfully.', $userId->id())); return 0; }
/** @test */ public function it_handles_email_address_change() { $userId = new UserId(11); $oldEmail = new EmailAddress('*****@*****.**'); $newEmail = new EmailAddress('*****@*****.**'); $user = new User($userId, new Name('Splinter'), $oldEmail); $command = new ChangeUserEmail($userId, $newEmail); $repository = $this->getUserRepositoryMock(); $repository->expects($this->once())->method('find')->willReturn($user); $interceptedMessage = null; $eventBus = $this->getEventBusMock(); $eventBus->expects($this->once())->method('handle')->willReturnCallback(function ($message) use(&$interceptedMessage) { $interceptedMessage = $message; }); $commandHandler = new UserHandler($repository, $eventBus); $commandHandler->handleChangeUserEmail($command); // change should trigger event $this->assertInstanceOf('AppBundle\\Messages\\Events\\ChangedUserEmail', $interceptedMessage); $this->assertTrue($userId->equals($interceptedMessage->userId())); // finally check if address changed $this->assertTrue($user->getEmail()->equals($newEmail)); }
/** @test */ public function it_is_equal_to_other_instance_with_the_same_id() { $id1 = new UserId(1234); $id2 = new UserId(1234); $this->assertTrue($id1->equals($id2)); }