Skip to content

Commit

Permalink
initial scheduler implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alanbem committed Mar 22, 2018
1 parent e7e7ea1 commit 2ec8d7f
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Application/Command/ScheduleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* 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 <[email protected]>
*/
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;
}
}
40 changes: 40 additions & 0 deletions src/Application/Command/ScheduleCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* 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 <[email protected]>
*/
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);
}
}
24 changes: 24 additions & 0 deletions src/Application/Command/ScheduledCommand/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* 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 <[email protected]>
*/
interface Repository
{
public function add(ScheduleCommand $command) : void;
}
65 changes: 65 additions & 0 deletions tests/Application/Command/ScheduleCommandHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* 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 <[email protected]>
*
* @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);
}
}
44 changes: 44 additions & 0 deletions tests/Application/Command/ScheduleCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* 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 <[email protected]>
*
* @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());
}
}

0 comments on commit 2ec8d7f

Please sign in to comment.