Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
[Workflow] Cleaned the transition blocker implementations
  • Loading branch information
lyrixx committed Mar 20, 2018
commit 2b8faffb415c4a3c4bf7891c45e7740f722076ac
6 changes: 3 additions & 3 deletions src/Symfony/Component/Workflow/Event/GuardEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ public function __construct($subject, Marking $marking, Transition $transition,

public function isBlocked(): bool
Comment thread
lyrixx marked this conversation as resolved.
{
return 0 !== count($this->transitionBlockerList);
return !$this->transitionBlockerList->isEmpty();
}

public function setBlocked(bool $blocked): void
Comment thread
lyrixx marked this conversation as resolved.
{
if (!$blocked) {
$this->transitionBlockerList = new TransitionBlockerList();
$this->transitionBlockerList->reset();

return;
}

$this->transitionBlockerList->add(TransitionBlocker::createUnknownReason($this->getTransition()->getName()));
$this->transitionBlockerList->add(TransitionBlocker::createUnknown());
Comment thread
lyrixx marked this conversation as resolved.
}

public function getTransitionBlockerList(): TransitionBlockerList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\Exception\InvalidTokenConfigurationException;
use Symfony\Component\Workflow\TransitionBlocker;

/**
* @author Grégoire Pineau <[email protected]>
Expand Down Expand Up @@ -49,8 +50,11 @@ public function onTransition(GuardEvent $event, $eventName)
return;
}

if (!$this->expressionLanguage->evaluate($this->configuration[$eventName], $this->getVariables($event))) {
$event->setBlocked(true);
$expression = $this->configuration[$eventName];

if (!$this->expressionLanguage->evaluate($expression, $this->getVariables($event))) {
$blocker = TransitionBlocker::createBlockedByExpressionGuardListener($expression);
$event->addTransitionBlocker($blocker);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
use Symfony\Component\Workflow\TransitionBlockerList;

/**
* Thrown by the workflow when a transition is not enabled.
* Thrown by Workflow when a not enabled transition is applied on a subject.
*
* @author Grégoire Pineau <[email protected]>
*/
class BlockedTransitionException extends LogicException
class NotEnabledTransitionException extends LogicException
{
private $transitionBlockerList;

public function __construct(string $message, TransitionBlockerList $transitionBlockerList)
public function __construct(string $transitionName, string $workflowName, TransitionBlockerList $transitionBlockerList)
{
parent::__construct($message);
parent::__construct(sprintf('Transition "%s" is not enabled for workflow "%s".', $transitionName, $workflowName));

$this->transitionBlockerList = $transitionBlockerList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@

/**
* Thrown by Workflow when an undefined transition is applied on a subject.
*
* @author Grégoire Pineau <[email protected]>
*/
class UndefinedTransitionException extends LogicException
{
public function __construct(string $transitionName, string $workflowName)
{
parent::__construct(sprintf('Transition "%s" is not defined for workflow "%s".', $transitionName, $workflowName));
}
}
217 changes: 93 additions & 124 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\Exception\BlockedTransitionException;
use Symfony\Component\Workflow\Exception\NotEnabledTransitionException;
use Symfony\Component\Workflow\Marking;
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore;
Expand Down Expand Up @@ -164,35 +164,114 @@ public function testCanDoesNotTriggerGuardEventsForNotEnabledTransitions()
$this->assertSame(array('workflow_name.guard.t3'), $dispatchedEvents);
}

public function testCanWithSameNameTransition()
{
$definition = $this->createWorkflowWithSameNameTransition();
$workflow = new Workflow($definition, new MultipleStateMarkingStore());

$subject = new \stdClass();
$subject->marking = null;
$this->assertTrue($workflow->can($subject, 'a_to_bc'));
$this->assertFalse($workflow->can($subject, 'b_to_c'));
$this->assertFalse($workflow->can($subject, 'to_a'));

$subject->marking = array('b' => 1);
$this->assertFalse($workflow->can($subject, 'a_to_bc'));
$this->assertTrue($workflow->can($subject, 'b_to_c'));
$this->assertTrue($workflow->can($subject, 'to_a'));
}

/**
* @expectedException \Symfony\Component\Workflow\Exception\LogicException
* @expectedExceptionMessage Unable to apply transition "t2" for workflow "unnamed".
* @expectedException \Symfony\Component\Workflow\Exception\UndefinedTransitionException
* @expectedExceptionMessage Transition "404 Not Found" is not defined for workflow "unnamed".
*/
public function testApplyWithImpossibleTransition()
public function testBuildTransitionBlockerListReturnsUndefinedTransition()
{
$definition = $this->createSimpleWorkflowDefinition();
$subject = new \stdClass();
$subject->marking = null;
$workflow = new Workflow($definition);

$workflow->buildTransitionBlockerList($subject, '404 Not Found');
}

public function testBuildTransitionBlockerListReturnsReasonsProvidedByMarking()
{
$definition = $this->createComplexWorkflowDefinition();
$subject = new \stdClass();
$subject->marking = null;
$workflow = new Workflow($definition, new MultipleStateMarkingStore());

$workflow->apply($subject, 't2');
$transitionBlockerList = $workflow->buildTransitionBlockerList($subject, 't2');
$this->assertCount(1, $transitionBlockerList);
$blockers = iterator_to_array($transitionBlockerList);
$this->assertSame('The marking does not enable the transition.', $blockers[0]->getMessage());
$this->assertSame('19beefc8-6b1e-4716-9d07-a39bd6d16e34', $blockers[0]->getCode());
}

public function testCanWithSameNameTransition()
public function testBuildTransitionBlockerListReturnsReasonsProvidedInGuards()
{
$definition = $this->createWorkflowWithSameNameTransition();
$definition = $this->createSimpleWorkflowDefinition();
$subject = new \stdClass();
$subject->marking = null;
$dispatcher = new EventDispatcher();
$workflow = new Workflow($definition, new MultipleStateMarkingStore(), $dispatcher);

$dispatcher->addListener('workflow.guard', function (GuardEvent $event) {
$event->addTransitionBlocker(new TransitionBlocker('Transition blocker 1', 'blocker_1'));
$event->addTransitionBlocker(new TransitionBlocker('Transition blocker 2', 'blocker_2'));
});
$dispatcher->addListener('workflow.guard', function (GuardEvent $event) {
$event->addTransitionBlocker(new TransitionBlocker('Transition blocker 3', 'blocker_3'));
});
$dispatcher->addListener('workflow.guard', function (GuardEvent $event) {
$event->setBlocked(true);
});

$transitionBlockerList = $workflow->buildTransitionBlockerList($subject, 't1');
$this->assertCount(4, $transitionBlockerList);
$blockers = iterator_to_array($transitionBlockerList);
$this->assertSame('Transition blocker 1', $blockers[0]->getMessage());
$this->assertSame('blocker_1', $blockers[0]->getCode());
$this->assertSame('Transition blocker 2', $blockers[1]->getMessage());
$this->assertSame('blocker_2', $blockers[1]->getCode());
$this->assertSame('Transition blocker 3', $blockers[2]->getMessage());
$this->assertSame('blocker_3', $blockers[2]->getCode());
$this->assertSame('Unknown reason.', $blockers[3]->getMessage());
$this->assertSame('e8b5bbb9-5913-4b98-bfa6-65dbd228a82a', $blockers[3]->getCode());
}

/**
* @expectedException \Symfony\Component\Workflow\Exception\UndefinedTransitionException
* @expectedExceptionMessage Transition "404 Not Found" is not defined for workflow "unnamed".
*/
public function testApplyWithNotExisingTransition()
{
$definition = $this->createComplexWorkflowDefinition();
$subject = new \stdClass();
$subject->marking = null;
$workflow = new Workflow($definition, new MultipleStateMarkingStore());

$workflow->apply($subject, '404 Not Found');
}

public function testApplyWithNotEnabledTransition()
{
$definition = $this->createComplexWorkflowDefinition();
$subject = new \stdClass();
$subject->marking = null;
$this->assertTrue($workflow->can($subject, 'a_to_bc'));
$this->assertFalse($workflow->can($subject, 'b_to_c'));
$this->assertFalse($workflow->can($subject, 'to_a'));
$workflow = new Workflow($definition, new MultipleStateMarkingStore());

$subject->marking = array('b' => 1);
$this->assertFalse($workflow->can($subject, 'a_to_bc'));
$this->assertTrue($workflow->can($subject, 'b_to_c'));
$this->assertTrue($workflow->can($subject, 'to_a'));
try {
$workflow->apply($subject, 't2');

$this->fail('Should throw an exception');
} catch (NotEnabledTransitionException $e) {
$this->assertSame('Transition "t2" is not enabled for workflow "unnamed".', $e->getMessage());
$this->assertCount(1, $e->getTransitionBlockerList());
$list = iterator_to_array($e->getTransitionBlockerList());
$this->assertSame('The marking does not enable the transition.', $list[0]->getMessage());
}
}

public function testApply()
Expand Down Expand Up @@ -413,116 +492,6 @@ public function testGetEnabledTransitionsWithSameNameTransition()
$this->assertSame('to_a', $transitions[1]->getName());
$this->assertSame('to_a', $transitions[2]->getName());
}

public function testWhyCannotReturnsReasonsProvidedInGuards()
{
$definition = $this->createSimpleWorkflowDefinition();
$subject = new \stdClass();
$subject->marking = null;
$dispatcher = new EventDispatcher();
$workflow = new Workflow($definition, new MultipleStateMarkingStore(), $dispatcher);

$guardsAddingTransitionBlockers = array(
function (GuardEvent $event) {
$event->addTransitionBlocker(new TransitionBlocker('Transition blocker 1', 'blocker_1'));
$event->addTransitionBlocker(new TransitionBlocker('Transition blocker 2', 'blocker_2'));
},
function (GuardEvent $event) {
$event->addTransitionBlocker(new TransitionBlocker('Transition blocker 3', 'blocker_3'));
},
);

foreach ($guardsAddingTransitionBlockers as $guard) {
$dispatcher->addListener('workflow.guard', $guard);
}

$transitionBlockerList = $workflow->buildTransitionBlockerList($subject, 't1');

$this->assertCount(3, $transitionBlockerList);

$assertTransitionBlockerPresentByCodeFn = function (string $code) use ($transitionBlockerList) {
$this->assertNotNull(
$transitionBlockerList->findByCode($code),
sprintf('Workflow did not produce transition blocker with code "%s"', $code)
);
};

$assertTransitionBlockerPresentByCodeFn('blocker_1');
$assertTransitionBlockerPresentByCodeFn('blocker_2');
$assertTransitionBlockerPresentByCodeFn('blocker_3');
}

public function testWhyCannotReturnsTransitionNotDefinedReason()
{
$definition = $this->createSimpleWorkflowDefinition();
$subject = new \stdClass();
$subject->marking = null;
$workflow = new Workflow($definition);

$transitionBlockerList = $workflow->buildTransitionBlockerList($subject, 'undefined_transition_name');

$this->assertCount(1, $transitionBlockerList);
$this->assertEquals(
TransitionBlocker::REASON_TRANSITION_NOT_DEFINED,
$transitionBlockerList->get(0)->getCode()
);
}

public function testWhyCannotReturnsTransitionNotApplicableReason()
{
$definition = $this->createSimpleWorkflowDefinition();
$subject = new \stdClass();
$subject->marking = null;
$workflow = new Workflow($definition);

$transitionBlockerList = $workflow->buildTransitionBlockerList($subject, 't2');

$this->assertCount(1, $transitionBlockerList);
$this->assertEquals(
TransitionBlocker::REASON_TRANSITION_NOT_APPLICABLE,
$transitionBlockerList->get(0)->getCode()
);
}

public function testApplyConveysTheTransitionBlockers()
{
$definition = $this->createSimpleWorkflowDefinition();
$subject = new \stdClass();
$subject->marking = null;
$dispatcher = new EventDispatcher();
$workflow = new Workflow($definition, new MultipleStateMarkingStore(), $dispatcher);

$dispatcher->addListener('workflow.guard', function (GuardEvent $event) {
$event->addTransitionBlocker(new TransitionBlocker('Transition blocker 3', 'blocker_1'));
});

try {
$workflow->apply($subject, 't1');
} catch (BlockedTransitionException $exception) {
$this->assertNotNull(
$exception->getTransitionBlockerList()->findByCode('blocker_1'),
'Workflow failed to convey it could not transition subject because of expected blocker'
);

return;
}

$this->fail('Workflow failed to prevent a transition from happening');
}

/**
* @expectedException \Symfony\Component\Workflow\Exception\UndefinedTransitionException
* @expectedExceptionMessage Transition "undefined_transition" is not defined in workflow "unnamed".
*/
public function testApplyWithUndefinedTransition()
{
$definition = $this->createSimpleWorkflowDefinition();
$subject = new \stdClass();
$subject->marking = null;
$workflow = new Workflow($definition);

$workflow->apply($subject, 'undefined_transition');
}
}

class EventDispatcherMock implements \Symfony\Component\EventDispatcher\EventDispatcherInterface
Expand Down
Loading