public function testCommandToHandlerMapIsProperlyConfigured() { $map = [AddTaskCommand::class => Mockery::mock(ConcreteMethodsHandler::class), CompleteTaskCommand::class => Mockery::mock(ConcreteMethodsHandler::class)]; $map[AddTaskCommand::class]->shouldReceive('handle')->once(); $map[CompleteTaskCommand::class]->shouldReceive('handle')->never(); $commandBus = QuickStart::create($map); $commandBus->handle(new AddTaskCommand()); }
public function testCommandToHandlerMapIsProperlyConfigured() { $map = ['League\\Tactician\\Tests\\Fixtures\\Command\\AddTaskCommand' => Mockery::mock('League\\Tactician\\Tests\\Fixtures\\Handler\\ConcreteMethodsHandler'), 'League\\Tactician\\Tests\\Fixtures\\Command\\CompleteTaskCommand' => Mockery::mock('League\\Tactician\\Tests\\Fixtures\\Handler\\ConcreteMethodsHandler')]; $map['League\\Tactician\\Tests\\Fixtures\\Command\\AddTaskCommand']->shouldReceive('handle')->once(); $map['League\\Tactician\\Tests\\Fixtures\\Command\\CompleteTaskCommand']->shouldReceive('handle')->never(); $commandBus = QuickStart::create($map); $commandBus->handle(new AddTaskCommand()); }
/** * Use the register method to register items with the container via the * protected $this->container property or the `getContainer` method * from the ContainerAwareTrait. */ public function register() { $container = $this->getContainer(); $commands = $this->commands; // add all message classes to the container foreach ($commands as $alias => $config) { $container->add($alias, function (Container $container, $config) { /** @var MessageFactoryInterface $factory */ $factory = !empty($config['factory']) ? $config['factory'] : DefaultFactory::class; $factory = $container->get($factory); if (!$factory instanceof MessageFactoryInterface) { throw new CliException(sprintf('Expected factory to be an instance of "%s". Got "%s" instead.', MessageFactoryInterface::class, is_object($factory) ? get_class($factory) : gettype($factory))); } return $factory->create($config['message']); })->withArguments([ContainerInterface::class, $config]); } // setup the command bus to know which handler to use for each message class $container->singleton(Services::COMMAND_BUS, function () use($commands) { $map = []; foreach ($commands as $alias => $config) { $message = $config['message']; $handler = $config['handler']; $map[$message] = new $handler(); } return QuickStart::create($map); }); // create a service (that's just an array) that has a list of all the commands for the app $container->add(Services::COMMANDS, function (ContainerInterface $container) use($commands) { $commandList = []; foreach ($commands as $alias => $config) { $commandList[] = new BaseCommand($container, $alias, $config['message']); } return $commandList; })->withArgument(ContainerInterface::class); }