Skip to content

Commit e511812

Browse files
committed
Merge branch '3.4' into 4.1
* 3.4: Update symfony links to https [FrameworkBundle] fixed guard event names for transitions [FrameworkBundle] fixed guard event names for transitions
2 parents 65befe6 + a3cc90d commit e511812

File tree

4 files changed

+93
-7
lines changed

4 files changed

+93
-7
lines changed

EventListener/GuardExpression.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow\EventListener;
13+
14+
use Symfony\Component\Workflow\Transition;
15+
16+
class GuardExpression
17+
{
18+
private $transition;
19+
20+
private $expression;
21+
22+
/**
23+
* @param string $expression
24+
*/
25+
public function __construct(Transition $transition, $expression)
26+
{
27+
$this->transition = $transition;
28+
$this->expression = $expression;
29+
}
30+
31+
public function getTransition()
32+
{
33+
return $this->transition;
34+
}
35+
36+
public function getExpression()
37+
{
38+
return $this->expression;
39+
}
40+
}

EventListener/GuardListener.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,21 @@ public function onTransition(GuardEvent $event, $eventName)
5050
return;
5151
}
5252

53-
$expression = $this->configuration[$eventName];
53+
$eventConfiguration = (array) $this->configuration[$eventName];
54+
foreach ($eventConfiguration as $guard) {
55+
if ($guard instanceof GuardExpression) {
56+
if ($guard->getTransition() !== $event->getTransition()) {
57+
continue;
58+
}
59+
$this->validateGuardExpression($event, $guard->getExpression());
60+
} else {
61+
$this->validateGuardExpression($event, $guard);
62+
}
63+
}
64+
}
5465

66+
private function validateGuardExpression(GuardEvent $event, string $expression)
67+
{
5568
if (!$this->expressionLanguage->evaluate($expression, $this->getVariables($event))) {
5669
$blocker = TransitionBlocker::createBlockedByExpressionGuardListener($expression);
5770
$event->addTransitionBlocker($blocker);

Tests/EventListener/GuardListenerTest.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Symfony\Component\Validator\Validator\ValidatorInterface;
1212
use Symfony\Component\Workflow\Event\GuardEvent;
1313
use Symfony\Component\Workflow\EventListener\ExpressionLanguage;
14+
use Symfony\Component\Workflow\EventListener\GuardExpression;
1415
use Symfony\Component\Workflow\EventListener\GuardListener;
1516
use Symfony\Component\Workflow\Marking;
1617
use Symfony\Component\Workflow\Transition;
@@ -21,12 +22,17 @@ class GuardListenerTest extends TestCase
2122
private $authenticationChecker;
2223
private $validator;
2324
private $listener;
25+
private $configuration;
2426

2527
protected function setUp()
2628
{
27-
$configuration = array(
29+
$this->configuration = array(
2830
'test_is_granted' => 'is_granted("something")',
2931
'test_is_valid' => 'is_valid(subject)',
32+
'test_expression' => array(
33+
new GuardExpression(new Transition('name', 'from', 'to'), '!is_valid(subject)'),
34+
new GuardExpression(new Transition('name', 'from', 'to'), 'is_valid(subject)'),
35+
),
3036
);
3137
$expressionLanguage = new ExpressionLanguage();
3238
$token = $this->getMockBuilder(TokenInterface::class)->getMock();
@@ -36,7 +42,7 @@ protected function setUp()
3642
$this->authenticationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
3743
$trustResolver = $this->getMockBuilder(AuthenticationTrustResolverInterface::class)->getMock();
3844
$this->validator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
39-
$this->listener = new GuardListener($configuration, $expressionLanguage, $tokenStorage, $this->authenticationChecker, $trustResolver, null, $this->validator);
45+
$this->listener = new GuardListener($this->configuration, $expressionLanguage, $tokenStorage, $this->authenticationChecker, $trustResolver, null, $this->validator);
4046
}
4147

4248
protected function tearDown()
@@ -97,11 +103,38 @@ public function testWithValidatorSupportedEventAndAccept()
97103
$this->assertFalse($event->isBlocked());
98104
}
99105

100-
private function createEvent()
106+
public function testWithGuardExpressionWithNotSupportedTransition()
107+
{
108+
$event = $this->createEvent();
109+
$this->configureValidator(false);
110+
$this->listener->onTransition($event, 'test_expression');
111+
112+
$this->assertFalse($event->isBlocked());
113+
}
114+
115+
public function testWithGuardExpressionWithSupportedTransition()
116+
{
117+
$event = $this->createEvent($this->configuration['test_expression'][1]->getTransition());
118+
$this->configureValidator(true, true);
119+
$this->listener->onTransition($event, 'test_expression');
120+
121+
$this->assertFalse($event->isBlocked());
122+
}
123+
124+
public function testGuardExpressionBlocks()
125+
{
126+
$event = $this->createEvent($this->configuration['test_expression'][1]->getTransition());
127+
$this->configureValidator(true, false);
128+
$this->listener->onTransition($event, 'test_expression');
129+
130+
$this->assertTrue($event->isBlocked());
131+
}
132+
133+
private function createEvent(Transition $transition = null)
101134
{
102135
$subject = new \stdClass();
103136
$subject->marking = new Marking();
104-
$transition = new Transition('name', 'from', 'to');
137+
$transition = $transition ?: new Transition('name', 'from', 'to');
105138

106139
$workflow = $this->getMockBuilder(WorkflowInterface::class)->getMock();
107140

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "library",
44
"description": "Symfony Workflow Component",
55
"keywords": ["workflow", "petrinet", "place", "transition", "statemachine", "state"],
6-
"homepage": "http://symfony.com",
6+
"homepage": "https://symfony.com",
77
"license": "MIT",
88
"authors": [
99
{
@@ -16,7 +16,7 @@
1616
},
1717
{
1818
"name": "Symfony Community",
19-
"homepage": "http://symfony.com/contributors"
19+
"homepage": "https://symfony.com/contributors"
2020
}
2121
],
2222
"require": {

0 commit comments

Comments
 (0)