addTransitionBlocker example

$this->validateGuardExpression($event$guard->getExpression());
            } else {
                $this->validateGuardExpression($event$guard);
            }
        }
    }

    private function validateGuardExpression(GuardEvent $event, string $expression): void
    {
        if (!$this->expressionLanguage->evaluate($expression$this->getVariables($event))) {
            $blocker = TransitionBlocker::createBlockedByExpressionGuardListener($expression);
            $event->addTransitionBlocker($blocker);
        }
    }

    // code should be sync with Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter     private function getVariables(GuardEvent $event): array
    {
        $token = $this->tokenStorage->getToken();

        $variables = [
            'subject' => $event->getSubject(),
            // needed for the is_granted expression function
$this->assertTrue($net->buildTransitionBlockerList($subject, 't3')->isEmpty());
    }

    public function testBuildTransitionBlockerListReturnsExpectedReasonOnBranchMerge()
    {
        $definition = $this->createComplexStateMachineDefinition();

        $dispatcher = new EventDispatcher();
        $net = new StateMachine($definition, null, $dispatcher);

        $dispatcher->addListener('workflow.guard', function DGuardEvent $event) {
            $event->addTransitionBlocker(new TransitionBlocker(sprintf('Transition blocker of place %s', $event->getTransition()->getFroms()[0]), 'blocker'));
        });

        $subject = new Subject();

        // There may be multiple transitions with the same name. Make sure that transitions         // that are enabled by the marking are evaluated.         // see https://github.com/symfony/symfony/issues/28432
        // Test if when you are in place "a" and trying to apply "t1" then it returns         // blocker list contains guard blocker instead blockedByMarking         $subject->setMarking('a');
        
$this->assertSame('19beefc8-6b1e-4716-9d07-a39bd6d16e34', $blockers[0]->getCode());
    }

    public function testBuildTransitionBlockerListReturnsReasonsProvidedInGuards()
    {
        $definition = $this->createSimpleWorkflowDefinition();
        $subject = new Subject();
        $dispatcher = new EventDispatcher();
        $workflow = new Workflow($definitionnew MethodMarkingStore()$dispatcher);

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

        
Home | Imprint | This part of the site doesn't use cookies.