From dddfdf65fc55b06088c268c88384df41ccb63811 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Sat, 16 Jan 2016 17:06:43 +0300 Subject: [PATCH] 88 - Added undo example for command pattern --- Behavioral/Command/AddMessageDateCommand.php | 46 +++++++++++++++++ Behavioral/Command/Receiver.php | 31 +++++++++++- Behavioral/Command/Tests/CommandTest.php | 2 +- .../Command/Tests/UndoableCommandTest.php | 49 +++++++++++++++++++ .../Command/UndoableCommandInterface.php | 15 ++++++ 5 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 Behavioral/Command/AddMessageDateCommand.php create mode 100644 Behavioral/Command/Tests/UndoableCommandTest.php create mode 100644 Behavioral/Command/UndoableCommandInterface.php diff --git a/Behavioral/Command/AddMessageDateCommand.php b/Behavioral/Command/AddMessageDateCommand.php new file mode 100644 index 0000000..9543bda --- /dev/null +++ b/Behavioral/Command/AddMessageDateCommand.php @@ -0,0 +1,46 @@ +output = $console; + } + + /** + * Execute and make receiver to enable displaying messages date. + */ + public function execute() + { + // sometimes, there is no receiver and this is the command which + // does all the work + $this->output->enableDate(); + } + + /** + * Undo the command and make receiver to disable displaying messages date. + */ + public function undo() + { + // sometimes, there is no receiver and this is the command which + // does all the work + $this->output->disableDate(); + } +} diff --git a/Behavioral/Command/Receiver.php b/Behavioral/Command/Receiver.php index ccf910b..956ce74 100644 --- a/Behavioral/Command/Receiver.php +++ b/Behavioral/Command/Receiver.php @@ -7,11 +7,40 @@ namespace DesignPatterns\Behavioral\Command; */ class Receiver { + private $enableDate = false; + + private $output = array(); + /** * @param string $str */ public function write($str) { - echo $str; + if ($this->enableDate) { + $str .= ' ['.date('Y-m-d').']'; + } + + $this->output[] = $str; + } + + public function getOutput() + { + return implode("\n", $this->output); + } + + /** + * Enable receiver to display message date. + */ + public function enableDate() + { + $this->enableDate = true; + } + + /** + * Disable receiver to display message date. + */ + public function disableDate() + { + $this->enableDate = false; } } diff --git a/Behavioral/Command/Tests/CommandTest.php b/Behavioral/Command/Tests/CommandTest.php index 9d9aa51..3852495 100644 --- a/Behavioral/Command/Tests/CommandTest.php +++ b/Behavioral/Command/Tests/CommandTest.php @@ -30,7 +30,7 @@ class CommandTest extends \PHPUnit_Framework_TestCase public function testInvocation() { $this->invoker->setCommand(new HelloCommand($this->receiver)); - $this->expectOutputString('Hello World'); $this->invoker->run(); + $this->assertEquals($this->receiver->getOutput(), 'Hello World'); } } diff --git a/Behavioral/Command/Tests/UndoableCommandTest.php b/Behavioral/Command/Tests/UndoableCommandTest.php new file mode 100644 index 0000000..5302a7b --- /dev/null +++ b/Behavioral/Command/Tests/UndoableCommandTest.php @@ -0,0 +1,49 @@ +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'); + + $messageDateCommand = new AddMessageDateCommand($this->receiver); + $messageDateCommand->execute(); + + $this->invoker->run(); + $this->assertEquals($this->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"); + } +} diff --git a/Behavioral/Command/UndoableCommandInterface.php b/Behavioral/Command/UndoableCommandInterface.php new file mode 100644 index 0000000..0fd101c --- /dev/null +++ b/Behavioral/Command/UndoableCommandInterface.php @@ -0,0 +1,15 @@ +