8000 Added context to exceptions thrown in apply method by koenreiniers · Pull Request #34457 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Added context to exceptions thrown in appl 8000 y method #34457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -46,4 +48,9 @@ public function getWorkflow(): WorkflowInterface
{
return $this->workflow;
}

public function getContext(): array
{
return $this->context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/
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);
}
}
21 changes: 17 additions & 4 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand Down Expand Up @@ -252,23 +253,34 @@ public function testBuildTransitionBlockerListReturnsReasonsProvidedInGuards()

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',
];

try {
$workflow->apply($subject, '404 Not Found', $context);

$workflow->apply($subject, '404 Not Found');
$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()
{
$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) {
Expand All @@ -279,6 +291,7 @@ public function testApplyWithNotEnabledTransition()
$this->assertSame($e->getWorkflow(), $workflow);
$this->assertSame($e->getSubject(), $subject);
$this->assertSame($e->getTransitionName(), 't2');
$this->assertSame($e->getContext(), $context);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Workflow/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ public function apply(object $subject, string $transitionName, array $context =
}

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;
Expand Down
0