From 8f86c337f7b72ad68d0a2dc287f5442c98bcfb60 Mon Sep 17 00:00:00 2001 From: Koen Reiniers Date: Tue, 19 Nov 2019 16:25:57 +0100 Subject: [PATCH] Added context to exceptions thrown in apply method --- src/Symfony/Component/Workflow/CHANGELOG.md | 5 +++++ .../NotEnabledTransitionException.php | 4 ++-- .../Exception/TransitionException.php | 9 +++++++- .../UndefinedTransitionException.php | 4 ++-- .../Component/Workflow/Tests/WorkflowTest.php | 21 +++++++++++++++---- src/Symfony/Component/Workflow/Workflow.php | 4 ++-- 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Workflow/CHANGELOG.md b/src/Symfony/Component/Workflow/CHANGELOG.md index c26ab940ba..27c833ad66 100644 --- a/src/Symfony/Component/Workflow/CHANGELOG.md +++ b/src/Symfony/Component/Workflow/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +5.1.0 +----- + + * Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()` + 5.0.0 ----- diff --git a/src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php b/src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php index 7b5b21e724..1771234bf1 100644 --- a/src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php +++ b/src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php @@ -23,9 +23,9 @@ class NotEnabledTransitionException extends TransitionException { private $transitionBlockerList; - public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, TransitionBlockerList $transitionBlockerList) + public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, TransitionBlockerList $transitionBlockerList, array $context = []) { - parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not enabled for workflow "%s".', $transitionName, $workflow->getName())); + parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not enabled for workflow "%s".', $transitionName, $workflow->getName()), $context); $this->transitionBlockerList = $transitionBlockerList; } diff --git a/src/Symfony/Component/Workflow/Exception/TransitionException.php b/src/Symfony/Component/Workflow/Exception/TransitionException.php index a5ace57487..5e35725a38 100644 --- a/src/Symfony/Component/Workflow/Exception/TransitionException.php +++ b/src/Symfony/Component/Workflow/Exception/TransitionException.php @@ -22,14 +22,16 @@ class TransitionException extends LogicException private $subject; private $transitionName; private $workflow; + private $context; - public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, string $message) + public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, string $message, array $context = []) { parent::__construct($message); $this->subject = $subject; $this->transitionName = $transitionName; $this->workflow = $workflow; + $this->context = $context; } public function getSubject() @@ -46,4 +48,9 @@ class TransitionException extends LogicException { return $this->workflow; } + + public function getContext(): array + { + return $this->context; + } } diff --git a/src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php b/src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php index d5d6dab663..75d38486d3 100644 --- a/src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php +++ b/src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php @@ -20,8 +20,8 @@ use Symfony\Component\Workflow\WorkflowInterface; */ class UndefinedTransitionException extends TransitionException { - public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow) + public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, array $context = []) { - parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not defined for workflow "%s".', $transitionName, $workflow->getName())); + parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not defined for workflow "%s".', $transitionName, $workflow->getName()), $context); } } diff --git a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php index 2a5077e588..c14b9bc2f8 100644 --- a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php +++ b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php @@ -9,6 +9,7 @@ use Symfony\Component\Workflow\Event\Event; use Symfony\Component\Workflow\Event\GuardEvent; use Symfony\Component\Workflow\Event\TransitionEvent; use Symfony\Component\Workflow\Exception\NotEnabledTransitionException; +use Symfony\Component\Workflow\Exception\UndefinedTransitionException; use Symfony\Component\Workflow\Marking; use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface; use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore; @@ -252,13 +253,21 @@ class WorkflowTest extends TestCase public function testApplyWithNotExisingTransition() { - $this->expectException('Symfony\Component\Workflow\Exception\UndefinedTransitionException'); - $this->expectExceptionMessage('Transition "404 Not Found" is not defined for workflow "unnamed".'); $definition = $this->createComplexWorkflowDefinition(); $subject = new Subject(); $workflow = new Workflow($definition, new MethodMarkingStore()); + $context = [ + 'lorem' => 'ipsum', + ]; - $workflow->apply($subject, '404 Not Found'); + try { + $workflow->apply($subject, '404 Not Found', $context); + + $this->fail('Should throw an exception'); + } catch (UndefinedTransitionException $e) { + $this->assertSame('Transition "404 Not Found" is not defined for workflow "unnamed".', $e->getMessage()); + $this->assertSame($e->getContext(), $context); + } } public function testApplyWithNotEnabledTransition() @@ -266,9 +275,12 @@ class WorkflowTest extends TestCase $definition = $this->createComplexWorkflowDefinition(); $subject = new Subject(); $workflow = new Workflow($definition, new MethodMarkingStore()); + $context = [ + 'lorem' => 'ipsum', + ]; try { - $workflow->apply($subject, 't2'); + $workflow->apply($subject, 't2', $context); $this->fail('Should throw an exception'); } catch (NotEnabledTransitionException $e) { @@ -279,6 +291,7 @@ class WorkflowTest extends TestCase $this->assertSame($e->getWorkflow(), $workflow); $this->assertSame($e->getSubject(), $subject); $this->assertSame($e->getTransitionName(), 't2'); + $this->assertSame($e->getContext(), $context); } } diff --git a/src/Symfony/Component/Workflow/Workflow.php b/src/Symfony/Component/Workflow/Workflow.php index 6d630d7792..236cb2ecad 100644 --- a/src/Symfony/Component/Workflow/Workflow.php +++ b/src/Symfony/Component/Workflow/Workflow.php @@ -189,11 +189,11 @@ class Workflow implements WorkflowInterface } if (!$transitionBlockerList) { - throw new UndefinedTransitionException($subject, $transitionName, $this); + throw new UndefinedTransitionException($subject, $transitionName, $this, $context); } if (!$applied) { - throw new NotEnabledTransitionException($subject, $transitionName, $this, $transitionBlockerList); + throw new NotEnabledTransitionException($subject, $transitionName, $this, $transitionBlockerList, $context); } return $marking;