Skip to content

Commit

Permalink
Merge pull request #10 from ericksonreyes/rename-command-handler
Browse files Browse the repository at this point in the history
Renamed command handler to simply command bus.
  • Loading branch information
ericksonreyes authored May 14, 2019
2 parents a129e33 + d044741 commit ddb3738
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,68 @@

namespace spec\EricksonReyes\DomainDrivenDesign\Application;

use EricksonReyes\DomainDrivenDesign\Application\CommandHandler;
use EricksonReyes\DomainDrivenDesign\Application\CommandBus;
use EricksonReyes\DomainDrivenDesign\Application\Exception\DuplicateCommandHandlerException;
use EricksonReyes\DomainDrivenDesign\Application\Exception\MissingHandlerMethodException;
use EricksonReyes\DomainDrivenDesign\Application\Exception\UnhandledCommandException;
use PhpSpec\ObjectBehavior;

class CommandHandlerSpec extends ObjectBehavior
class CommandBusSpec extends ObjectBehavior
{
/**
* @var Handler
* @var MockHandler
*/
private $handler;

/**
* @var Command
* @var MockCommand
*/
private $command;

public function let()
{
$this->command = new Command();
$this->handler = new Handler();
$this->command = new MockCommand();
$this->handler = new MockHandler();
}

public function it_is_initializable()
{
$this->shouldHaveType(CommandHandler::class);
$this->shouldHaveType(CommandBus::class);
}

public function it_accepts_handlers()
{
$this->addHandler(new Handler(), Command::class)->shouldBeNull();
$this->addHandler(new MockHandler(), MockCommand::class)->shouldBeNull();
}

public function it_executes_commands()
{
$this->addHandler(new Handler(), Command::class);
$this->execute(new Command())->shouldReturn([Handler::class => Command::name()]);
$this->addHandler(new MockHandler(), MockCommand::class);
$this->execute(new MockCommand())->shouldReturn([MockHandler::class => MockCommand::name()]);
}

public function it_requires_a_handler_for_commands()
{
$this->shouldThrow(UnhandledCommandException::class)->during('execute', [
new Command()
new MockCommand()
]);
}

public function it_requires_handler_to_have_handler_methods()
{
$this->shouldThrow(MissingHandlerMethodException::class)->during('addHandler', [
new EmptyHandler(),
Command::class
new MockEmptyHandler(),
MockCommand::class
]);
}

public function it_prevents_duplicate_handler_for_a_command()
{
$handler = new Handler();
$this->addHandler($handler, Command::class);
$handler = new MockHandler();
$this->addHandler($handler, MockCommand::class);
$this->shouldThrow(DuplicateCommandHandlerException::class)->during('addHandler', [
$handler,
Command::class
MockCommand::class
]);
}
}

class Command
{
public static function name()
{
return __CLASS__;
}
}

class Handler
{
public function handleThis(Command $command)
{
return $command::name();
}
}

class EmptyHandler
{

}
12 changes: 12 additions & 0 deletions spec/EricksonReyes/DomainDrivenDesign/Application/MockCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace spec\EricksonReyes\DomainDrivenDesign\Application;


class MockCommand
{
public static function name()
{
return __CLASS__;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace spec\EricksonReyes\DomainDrivenDesign\Application;


class MockEmptyHandler
{

}
12 changes: 12 additions & 0 deletions spec/EricksonReyes/DomainDrivenDesign/Application/MockHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace spec\EricksonReyes\DomainDrivenDesign\Application;


class MockHandler
{
public function handleThis(MockCommand $command)
{
return $command::name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Class CommandHandler
* @package EricksonReyes\DomainDrivenDesign\Application
*/
class CommandHandler
class CommandBus
{

/**
Expand Down

0 comments on commit ddb3738

Please sign in to comment.