-
Notifications
You must be signed in to change notification settings - Fork 4.6k
State pattern is maintainable #285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace DesignPatterns\Behavioral\State; | ||
|
||
class ContextOrder extends StateOrder | ||
{ | ||
public function getState():StateOrder | ||
{ | ||
return static::$state; | ||
} | ||
|
||
public function setState(StateOrder $state) | ||
{ | ||
static::$state = $state; | ||
} | ||
|
||
public function done() | ||
{ | ||
static::$state->done(); | ||
} | ||
|
||
public function getStatus(): string | ||
{ | ||
return static::$state->getStatus(); | ||
} | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace DesignPatterns\Behavioral\State; | ||
|
||
abstract class StateOrder | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $details; | ||
|
||
/** | ||
* @var StateOrder $state | ||
*/ | ||
protected static $state; | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
abstract protected function done(); | ||
|
||
protected function setStatus(string $status) | ||
{ | ||
$this->details['status'] = $status; | ||
$this->details['updatedTime'] = time(); | ||
} | ||
|
||
protected function getStatus(): string | ||
{ | ||
return $this->details['status']; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,33 +2,30 @@ | |
|
||
namespace DesignPatterns\Behavioral\State\Tests; | ||
|
||
use DesignPatterns\Behavioral\State\OrderRepository; | ||
use DesignPatterns\Behavioral\State\ContextOrder; | ||
use DesignPatterns\Behavioral\State\CreateOrder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class StateTest extends TestCase | ||
{ | ||
public function testCanShipCreatedOrder() | ||
{ | ||
$order = (new OrderRepository())->findById(1); | ||
$order->shipOrder(); | ||
$order = new CreateOrder(); | ||
$contextOrder = new ContextOrder(); | ||
$contextOrder->setState($order); | ||
$contextOrder->done(); | ||
|
||
$this->assertEquals('shipping', $order->getStatus()); | ||
$this->assertEquals('shipping', $contextOrder->getStatus()); | ||
} | ||
|
||
public function testCanCompleteShippedOrder() | ||
{ | ||
$order = (new OrderRepository())->findById(2); | ||
$order->completeOrder(); | ||
$order = new CreateOrder(); | ||
$contextOrder = new ContextOrder(); | ||
$contextOrder->setState($order); | ||
$contextOrder->done(); | ||
$contextOrder->done(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's show, that's order from create go to shipping and after to completed. Our states is connected. It's normal behaviour, one order transform from one state to another. |
||
|
||
$this->assertEquals('completed', $order->getStatus()); | ||
} | ||
|
||
/** | ||
* @expectedException \Exception | ||
*/ | ||
public function testThrowsExceptionWhenTryingToCompleteCreatedOrder() | ||
{ | ||
$order = (new OrderRepository())->findById(1); | ||
$order->completeOrder(); | ||
$this->assertEquals('completed', $contextOrder->getStatus()); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PHP 7 sintax should go to
php7
branch #169 (comment). Master branch still PHP 5.6 compatibleUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you say about https://github.com/domnikl/DesignPatternsPHP/blob/master/Structural/Adapter/Book.php#L22? It's master.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, didn't know that php7 branch was merged on master #235