mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-05 06:27:25 +02:00
Merge pull request #503 from mth-bou/fix/state-pattern
Fix: State pattern
This commit is contained in:
@@ -4,29 +4,29 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
class OrderContext
|
class ContextOrder
|
||||||
{
|
{
|
||||||
private State $state;
|
private StateOrder $state;
|
||||||
|
|
||||||
public static function create(): OrderContext
|
public static function create(): ContextOrder
|
||||||
{
|
{
|
||||||
$order = new self();
|
$order = new self();
|
||||||
$order->state = new StateCreated();
|
$order->state = new CreateOrder();
|
||||||
|
|
||||||
return $order;
|
return $order;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setState(State $state)
|
public function setState(StateOrder $state): void
|
||||||
{
|
{
|
||||||
$this->state = $state;
|
$this->state = $state;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function proceedToNext()
|
public function proceedToNext(): void
|
||||||
{
|
{
|
||||||
$this->state->proceedToNext($this);
|
$this->state->proceedToNext($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toString()
|
public function toString(): string
|
||||||
{
|
{
|
||||||
return $this->state->toString();
|
return $this->state->toString();
|
||||||
}
|
}
|
@@ -4,11 +4,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
class StateCreated implements State
|
class CreateOrder implements StateOrder
|
||||||
{
|
{
|
||||||
public function proceedToNext(OrderContext $context)
|
public function proceedToNext(ContextOrder $context): void
|
||||||
{
|
{
|
||||||
$context->setState(new StateShipped());
|
$context->setState(new ShippingOrder());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toString(): string
|
public function toString(): string
|
@@ -4,9 +4,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
class StateDone implements State
|
class OrderDone implements StateOrder
|
||||||
{
|
{
|
||||||
public function proceedToNext(OrderContext $context)
|
public function proceedToNext(ContextOrder $context): void
|
||||||
{
|
{
|
||||||
// there is nothing more to do
|
// there is nothing more to do
|
||||||
}
|
}
|
@@ -20,33 +20,33 @@ Code
|
|||||||
|
|
||||||
You can also find this code on `GitHub`_
|
You can also find this code on `GitHub`_
|
||||||
|
|
||||||
OrderContext.php
|
ContextOrder.php
|
||||||
|
|
||||||
.. literalinclude:: OrderContext.php
|
.. literalinclude:: ContextOrder.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
State.php
|
StateOrder.php
|
||||||
|
|
||||||
.. literalinclude:: State.php
|
.. literalinclude:: StateOrder.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
StateCreated.php
|
CreateOrder.php
|
||||||
|
|
||||||
.. literalinclude:: StateCreated.php
|
.. literalinclude:: CreateOrder.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
StateShipped.php
|
ShippingOrder.php
|
||||||
|
|
||||||
.. literalinclude:: StateShipped.php
|
.. literalinclude:: ShippingOrder.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
StateDone.php
|
OrderDone.php
|
||||||
|
|
||||||
.. literalinclude:: StateDone.php
|
.. literalinclude:: OrderDone.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
|
@@ -4,11 +4,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
class StateShipped implements State
|
class ShippingOrder implements StateOrder
|
||||||
{
|
{
|
||||||
public function proceedToNext(OrderContext $context)
|
public function proceedToNext(ContextOrder $context): void
|
||||||
{
|
{
|
||||||
$context->setState(new StateDone());
|
$context->setState(new OrderDone());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toString(): string
|
public function toString(): string
|
@@ -4,9 +4,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
interface State
|
interface StateOrder
|
||||||
{
|
{
|
||||||
public function proceedToNext(OrderContext $context);
|
public function proceedToNext(ContextOrder $context): void;
|
||||||
|
|
||||||
public function toString(): string;
|
public function toString(): string;
|
||||||
}
|
}
|
@@ -4,38 +4,38 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State\Tests;
|
namespace DesignPatterns\Behavioral\State\Tests;
|
||||||
|
|
||||||
use DesignPatterns\Behavioral\State\OrderContext;
|
use DesignPatterns\Behavioral\State\ContextOrder;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class StateTest extends TestCase
|
class StateTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testIsCreatedWithStateCreated()
|
public function testIsCreatedWithStateCreated(): void
|
||||||
{
|
{
|
||||||
$orderContext = OrderContext::create();
|
$orderContext = ContextOrder::create();
|
||||||
|
|
||||||
$this->assertSame('created', $orderContext->toString());
|
$this->assertSame('created', $orderContext->toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCanProceedToStateShipped()
|
public function testCanProceedToStateShipped(): void
|
||||||
{
|
{
|
||||||
$contextOrder = OrderContext::create();
|
$contextOrder = ContextOrder::create();
|
||||||
$contextOrder->proceedToNext();
|
$contextOrder->proceedToNext();
|
||||||
|
|
||||||
$this->assertSame('shipped', $contextOrder->toString());
|
$this->assertSame('shipped', $contextOrder->toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCanProceedToStateDone()
|
public function testCanProceedToStateDone(): void
|
||||||
{
|
{
|
||||||
$contextOrder = OrderContext::create();
|
$contextOrder = ContextOrder::create();
|
||||||
$contextOrder->proceedToNext();
|
$contextOrder->proceedToNext();
|
||||||
$contextOrder->proceedToNext();
|
$contextOrder->proceedToNext();
|
||||||
|
|
||||||
$this->assertSame('done', $contextOrder->toString());
|
$this->assertSame('done', $contextOrder->toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testStateDoneIsTheLastPossibleState()
|
public function testStateDoneIsTheLastPossibleState(): void
|
||||||
{
|
{
|
||||||
$contextOrder = OrderContext::create();
|
$contextOrder = ContextOrder::create();
|
||||||
$contextOrder->proceedToNext();
|
$contextOrder->proceedToNext();
|
||||||
$contextOrder->proceedToNext();
|
$contextOrder->proceedToNext();
|
||||||
$contextOrder->proceedToNext();
|
$contextOrder->proceedToNext();
|
||||||
|
Reference in New Issue
Block a user