Skip to content

Commit 945f618

Browse files
committed
[Workflow] improve workflow config validation
1 parent e493a1b commit 945f618

11 files changed

+261
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
250250
->ifString()
251251
->then(function ($v) { return array($v); })
252252
->end()
253+
->requiresAtLeastOneElement()
253254
->prototype('scalar')
254255
->end()
255256
->end()
@@ -258,13 +259,16 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
258259
->end()
259260
->end()
260261
->validate()
261-
->always(function ($v) {
262-
if (isset($v['type']) && isset($v['service'])) {
263-
throw new \InvalidArgumentException('"type" and "service" could not be used together.');
264-
}
265-
266-
return $v;
267-
})
262+
->ifTrue(function ($v) { return !isset($v['type']) && !isset($v['service']); })
263+
->thenInvalid('One of "type" or "service" must be configured.')
264+
->end()
265+
->validate()
266+
->ifTrue(function ($v) { return isset($v['type']) && isset($v['service']); })
267+
->thenInvalid('"type" and "service" cannot be used together.')
268+
->end()
269+
->validate()
270+
->ifTrue(function ($v) { return isset($v['arguments']) && isset($v['service']); })
271+
->thenInvalid('"arguments" and "service" cannot be used together.')
268272
->end()
269273
->end()
270274
->arrayNode('supports')
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+
));
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+
));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
'supports' => array(
10+
FrameworkExtensionTest::class,
11+
),
12+
'places' => array(
13+
'first',
14+
'last',
15+
),
16+
'transitions' => array(
17+
'go' => array(
18+
'from' => array(
19+
'first',
20+
),
21+
'to' => array(
22+
'last',
23+
),
24+
),
25+
),
26+
),
27+
),
28+
));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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:workflows>
11+
<framework:workflow name="my_workflow">
12+
<framework:marking-store>
13+
<framework:arguments>a</framework:arguments>
14+
<framework:arguments>a</framework:arguments>
15+
<framework:service>workflow_service</framework:service>
16+
</framework:marking-store>
17+
<framework:supports>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:supports>
18+
<framework:places>first</framework:places>
19+
<framework:places>last</framework:places>
20+
<framework:transitions>
21+
<framework:transition name="foobar">
22+
<framework:from>a</framework:from>
23+
<framework:to>a</framework:to>
24+
</framework:transition>
25+
</framework:transitions>
26+
</framework:workflow>
27+
</framework:workflows>
28+
</framework:config>
29+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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:workflows>
11+
<framework:workflow name="my_workflow">
12+
<framework:marking-store>
13+
<framework:type>multiple_state</framework:type>
14+
<framework:service>workflow_service</framework:service>
15+
</framework:marking-store>
16+
<framework:supports>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:supports>
17+
<framework:places>first</framework:places>
18+
<framework:places>last</framework:places>
19+
<framework:transitions>
20+
<framework:transition name="foobar">
21+
<framework:from>a</framework:from>
22+
<framework:to>a</framework:to>
23+
</framework:transition>
24+
</framework:transitions>
25+
</framework:workflow>
26+
</framework:workflows>
27+
</framework:config>
28+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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:workflows>
11+
<framework:workflow name="my_workflow">
12+
<framework:marking-store />
13+
<framework:supports>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:supports>
14+
<framework:places>first</framework:places>
15+
<framework:places>last</framework:places>
16+
<framework:transitions>
17+
<framework:transition name="foobar">
18+
<framework:from>a</framework:from>
19+
<framework:to>a</framework:to>
20+
</framework:transition>
21+
</framework:transitions>
22+
</framework:workflow>
23+
</framework:workflows>
24+
</framework:config>
25+
</container>
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
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
framework:
2+
workflows:
3+
my_workflow:
4+
marking_store: {}
5+
supports:
6+
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest
7+
places:
8+
- first
9+
- last
10+
transitions:
11+
go:
12+
from:
13+
- first
14+
to:
15+
- last

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,33 @@ public function testWorkflow()
127127
$this->assertTrue($container->hasDefinition('workflow.my_workflow'));
128128
}
129129

130+
/**
131+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
132+
* @expectedExceptionMessage One of "type" or "service" must be configured.
133+
*/
134+
public function testWorkflowNeedsTypeOrService()
135+
{
136+
$this->createContainerFromFile('workflow_without_type_and_service');
137+
}
138+
139+
/**
140+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
141+
* @expectedExceptionMessage "type" and "service" cannot be used together.
142+
*/
143+
public function testWorkflowCannotHaveBothTypeAndService()
144+
{
145+
$this->createContainerFromFile('workflow_with_type_and_service');
146+
}
147+
148+
/**
149+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
150+
* @expectedExceptionMessage "arguments" and "service" cannot be used together.
151+
*/
152+
public function testWorkflowCannotHaveBothArgumentsAndService()
153+
{
154+
$this->createContainerFromFile('workflow_with_arguments_and_service');
155+
}
156+
130157
public function testRouter()
131158
{
132159
$container = $this->createContainerFromFile('full');

0 commit comments

Comments
 (0)