From a178f9287fe131aa30b59d509d3403d75f046a81 Mon Sep 17 00:00:00 2001 From: Patrick van Loon Date: Fri, 2 Jul 2021 17:49:51 +0200 Subject: [PATCH 1/2] Only initialize the flow when it is needed. --- src/FormFlow.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/FormFlow.php b/src/FormFlow.php index ef6ea9c..92e7ebf 100644 --- a/src/FormFlow.php +++ b/src/FormFlow.php @@ -54,11 +54,14 @@ public function __construct( $this->storageManager = $storageManager; $this->formFactory = $formFactory; $this->definition = $definition; - $this->initialize(); } public function isStarted(): bool { + if (null === $this->context) { + $this->initialize(); + } + return null !== $this->context; } @@ -74,7 +77,7 @@ public function start(object $data): void public function getContext(): Context { - if (null === $this->context) { + if (!$this->isStarted()) { throw new LogicException('The flow is missing it\'s context. Did you FormFlow#start() the flow?'); } From c71610dd3a639a35576707af80ecdf0549923658 Mon Sep 17 00:00:00 2001 From: Patrick van Loon Date: Fri, 2 Jul 2021 18:51:57 +0200 Subject: [PATCH 2/2] Update unit tests. --- tests/FormFlowTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/FormFlowTest.php b/tests/FormFlowTest.php index 35f67a2..eb65dd4 100644 --- a/tests/FormFlowTest.php +++ b/tests/FormFlowTest.php @@ -40,7 +40,8 @@ public function testInitialize(): void $this->mockedStorageManager->expects(self::once())->method('load') ->with('form_flow.storage.form_flow')->willReturn($this->createStub(Context::class)); - $this->createDefaultFormFlow(); + $flow = $this->createDefaultFormFlow(); + self::assertTrue($flow->isStarted()); } public function testInitializeWithCorruptedContext(): void @@ -52,7 +53,8 @@ public function testInitializeWithCorruptedContext(): void $this->expectException(UnexpectedValueException::class); $this->expectExceptionMessage('The stored context is corrupted.'); - $this->createDefaultFormFlow(); + $flow = $this->createDefaultFormFlow(); + self::assertTrue($flow->isStarted()); } public function testStartWhenFlowIsAlreadyStarted(): void