88use Tempest \CommandBus \CommandBus ;
99use Tempest \CommandBus \CommandBusConfig ;
1010use Tempest \CommandBus \CommandHandler ;
11+ use Tempest \CommandBus \CommandHandlerAlreadyExists ;
1112use Tempest \CommandBus \CommandHandlerNotFound ;
1213use Tempest \CommandBus \GenericCommandBus ;
1314use Tempest \CommandBus \Tests \Fixtures \CreateUserCommand ;
2122 */
2223final class GenericCommandBusTest extends TestCase
2324{
25+ private CommandBusConfig $ config ;
26+
2427 private CommandBus $ commandBus ;
2528
2629 public function test_getting_command_handler_that_exists (): void
@@ -44,26 +47,41 @@ public function test_exception_is_thrown_when_command_handler_doesnt_exist(): vo
4447 $ this ->commandBus ->dispatch ($ command );
4548 }
4649
50+ public function test_exception_is_thrown_when_command_handler_already_exist (): void
51+ {
52+ $ createUserCommandHandlerClass = new ClassReflector (CreateUserCommandHandler::class);
53+ $ createUserCommandHandlerMethod = $ createUserCommandHandlerClass ->getMethod ('__invoke ' );
54+ $ createUserCommandHandler = $ createUserCommandHandlerMethod ->getAttribute (CommandHandler::class);
55+
56+ $ this ->expectException (CommandHandlerAlreadyExists::class);
57+
58+ $ this ->config ->addHandler (
59+ commandHandler: $ createUserCommandHandler ,
60+ commandName: CreateUserCommand::class,
61+ handler: $ createUserCommandHandlerClass ->getMethod ('double ' ),
62+ );
63+ }
64+
4765 protected function setUp (): void
4866 {
4967 parent ::setUp ();
5068
5169 // TODO: I'd like to make this easier to setup.
52- $ config = new CommandBusConfig ();
70+ $ this -> config = new CommandBusConfig ();
5371
5472 $ createUserCommandHandlerClass = new ClassReflector (CreateUserCommandHandler::class);
5573 $ createUserCommandHandlerMethod = $ createUserCommandHandlerClass ->getMethod ('__invoke ' );
5674 $ createUserCommandHandler = $ createUserCommandHandlerMethod ->getAttribute (CommandHandler::class);
5775
58- $ config ->addHandler (
76+ $ this -> config ->addHandler (
5977 commandHandler: $ createUserCommandHandler ,
6078 commandName: CreateUserCommand::class,
61- handler: $ createUserCommandHandlerMethod
79+ handler: $ createUserCommandHandlerMethod,
6280 );
6381
6482 $ this ->commandBus = new GenericCommandBus (
6583 container: new GenericContainer (),
66- commandBusConfig: $ config
84+ commandBusConfig: $ this -> config ,
6785 );
6886 }
6987}
0 commit comments