Skip to content

Commit 3e733e8

Browse files
committed
[Workflow] improve workflow config validation
1 parent 7e2f9fa commit 3e733e8

8 files changed

+168
-7
lines changed

DependencyInjection/Configuration.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
255255
->ifString()
256256
->then(function ($v) { return array($v); })
257257
->end()
258+
->requiresAtLeastOneElement()
258259
->prototype('scalar')
259260
->end()
260261
->end()
@@ -263,13 +264,12 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
263264
->end()
264265
->end()
265266
->validate()
266-
->always(function ($v) {
267-
if (isset($v['type']) && isset($v['service'])) {
268-
throw new \InvalidArgumentException('"type" and "service" could not be used together.');
269-
}
270-
271-
return $v;
272-
})
267+
->ifTrue(function ($v) { return isset($v['type']) && isset($v['service']); })
268+
->thenInvalid('"type" and "service" cannot be used together.')
269+
->end()
270+
->validate()
271+
->ifTrue(function ($v) { return isset($v['arguments']) && isset($v['service']); })
272+
->thenInvalid('"arguments" and "service" cannot be used together.')
273273
->end()
274274
->end()
275275
->arrayNode('supports')
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest;
4+
5+
$container->loadFromExtension('framework', array(
6+
'workflows' => array(
7+
'my_workflow' => array(
8+
'marking_store' => array(
9+
'arguments' => array('a', 'b'),
10+
'service' => 'workflow_service',
11+
),
12+
'supports' => array(
13+
FrameworkExtensionTest::class,
14+
),
15+
'places' => array(
16+
'first',
17+
'last',
18+
),
19+
'transitions' => array(
20+
'go' => array(
21+
'from' => array(
22+
'first',
23+
),
24+
'to' => array(
25+
'last',
26+
),
27+
),
28+
),
29+
),
30+
),
31+
));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest;
4+
5+
$container->loadFromExtension('framework', array(
6+
'workflows' => array(
7+
'my_workflow' => array(
8+
'marking_store' => array(
9+
'type' => 'multiple_state',
10+
'service' => 'workflow_service',
11+
),
12+
'supports' => array(
13+
FrameworkExtensionTest::class,
14+
),
15+
'places' => array(
16+
'first',
17+
'last',
18+
),
19+
'transitions' => array(
20+
'go' => array(
21+
'from' => array(
22+
'first',
23+
),
24+
'to' => array(
25+
'last',
26+
),
27+
),
28+
),
29+
),
30+
),
31+
));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config>
10+
<framework:workflow name="my_workflow">
11+
<framework:marking-store service="workflow_service">
12+
<framework:argument>a</framework:argument>
13+
<framework:argument>a</framework:argument>
14+
</framework:marking-store>
15+
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
16+
<framework:place>first</framework:place>
17+
<framework:place>last</framework:place>
18+
<framework:transition name="foobar">
19+
<framework:from>a</framework:from>
20+
<framework:to>a</framework:to>
21+
</framework:transition>
22+
</framework:workflow>
23+
</framework:config>
24+
</container>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config>
10+
<framework:workflow name="my_workflow">
11+
<framework:marking-store type="multiple_state" service="workflow_service" />
12+
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
13+
<framework:place>first</framework:place>
14+
<framework:place>last</framework:place>
15+
<framework:transition name="foobar">
16+
<framework:from>a</framework:from>
17+
<framework:to>a</framework:to>
18+
</framework:transition>
19+
</framework:workflow>
20+
</framework:config>
21+
</container>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
framework:
2+
workflows:
3+
my_workflow:
4+
marking_store:
5+
arguments:
6+
- a
7+
- b
8+
service: workflow_service
9+
supports:
10+
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest
11+
places:
12+
- first
13+
- last
14+
transitions:
15+
go:
16+
from:
17+
- first
18+
to:
19+
- last
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
framework:
2+
workflows:
3+
my_workflow:
4+
marking_store:
5+
type: multiple_state
6+
service: workflow_service
7+
supports:
8+
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest
9+
places:
10+
- first
11+
- last
12+
transitions:
13+
go:
14+
from:
15+
- first
16+
to:
17+
- last

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ public function testWorkflows()
167167
$this->assertSame('start', $stateMachineDefinition->getArgument(2));
168168
}
169169

170+
/**
171+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
172+
* @expectedExceptionMessage "type" and "service" cannot be used together.
173+
*/
174+
public function testWorkflowCannotHaveBothTypeAndService()
175+
{
176+
$this->createContainerFromFile('workflow_with_type_and_service');
177+
}
178+
179+
/**
180+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
181+
* @expectedExceptionMessage "arguments" and "service" cannot be used together.
182+
*/
183+
public function testWorkflowCannotHaveBothArgumentsAndService()
184+
{
185+
$this->createContainerFromFile('workflow_with_arguments_and_service');
186+
}
187+
170188
public function testRouter()
171189
{
172190
$container = $this->createContainerFromFile('full');

0 commit comments

Comments
 (0)