Skip to content

Commit

Permalink
Merge pull request #1 from cherifGsoul/remove_bootstraping
Browse files Browse the repository at this point in the history
Remove bootstraping
  • Loading branch information
Mohamed Cherif Bouchelaghem committed Sep 14, 2015
2 parents 9198210 + 7b72ced commit 39d2941
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 116 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ In the configuration file the component must be in the application bootstrap con

```php
...
'bootstrap'=>[...,'commandBus'],
...
'components'=>[
...
'commandBus'=> [
Expand All @@ -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'=> [],
]
]
]
```
Expand All @@ -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/).
98 changes: 35 additions & 63 deletions Tactician.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
61 changes: 29 additions & 32 deletions tests/TacticianTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
36 changes: 19 additions & 17 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 39d2941

Please sign in to comment.