Minimalist implementation of CommandBus pattern for test purpose only
https://tactician.thephpleague.com/
Insert official repository in your composer.json
"repositories": [
{
"type": "vcs",
"url": "https://github.com/proyectotau/command-bus.git"
}
],
Require it by composer
composer -vvv require proyectotau/command-bus:dev-master
Create a CommandHandler
class
class CommandHandler {
function handler($cmd){
// run your task here
// you can get public var from cmd if it is an object
}
}
Make a CommandBus
class
$cmdbus = new CommandBus();
And bind
command to that command handler
$cmdbus->bind('MyCommand', $handler);
Command can be an object with parameters
$cmdobj = new CommandObject(true, 1, []);
$cmdbus->bind($cmdobj, $handler);
Finally, dispatch
command
$cmdbus->dispatch('MyCommand');
or
$cmdbus->dispatch($cmdobj);
As a result, handler method will be invoke receiving command as an argument. If it is an object, you could get constructor's params. Let command be an object like this:
class CommandObject {
public $param1;
public $param2;
public $param3;
function __constructor($param1, $param2, $param3) {
$this->param1 = $param1;
$this->param2 = $param2;
$this->param3 = $param3;
}
}
You can pick up them
function handler($cmd){
$this->param1 = $cmd->param1;
$this->param2 = $cmd->param2;
$this->param3 = $cmd->param3;
}
You can run tests like this
vendor/bin/phpunit --color --testdox tests/CommandBusTest.php