From a6b09d6b18edcca1bde40c8102b3b3ac0b878e2a Mon Sep 17 00:00:00 2001 From: Dominik Liebler Date: Thu, 22 Sep 2016 09:25:26 +0200 Subject: [PATCH] PHP7 Command --- Behavioral/Command/AddMessageDateCommand.php | 4 +- Behavioral/Command/CommandInterface.php | 3 -- Behavioral/Command/HelloCommand.php | 9 ++-- Behavioral/Command/Invoker.php | 10 ++--- Behavioral/Command/Receiver.php | 18 +++++--- Behavioral/Command/Tests/CommandTest.php | 28 +++---------- .../Command/Tests/UndoableCommandTest.php | 41 ++++++------------- .../Command/UndoableCommandInterface.php | 4 -- 8 files changed, 40 insertions(+), 77 deletions(-) diff --git a/Behavioral/Command/AddMessageDateCommand.php b/Behavioral/Command/AddMessageDateCommand.php index 11bb9af..fcaeb4f 100644 --- a/Behavioral/Command/AddMessageDateCommand.php +++ b/Behavioral/Command/AddMessageDateCommand.php @@ -4,14 +4,14 @@ namespace DesignPatterns\Behavioral\Command; /** * This concrete command tweaks receiver to add current date to messages - * invoker just knows that it can call "execute". + * invoker just knows that it can call "execute" */ class AddMessageDateCommand implements UndoableCommandInterface { /** * @var Receiver */ - protected $output; + private $output; /** * Each concrete command is built with different receivers. diff --git a/Behavioral/Command/CommandInterface.php b/Behavioral/Command/CommandInterface.php index cd9d9c6..265e862 100644 --- a/Behavioral/Command/CommandInterface.php +++ b/Behavioral/Command/CommandInterface.php @@ -2,9 +2,6 @@ namespace DesignPatterns\Behavioral\Command; -/** - * class CommandInterface. - */ interface CommandInterface { /** diff --git a/Behavioral/Command/HelloCommand.php b/Behavioral/Command/HelloCommand.php index 94d4723..1dbfaf5 100644 --- a/Behavioral/Command/HelloCommand.php +++ b/Behavioral/Command/HelloCommand.php @@ -4,18 +4,18 @@ namespace DesignPatterns\Behavioral\Command; /** * This concrete command calls "print" on the Receiver, but an external - * invoker just knows that it can call "execute". + * invoker just knows that it can call "execute" */ class HelloCommand implements CommandInterface { /** * @var Receiver */ - protected $output; + private $output; /** * Each concrete command is built with different receivers. - * There can be one, many or completely no receivers, but there can be other commands in the parameters. + * There can be one, many or completely no receivers, but there can be other commands in the parameters * * @param Receiver $console */ @@ -29,8 +29,7 @@ class HelloCommand implements CommandInterface */ public function execute() { - // sometimes, there is no receiver and this is the command which - // does all the work + // sometimes, there is no receiver and this is the command which does all the work $this->output->write('Hello World'); } } diff --git a/Behavioral/Command/Invoker.php b/Behavioral/Command/Invoker.php index 7942adb..c6e7f93 100644 --- a/Behavioral/Command/Invoker.php +++ b/Behavioral/Command/Invoker.php @@ -11,11 +11,11 @@ class Invoker /** * @var CommandInterface */ - protected $command; + private $command; /** - * In the invoker we find this kind of method for subscribing the command. - * There can be also a stack, a list, a fixed set... + * in the invoker we find this kind of method for subscribing the command + * There can be also a stack, a list, a fixed set ... * * @param CommandInterface $cmd */ @@ -25,12 +25,10 @@ class Invoker } /** - * executes the command. + * executes the command; the invoker is the same whatever is the command */ public function run() { - // here is a key feature of the invoker - // the invoker is the same whatever is the command $this->command->execute(); } } diff --git a/Behavioral/Command/Receiver.php b/Behavioral/Command/Receiver.php index 956ce74..7ca6343 100644 --- a/Behavioral/Command/Receiver.php +++ b/Behavioral/Command/Receiver.php @@ -7,14 +7,20 @@ namespace DesignPatterns\Behavioral\Command; */ class Receiver { + /** + * @var bool + */ private $enableDate = false; - private $output = array(); + /** + * @var string[] + */ + private $output = []; /** * @param string $str */ - public function write($str) + public function write(string $str) { if ($this->enableDate) { $str .= ' ['.date('Y-m-d').']'; @@ -23,13 +29,13 @@ class Receiver $this->output[] = $str; } - public function getOutput() + public function getOutput(): string { - return implode("\n", $this->output); + return join("\n", $this->output); } /** - * Enable receiver to display message date. + * Enable receiver to display message date */ public function enableDate() { @@ -37,7 +43,7 @@ class Receiver } /** - * Disable receiver to display message date. + * Disable receiver to display message date */ public function disableDate() { diff --git a/Behavioral/Command/Tests/CommandTest.php b/Behavioral/Command/Tests/CommandTest.php index 3852495..cd3ea03 100644 --- a/Behavioral/Command/Tests/CommandTest.php +++ b/Behavioral/Command/Tests/CommandTest.php @@ -6,31 +6,15 @@ use DesignPatterns\Behavioral\Command\HelloCommand; use DesignPatterns\Behavioral\Command\Invoker; use DesignPatterns\Behavioral\Command\Receiver; -/** - * CommandTest has the role of the Client in the Command Pattern. - */ class CommandTest extends \PHPUnit_Framework_TestCase { - /** - * @var Invoker - */ - protected $invoker; - - /** - * @var Receiver - */ - protected $receiver; - - protected function setUp() - { - $this->invoker = new Invoker(); - $this->receiver = new Receiver(); - } - public function testInvocation() { - $this->invoker->setCommand(new HelloCommand($this->receiver)); - $this->invoker->run(); - $this->assertEquals($this->receiver->getOutput(), 'Hello World'); + $invoker = new Invoker(); + $receiver = new Receiver(); + + $invoker->setCommand(new HelloCommand($receiver)); + $invoker->run(); + $this->assertEquals($receiver->getOutput(), 'Hello World'); } } diff --git a/Behavioral/Command/Tests/UndoableCommandTest.php b/Behavioral/Command/Tests/UndoableCommandTest.php index 5302a7b..8d10259 100644 --- a/Behavioral/Command/Tests/UndoableCommandTest.php +++ b/Behavioral/Command/Tests/UndoableCommandTest.php @@ -6,44 +6,27 @@ use DesignPatterns\Behavioral\Command\AddMessageDateCommand; use DesignPatterns\Behavioral\Command\HelloCommand; use DesignPatterns\Behavioral\Command\Invoker; use DesignPatterns\Behavioral\Command\Receiver; -use PHPUnit_Framework_TestCase; -/** - * UndoableCommandTest has the role of the Client in the Command Pattern. - */ -class UndoableCommandTest extends PHPUnit_Framework_TestCase +class UndoableCommandTest extends \PHPUnit_Framework_TestCase { - /** - * @var Invoker - */ - protected $invoker; - - /** - * @var Receiver - */ - protected $receiver; - - protected function setUp() - { - $this->invoker = new Invoker(); - $this->receiver = new Receiver(); - } - public function testInvocation() { - $this->invoker->setCommand(new HelloCommand($this->receiver)); - $this->invoker->run(); - $this->assertEquals($this->receiver->getOutput(), 'Hello World'); + $invoker = new Invoker(); + $receiver = new Receiver(); - $messageDateCommand = new AddMessageDateCommand($this->receiver); + $invoker->setCommand(new HelloCommand($receiver)); + $invoker->run(); + $this->assertEquals($receiver->getOutput(), 'Hello World'); + + $messageDateCommand = new AddMessageDateCommand($receiver); $messageDateCommand->execute(); - $this->invoker->run(); - $this->assertEquals($this->receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d').']'); + $invoker->run(); + $this->assertEquals($receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d').']'); $messageDateCommand->undo(); - $this->invoker->run(); - $this->assertEquals($this->receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d')."]\nHello World"); + $invoker->run(); + $this->assertEquals($receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d')."]\nHello World"); } } diff --git a/Behavioral/Command/UndoableCommandInterface.php b/Behavioral/Command/UndoableCommandInterface.php index f9234ab..52b02e9 100644 --- a/Behavioral/Command/UndoableCommandInterface.php +++ b/Behavioral/Command/UndoableCommandInterface.php @@ -2,14 +2,10 @@ namespace DesignPatterns\Behavioral\Command; -/** - * Interface UndoableCommandInterface. - */ interface UndoableCommandInterface extends CommandInterface { /** * This method is used to undo change made by command execution - * The Receiver goes in the constructor. */ public function undo(); }