8000 Added context to exceptions thrown in apply method · linaori/symfony@8f86c33 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f86c33

Browse files
koenreiniersfabpot
authored andcommitted
Added context to exceptions thrown in apply method
1 parent d2649f2 commit 8f86c33

File tree

6 files changed

+36
-11
lines changed

6 files changed

+36
-11
lines changed

src/Symfony/Component/Workflow/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()`
8+
49
5.0.0
510
-----
611

src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class NotEnabledTransitionException extends TransitionException
2323
{
2424
private $transitionBlockerList;
2525

26-
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, TransitionBlockerList $transitionBlockerList)
26+
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, TransitionBlockerList $transitionBlockerList, array $context = [])
2727
{
28-
parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not enabled for workflow "%s".', $transitionName, $workflow->getName()));
28+
parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not enabled for workflow "%s".', $transitionName, $workflow->getName()), $context);
2929

3030
$this->transitionBlockerList = $transitionBlockerList;
3131
}

src/Symfony/Component/Workflow/Exception/TransitionException.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ class TransitionException extends LogicException
2222
private $subject;
2323
private $transitionName;
2424
private $workflow;
25+
private $context;
2526

26-
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, string $message)
27+
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, string $message, array $context = [])
2728
{
2829
parent::__construct($message);
2930

3031
$this->subject = $subject;
3132
$this->transitionName = $transitionName;
3233
$this->workflow = $workflow;
34+
$this->context = $context;
3335
}
3436

3537
public function getSubject()
@@ -46,4 +48,9 @@ public function getWorkflow(): WorkflowInterface
4648
{
4749
return $this->workflow;
4850
}
51+
52+
public function getContext(): array
53+
{
54+
return $this->context;
55+
}
4956
}

src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
*/
2121
class UndefinedTransitionException extends TransitionException
2222
{
23-
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow)
23+
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, array $context = [])
2424
{
25-
parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not defined for workflow "%s".', $transitionName, $workflow->getName()));
25+
parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not defined for workflow "%s".', $transitionName, $workflow->getName()), $context);
2626
}
2727
}

src/Symfony/Component/Workflow/Tests/WorkflowTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Workflow\Event\GuardEvent;
1010
use Symfony\Component\Workflow\Event\TransitionEvent;
1111
use Symfony\Component\Workflow\Exception\NotEnabledTransitionException;
12+
use Symfony\Component\Workflow\Exception\UndefinedTransitionException;
1213
use Symfony\Component\Workflow\Marking;
1314
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
1415
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
@@ -252,23 +253,34 @@ public function testBuildTransitionBlockerListReturnsReasonsProvidedInGuards()
252253

253254
public function testApplyWithNotExisingTransition()
254255
{
255-
$this->expectException('Symfony\Component\Workflow\Exception\UndefinedTransitionException');
256-
$this->expectExceptionMessage('Transition "404 Not Found" is not defined for workflow "unnamed".');
257256
$definition = $this->createComplexWorkflowDefinition();
258257
$subject = new Subject();
259258
$workflow = new Workflow($definition, new MethodMarkingStore());
259+
$context = [
260+
'lorem' => 'ipsum',
261+
];
262+
263+
try {
264+
$workflow->apply($subject, '404 Not Found', $context);
260265

261-
$workflow->apply($subject, '404 Not Found');
266+
$this->fail('Should throw an exception');
267+
} catch (UndefinedTransitionException $e) {
268+
$this->assertSame('Transition "404 Not Found" is not defined for workflow "unnamed".', $e->getMessage());
269+
$this->assertSame($e->getContext(), $context);
270+
}
262271
}
263272

264273
public function testApplyWithNotEnabledTransition()
265274
{
266275
$definition = $this->createComplexWorkflowDefinition();
267276
$subject = new Subject();
268277
$workflow = new Workflow($definition, new MethodMarkingStore());
278+
$context = [
279+
'lorem' => 'ipsum',
280+
];
269281

270282
try {
271-
$workflow->apply($subject, 't2');
283+
$workflow->apply($subject, 't2', $context);
272284

273285
$this->fail('Should throw an exception');
274286
} catch (NotEnabledTransitionException $e) {
@@ -279,6 +291,7 @@ public function testApplyWithNotEnabledTransition()
279291
$this->assertSame($e->getWorkflow(), $workflow);
280292
$this->assertSame($e->getSubject(), $subject);
281293
$this->assertSame($e->getTransitionName(), 't2');
294+
$this->assertSame($e->getContext(), $context);
282295
}
283296
}
284297

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ public function apply(object $subject, string $transitionName, array $context =
189189
}
190190

191191
if (!$transitionBlockerList) {
192-
throw new UndefinedTransitionException($subject, $transitionName, $this);
192+
throw new UndefinedTransitionException($subject, $transitionName, $this, $context);
193193
}
194194

195195
if (!$applied) {
196-
throw new NotEnabledTransitionException($subject, $transitionName, $this, $transitionBlockerList);
196+
throw new NotEnabledTransitionException($subject, $transitionName, $this, $transitionBlockerList, $context);
197197
}
198198

199199
return $marking;

0 commit comments

Comments
 (0)
0