From b960c4b89415076e07beb6257a8727b5bb8f8733 Mon Sep 17 00:00:00 2001 From: Cherif BOUCHELAGHEM Date: Mon, 14 Sep 2015 14:03:50 +0100 Subject: [PATCH 1/2] remove bootstrap relying on init only :) --- Tactician.php | 98 +++++++++++++++-------------------------- tests/TacticianTest.php | 61 ++++++++++++------------- tests/TestCase.php | 36 ++++++++------- 3 files changed, 83 insertions(+), 112 deletions(-) diff --git a/Tactician.php b/Tactician.php index 77b9560..ed6cc89 100644 --- a/Tactician.php +++ b/Tactician.php @@ -3,98 +3,70 @@ use Yii; use yii\base\Component; -use yii\base\BootstrapInterface; use League\Tactician\Handler\CommandHandlerMiddleware; use League\Tactician\CommandBus; -class Tactician extends Component implements BootstrapInterface +class Tactician extends Component { public $inflector; public $extractor; public $commandHandlerMap = []; - public $middlewares = []; - public function bootstrap($app) - { - $this->buildTactician(); - } + protected $tactician; + protected $middlewares = []; public function init() { + $this->buildTactician(); } protected function buildTactician() { - $this->createExtractor($this->extractor); - $this->createContainerLocator($this->commandHandlerMap); - $this->createInflector($this->inflector); - $this->createHandlerMiddleware(); - $this->createCommandBus(); - } + $extractor = Yii::createObject($this->extractor); + + $containerLocator = Yii::createObject('cherif\tactician\ContainerLocator',[ + $this->commandHandlerMap + ] + ); - protected function createInflector($inflectorClass) - { - return Yii::$container->set('League\Tactician\Handler\MethodNameInflector\MethodNameInflector',[ - 'class'=>$inflectorClass - ]); - } + $inflector = Yii::createObject($this->inflector); - protected function createExtractor($extractorClass) - { - return Yii::$container->set('League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor',[ - 'class'=>$extractorClass - ]); - } - - protected function createContainerLocator($mapping = []) - { - return Yii::$container->set('League\Tactician\Handler\Locator\HandlerLocator',[ - 'class'=>'cherif\tactician\ContainerLocator', - ],[$mapping]); - } + $handlerMiddleware = Yii::createObject('League\Tactician\Handler\CommandHandlerMiddleware',[ + $extractor, + $containerLocator, + $inflector + ] + ); - /** - * - */ + $this->registerMiddleware($handlerMiddleware); - protected function createHandlerMiddleware() - { - return Yii::$container->set('commandHandlerMiddleware', - [ - 'class'=>'League\Tactician\Handler\CommandHandlerMiddleware' - ], - [ - Yii::$container->get('League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor'), - Yii::$container->get('League\Tactician\Handler\Locator\HandlerLocator'), - Yii::$container->get('League\Tactician\Handler\MethodNameInflector\MethodNameInflector') + $this->tactician = Yii::createObject('League\Tactician\CommandBus',[ + $this->middlewares ] - ); + ); } - /** - * - */ - protected function createCommandBus() + public function registerMiddleware($middleware) { - $handlerMiddleware = Yii::$container->get('commandHandlerMiddleware'); - Yii::$container->set('commandBus',[ - 'class'=>'League\Tactician\CommandBus' - ], - [[$handlerMiddleware]] - ); + array_push($this->middlewares,$middleware); } + public function handle( $command ) + { + Yii::trace(sprintf('Handle command class for %s', get_class( $command ) ) ); + return $this->tactician->handle( $command ); + } + + public function getMiddlewares() + { + return $this->middlewares; + } - public function __call($name,$args) + public function getTactician() { - try { - $commandBus = Yii::$container->get('commandBus'); - return call_user_func_array([$commandBus,$name], $args); - } catch (Exception $e) { - parent::__call($name,$args); - } + return $this->tactician; } } \ No newline at end of file diff --git a/tests/TacticianTest.php b/tests/TacticianTest.php index fc2f6b6..3f99af8 100644 --- a/tests/TacticianTest.php +++ b/tests/TacticianTest.php @@ -5,69 +5,66 @@ use cherif\tactician\tests\fixtures\commands\CompleteTaskCommand; use cherif\tactician\tests\fixtures\commands\DeleteTaskCommand; +use League\Tactician\Middleware; + class TacticianTests extends TestCase { - /** - * @test - */ - public function should_return_tactician_component() + private $_commandBus; + + protected function setUp() { - $this->assertInstanceOf('cherif\tactician\Tactician',Yii::$app->tactician); + parent::setUp(); + $this->_commandBus = Yii::$app->commandBus; } /** * @test */ - public function should_return_instance_of_container_locator() + public function should_handle_command() { - $containerLocator = Yii::$container->get('League\Tactician\Handler\Locator\HandlerLocator'); - $extractor = Yii::$container->get('League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor'); - $inflector = Yii::$container->get('League\Tactician\Handler\MethodNameInflector\MethodNameInflector'); - - $this->assertInstanceOf('cherif\tactician\containerLocator',$containerLocator); - $this->assertInstanceOf('League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor',$extractor); - $this->assertInstanceOf('League\Tactician\Handler\MethodNameInflector\MethodNameInflector',$inflector); + $this->assertEquals( + 'foobar', + $this->_commandBus->handle(new CompleteTaskCommand()) + ); } /** * @test */ - - public function should_return_insrance_of_command_bus() + public function should_register_middleware_to_be_executed() { - $commandBus = Yii::$container->get('commandBus'); - $this->assertInstanceOf('League\Tactician\CommandBus',$commandBus); - } + $middleware = $this->getMock(Middleware::class,array('execute')); - /** - * @test - */ - public function should_handle_command() - { - $commandBusComponent = Yii::$app->tactician; - $commandBusContainer = Yii::$container->get('commandBus'); + $middleware->expects($this->any()) + ->method('execute') + ->will($this->returnValue('Heelo')); + + $this->_commandBus->registerMiddleware($middleware); + + $tactician = $this->_commandBus->getTactician(); + $this->assertEquals( 'foobar', - $commandBusComponent->handle(new CompleteTaskCommand()) + $this->_commandBus->handle(new CompleteTaskCommand()) ); - $this->assertEquals('foobar', - $commandBusContainer->handle(new CompleteTaskCommand()) - ); + $this->assertEquals( + 'foobar', + $tactician->handle(new CompleteTaskCommand()) + ); } /** - * @test + * @test * @expectedException League\Tactician\Exception\MissingHandlerException */ public function should_throw_exception_on_missing_handler() { - $commandBus = Yii::$container->get('commandBus'); - $commandBus->handle(new DeleteTaskCommand()); + $this->_commandBus->handle(new DeleteTaskCommand()); } } \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 2a3d9a6..04eded6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -27,24 +27,26 @@ protected function tearDown() protected function mockApplication($config = null) { Yii::$container = new Container; - $config = [ - 'id'=>'unit', - 'class'=>'\yii\console\Application', - 'bootstrap'=>['tactician'], - 'name'=>'Tactician Yii2 container unit tests', - 'basePath'=>__DIR__, - 'components'=>[ - 'tactician'=> [ - 'class'=>'cherif\tactician\Tactician', - 'inflector' => 'League\Tactician\Handler\MethodNameInflector\HandleClassNameInflector', - 'extractor' => 'League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor', - 'commandHandlerMap'=> [ - 'cherif\tactician\tests\fixtures\commands\CompleteTaskCommand' => 'cherif\tactician\tests\fixtures\handlers\CompleteTaskCommandHandler', - ], - 'middlewares'=> [], + if( $config === null ) + { + $config = [ + 'id'=>'unit', + 'name'=>'Tactician Yii2 container unit tests', + 'basePath'=>__DIR__, + 'components'=>[ + 'commandBus'=> [ + 'class'=>'cherif\tactician\Tactician', + 'inflector' => 'League\Tactician\Handler\MethodNameInflector\HandleClassNameInflector', + 'extractor' => 'League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor', + 'commandHandlerMap'=> [ + 'cherif\tactician\tests\fixtures\commands\CompleteTaskCommand' => 'cherif\tactician\tests\fixtures\handlers\CompleteTaskCommandHandler' + ] + ] ] - ] - ]; + ]; + } + + $config['class'] = 'yii\console\Application'; Yii::createObject($config); } From 7b72ced5d168bd95995a14b289c7f744d0268e33 Mon Sep 17 00:00:00 2001 From: Cherif BOUCHELAGHEM Date: Mon, 14 Sep 2015 14:08:07 +0100 Subject: [PATCH 2/2] change usage instructions in readme :p --- README.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 58d9480..43da731 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,6 @@ In the configuration file the component must be in the application bootstrap con ```php ... -'bootstrap'=>[...,'commandBus'], -... 'components'=>[ ... 'commandBus'=> [ @@ -38,8 +36,7 @@ In the configuration file the component must be in the application bootstrap con 'extractor' => 'League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor', 'commandHandlerMap'=> [ 'cherif\tactician\tests\fixtures\commands\CompleteTaskCommand' => 'cherif\tactician\tests\fixtures\handlers\CompleteTaskCommandHandler', - ], - 'middlewares'=> [], + ] ] ] ``` @@ -50,4 +47,15 @@ Somewhere in your app (maybe controller): Yii:$app->commandBus->handle(new CompleteTaskCommand) ``` +You can add additional middlewares before handling a command like this: + +```php +$middleware = new MiddlewareClass(); +$command = new MyCommand(); + +Yii::$app->commandBus->registerMiddleware($middleware); + +Yii::$app->commandBus->handle($command); +``` + For more information about configuration please visit [Tactician library homepage](http://tactician.thephpleague.com/). \ No newline at end of file