Added context to exceptions thrown in apply method
This commit is contained in:
parent
d2649f2f78
commit
8f86c337f7
@ -1,6 +1,11 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
5.1.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()`
|
||||||
|
|
||||||
5.0.0
|
5.0.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
@ -23,9 +23,9 @@ class NotEnabledTransitionException extends TransitionException
|
|||||||
{
|
{
|
||||||
private $transitionBlockerList;
|
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;
|
$this->transitionBlockerList = $transitionBlockerList;
|
||||||
}
|
}
|
||||||
|
@ -22,14 +22,16 @@ class TransitionException extends LogicException
|
|||||||
private $subject;
|
private $subject;
|
||||||
private $transitionName;
|
private $transitionName;
|
||||||
private $workflow;
|
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);
|
parent::__construct($message);
|
||||||
|
|
||||||
$this->subject = $subject;
|
$this->subject = $subject;
|
||||||
$this->transitionName = $transitionName;
|
$this->transitionName = $transitionName;
|
||||||
$this->workflow = $workflow;
|
$this->workflow = $workflow;
|
||||||
|
$this->context = $context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubject()
|
public function getSubject()
|
||||||
@ -46,4 +48,9 @@ class TransitionException extends LogicException
|
|||||||
{
|
{
|
||||||
return $this->workflow;
|
return $this->workflow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getContext(): array
|
||||||
|
{
|
||||||
|
return $this->context;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,8 @@ use Symfony\Component\Workflow\WorkflowInterface;
|
|||||||
*/
|
*/
|
||||||
class UndefinedTransitionException extends TransitionException
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ use Symfony\Component\Workflow\Event\Event;
|
|||||||
use Symfony\Component\Workflow\Event\GuardEvent;
|
use Symfony\Component\Workflow\Event\GuardEvent;
|
||||||
use Symfony\Component\Workflow\Event\TransitionEvent;
|
use Symfony\Component\Workflow\Event\TransitionEvent;
|
||||||
use Symfony\Component\Workflow\Exception\NotEnabledTransitionException;
|
use Symfony\Component\Workflow\Exception\NotEnabledTransitionException;
|
||||||
|
use Symfony\Component\Workflow\Exception\UndefinedTransitionException;
|
||||||
use Symfony\Component\Workflow\Marking;
|
use Symfony\Component\Workflow\Marking;
|
||||||
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
|
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
|
||||||
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
|
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
|
||||||
@ -252,13 +253,21 @@ class WorkflowTest extends TestCase
|
|||||||
|
|
||||||
public function testApplyWithNotExisingTransition()
|
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();
|
$definition = $this->createComplexWorkflowDefinition();
|
||||||
$subject = new Subject();
|
$subject = new Subject();
|
||||||
$workflow = new Workflow($definition, new MethodMarkingStore());
|
$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()
|
public function testApplyWithNotEnabledTransition()
|
||||||
@ -266,9 +275,12 @@ class WorkflowTest extends TestCase
|
|||||||
$definition = $this->createComplexWorkflowDefinition();
|
$definition = $this->createComplexWorkflowDefinition();
|
||||||
$subject = new Subject();
|
$subject = new Subject();
|
||||||
$workflow = new Workflow($definition, new MethodMarkingStore());
|
$workflow = new Workflow($definition, new MethodMarkingStore());
|
||||||
|
$context = [
|
||||||
|
'lorem' => 'ipsum',
|
||||||
|
];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$workflow->apply($subject, 't2');
|
$workflow->apply($subject, 't2', $context);
|
||||||
|
|
||||||
$this->fail('Should throw an exception');
|
$this->fail('Should throw an exception');
|
||||||
} catch (NotEnabledTransitionException $e) {
|
} catch (NotEnabledTransitionException $e) {
|
||||||
@ -279,6 +291,7 @@ class WorkflowTest extends TestCase
|
|||||||
$this->assertSame($e->getWorkflow(), $workflow);
|
$this->assertSame($e->getWorkflow(), $workflow);
|
||||||
$this->assertSame($e->getSubject(), $subject);
|
$this->assertSame($e->getSubject(), $subject);
|
||||||
$this->assertSame($e->getTransitionName(), 't2');
|
$this->assertSame($e->getTransitionName(), 't2');
|
||||||
|
$this->assertSame($e->getContext(), $context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,11 +189,11 @@ class Workflow implements WorkflowInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$transitionBlockerList) {
|
if (!$transitionBlockerList) {
|
||||||
throw new UndefinedTransitionException($subject, $transitionName, $this);
|
throw new UndefinedTransitionException($subject, $transitionName, $this, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$applied) {
|
if (!$applied) {
|
||||||
throw new NotEnabledTransitionException($subject, $transitionName, $this, $transitionBlockerList);
|
throw new NotEnabledTransitionException($subject, $transitionName, $this, $transitionBlockerList, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $marking;
|
return $marking;
|
||||||
|
Reference in New Issue
Block a user