Merge pull request #503 from mth-bou/fix/state-pattern

Fix: State pattern
This commit is contained in:
Borislav Sabev
2024-06-05 11:12:17 +03:00
committed by GitHub
7 changed files with 36 additions and 36 deletions

View File

@@ -4,29 +4,29 @@ declare(strict_types=1);
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->state = new StateCreated();
$order->state = new CreateOrder();
return $order;
}
public function setState(State $state)
public function setState(StateOrder $state): void
{
$this->state = $state;
}
public function proceedToNext()
public function proceedToNext(): void
{
$this->state->proceedToNext($this);
}
public function toString()
public function toString(): string
{
return $this->state->toString();
}

View File

@@ -4,11 +4,11 @@ declare(strict_types=1);
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

View File

@@ -4,9 +4,9 @@ declare(strict_types=1);
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
}

View File

@@ -20,33 +20,33 @@ Code
You can also find this code on `GitHub`_
OrderContext.php
ContextOrder.php
.. literalinclude:: OrderContext.php
.. literalinclude:: ContextOrder.php
:language: php
:linenos:
State.php
StateOrder.php
.. literalinclude:: State.php
.. literalinclude:: StateOrder.php
:language: php
:linenos:
StateCreated.php
CreateOrder.php
.. literalinclude:: StateCreated.php
.. literalinclude:: CreateOrder.php
:language: php
:linenos:
StateShipped.php
ShippingOrder.php
.. literalinclude:: StateShipped.php
.. literalinclude:: ShippingOrder.php
:language: php
:linenos:
StateDone.php
OrderDone.php
.. literalinclude:: StateDone.php
.. literalinclude:: OrderDone.php
:language: php
:linenos:

View File

@@ -4,11 +4,11 @@ declare(strict_types=1);
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

View File

@@ -4,9 +4,9 @@ declare(strict_types=1);
namespace DesignPatterns\Behavioral\State;
interface State
interface StateOrder
{
public function proceedToNext(OrderContext $context);
public function proceedToNext(ContextOrder $context): void;
public function toString(): string;
}

View File

@@ -4,38 +4,38 @@ declare(strict_types=1);
namespace DesignPatterns\Behavioral\State\Tests;
use DesignPatterns\Behavioral\State\OrderContext;
use DesignPatterns\Behavioral\State\ContextOrder;
use PHPUnit\Framework\TestCase;
class StateTest extends TestCase
{
public function testIsCreatedWithStateCreated()
public function testIsCreatedWithStateCreated(): void
{
$orderContext = OrderContext::create();
$orderContext = ContextOrder::create();
$this->assertSame('created', $orderContext->toString());
}
public function testCanProceedToStateShipped()
public function testCanProceedToStateShipped(): void
{
$contextOrder = OrderContext::create();
$contextOrder = ContextOrder::create();
$contextOrder->proceedToNext();
$this->assertSame('shipped', $contextOrder->toString());
}
public function testCanProceedToStateDone()
public function testCanProceedToStateDone(): void
{
$contextOrder = OrderContext::create();
$contextOrder = ContextOrder::create();
$contextOrder->proceedToNext();
$contextOrder->proceedToNext();
$this->assertSame('done', $contextOrder->toString());
}
public function testStateDoneIsTheLastPossibleState()
public function testStateDoneIsTheLastPossibleState(): void
{
$contextOrder = OrderContext::create();
$contextOrder = ContextOrder::create();
$contextOrder->proceedToNext();
$contextOrder->proceedToNext();
$contextOrder->proceedToNext();