diff --git a/src/Application/Command/ScheduleCommand.php b/src/Application/Command/ScheduleCommand.php new file mode 100644 index 00000000..33e533b2 --- /dev/null +++ b/src/Application/Command/ScheduleCommand.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Streak\Application\Command; + +use Streak\Application\Command; + +/** + * @author Alan Gabriel Bem + */ +class ScheduleCommand implements Command +{ + private $command; + + private $when; + + public function __construct(Command $command, \DateTimeInterface $when) + { + $this->command = $command; + $this->when = $when; + } + + public function command() : Command + { + return $this->command; + } + + public function when() : \DateTimeInterface + { + return $this->when; + } +} diff --git a/src/Application/Command/ScheduleCommandHandler.php b/src/Application/Command/ScheduleCommandHandler.php new file mode 100644 index 00000000..96adeaad --- /dev/null +++ b/src/Application/Command/ScheduleCommandHandler.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Streak\Application\Command; + +use Streak\Application\Command; +use Streak\Application\CommandHandler; +use Streak\Application\Exception; + +/** + * @author Alan Gabriel Bem + */ +class ScheduleCommandHandler implements CommandHandler +{ + private $commands; + + public function __construct(ScheduledCommand\Repository $commands) + { + $this->commands = $commands; + } + + public function handle(Command $command) : void + { + if (!$command instanceof ScheduleCommand) { + throw new Exception\CommandNotSupported($command); + } + + $this->commands->add($command); + } +} diff --git a/src/Application/Command/ScheduledCommand/Repository.php b/src/Application/Command/ScheduledCommand/Repository.php new file mode 100644 index 00000000..c60706ec --- /dev/null +++ b/src/Application/Command/ScheduledCommand/Repository.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Streak\Application\Command\ScheduledCommand; + +use Streak\Application\Command\ScheduleCommand; + +/** + * @author Alan Gabriel Bem + */ +interface Repository +{ + public function add(ScheduleCommand $command) : void; +} diff --git a/tests/Application/Command/ScheduleCommandHandlerTest.php b/tests/Application/Command/ScheduleCommandHandlerTest.php new file mode 100644 index 00000000..60c34b1a --- /dev/null +++ b/tests/Application/Command/ScheduleCommandHandlerTest.php @@ -0,0 +1,65 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Streak\Application\Command; + +use PHPUnit\Framework\TestCase; +use Streak\Application\Command; +use Streak\Application\Exception; + +/** + * @author Alan Gabriel Bem + * + * @covers \Streak\Application\Command\ScheduleCommandHandler + */ +class ScheduleCommandHandlerTest extends TestCase +{ + /** + * @var ScheduledCommand\Repository|\PHPUnit_Framework_MockObject_MockObject + */ + private $repository; + + /** + * @var Command|\PHPUnit_Framework_MockObject_MockObject + */ + private $command; + + protected function setUp() + { + $this->repository = $this->getMockBuilder(ScheduledCommand\Repository::class)->getMockForAbstractClass(); + $this->command = $this->getMockBuilder(Command::class)->getMockForAbstractClass(); + } + + public function testSuccess() + { + $command = new ScheduleCommand($this->command, new \DateTime()); + + $this->repository + ->expects($this->once()) + ->method('add') + ->with($command); + + $handler = new ScheduleCommandHandler($this->repository); + $handler->handle($command); + } + + public function testFailure() + { + $handler = new ScheduleCommandHandler($this->repository); + + $exception = new Exception\CommandNotSupported($this->command); + $this->expectExceptionObject($exception); + + $handler->handle($this->command); + } +} diff --git a/tests/Application/Command/ScheduleCommandTest.php b/tests/Application/Command/ScheduleCommandTest.php new file mode 100644 index 00000000..6b388794 --- /dev/null +++ b/tests/Application/Command/ScheduleCommandTest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Streak\Application\Command; + +use PHPUnit\Framework\TestCase; +use Streak\Application\Command; + +/** + * @author Alan Gabriel Bem + * + * @covers \Streak\Application\Command\ScheduleCommand + */ +class ScheduleCommandTest extends TestCase +{ + /** + * @var Command|\PHPUnit_Framework_MockObject_MockObject + */ + private $command; + + protected function setUp() + { + $this->command = $this->getMockBuilder(Command::class)->getMockForAbstractClass(); + } + + public function testCommand() + { + $now = new \DateTime(); + $scheduled = new ScheduleCommand($this->command, $now); + + $this->assertSame($this->command, $scheduled->command()); + $this->assertSame($now, $scheduled->when()); + } +}