Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <alan.bem@gmail.com>
*
* 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 <alan.bem@gmail.com>
*/
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 <alan.bem@gmail.com>
*
* 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 <alan.bem@gmail.com>
*/
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 <alan.bem@gmail.com>
*
* 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 <alan.bem@gmail.com>
*/
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 <alan.bem@gmail.com>
*
* 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 <alan.bem@gmail.com>
*
* @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 <alan.bem@gmail.com>
*
* 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 <alan.bem@gmail.com>
*
* @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());
}
}