From 2a88c3c401582c49c27de96d45ed23a1cca06687 Mon Sep 17 00:00:00 2001 From: Andrew Tch Date: Sun, 18 Mar 2018 12:22:42 +0200 Subject: [PATCH] implemented TransitionException to be thrown instead of Logic exception --- .../Exception/TransitionException.php | 66 +++++++++++++++++++ .../Component/Workflow/Tests/WorkflowTest.php | 19 +++++- src/Symfony/Component/Workflow/Workflow.php | 3 +- 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Workflow/Exception/TransitionException.php diff --git a/src/Symfony/Component/Workflow/Exception/TransitionException.php b/src/Symfony/Component/Workflow/Exception/TransitionException.php new file mode 100644 index 0000000000000..2abc6cdc12cca --- /dev/null +++ b/src/Symfony/Component/Workflow/Exception/TransitionException.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Workflow\Exception; + +/** + * @author Andrew Tch + */ +class TransitionException extends LogicException +{ + /** + * @var mixed + */ + private $subject; + + /** + * @var string + */ + private $transitionName; + + /** + * @var string + */ + private $workflowName; + + public function __construct($subject, $transitionName, $workflowName) + { + $this->subject = $subject; + $this->transitionName = $transitionName; + $this->workflowName = $workflowName; + + parent::__construct(sprintf('Unable to apply transition "%s" for workflow "%s".', $this->transitionName, $this->workflowName)); + } + + /** + * @return mixed + */ + public function getSubject() + { + return $this->subject; + } + + /** + * @return string + */ + public function getTransitionName() + { + return $this->transitionName; + } + + /** + * @return string + */ + public function getWorkflowName() + { + return $this->workflowName; + } +} diff --git a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php index 547fb03e82cfe..72bf883e6aa89 100644 --- a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php +++ b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php @@ -7,6 +7,7 @@ use Symfony\Component\Workflow\Definition; use Symfony\Component\Workflow\Event\Event; use Symfony\Component\Workflow\Event\GuardEvent; +use Symfony\Component\Workflow\Exception\TransitionException; use Symfony\Component\Workflow\Marking; use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface; use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore; @@ -163,7 +164,7 @@ public function testCanDoesNotTriggerGuardEventsForNotEnabledTransitions() } /** - * @expectedException \Symfony\Component\Workflow\Exception\LogicException + * @expectedException \Symfony\Component\Workflow\Exception\TransitionException * @expectedExceptionMessage Unable to apply transition "t2" for workflow "unnamed". */ public function testApplyWithImpossibleTransition() @@ -176,6 +177,22 @@ public function testApplyWithImpossibleTransition() $workflow->apply($subject, 't2'); } + public function testTransitionExceptionHasCorrectFields() + { + $definition = $this->createComplexWorkflowDefinition(); + $subject = new \stdClass(); + $subject->marking = null; + $workflow = new Workflow($definition, new MultipleStateMarkingStore()); + + try { + $workflow->apply($subject, 't2'); + } catch (TransitionException $exception) { + $this->assertEquals('t2', $exception->getTransitionName()); + $this->assertEquals('unnamed', $exception->getWorkflowName()); + $this->assertEquals($subject, $exception->getSubject()); + } + } + public function testCanWithSameNameTransition() { $definition = $this->createWorkflowWithSameNameTransition(); diff --git a/src/Symfony/Component/Workflow/Workflow.php b/src/Symfony/Component/Workflow/Workflow.php index ac52794c2dc8d..1c53b79a9c939 100644 --- a/src/Symfony/Component/Workflow/Workflow.php +++ b/src/Symfony/Component/Workflow/Workflow.php @@ -15,6 +15,7 @@ use Symfony\Component\Workflow\Event\Event; use Symfony\Component\Workflow\Event\GuardEvent; use Symfony\Component\Workflow\Exception\LogicException; +use Symfony\Component\Workflow\Exception\TransitionException; use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface; use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore; @@ -138,7 +139,7 @@ public function apply($subject, $transitionName) } if (!$applied) { - throw new LogicException(sprintf('Unable to apply transition "%s" for workflow "%s".', $transitionName, $this->name)); + throw new TransitionException($subject, $transitionName, $this->name); } return $marking;