Skip to content

Commit 3c519e5

Browse files
[FrameworkBundle] Fix normalization of enums in workflow transitions
1 parent 3c6d5e6 commit 3c519e5

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

DependencyInjection/Configuration.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -588,22 +588,18 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
588588
->then($workflowNormalizeArcs = static function ($arcs) {
589589
// Fix XML parsing, when only one arc is defined
590590
if (\array_key_exists('value', $arcs) && \array_key_exists('weight', $arcs)) {
591-
return [[
591+
$arcs = [[
592592
'place' => $arcs['value'],
593593
'weight' => $arcs['weight'],
594594
]];
595+
} elseif (\array_key_exists('place', $arcs)) {
596+
$arcs = [$arcs];
595597
}
596598

597599
$normalizedArcs = [];
598600
foreach ($arcs as $arc) {
599-
if ($arc instanceof \BackedEnum) {
600-
$arc = $arc->value;
601-
}
602-
if (\is_string($arc)) {
603-
$arc = [
604-
'place' => $arc,
605-
'weight' => 1,
606-
];
601+
if (\is_string($arc) || $arc instanceof \BackedEnum) {
602+
$arc = ['place' => $arc];
607603
} elseif (!\is_array($arc)) {
608604
throw new InvalidConfigurationException('The "from" arcs must be a list of strings or arrays in workflow configuration.');
609605
} elseif (\array_key_exists('value', $arc) && \array_key_exists('weight', $arc)) {
@@ -614,6 +610,10 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
614610
];
615611
}
616612

613+
if (($arc['place'] ?? null) instanceof \BackedEnum) {
614+
$arc['place'] = $arc['place']->value;
615+
}
616+
617617
$normalizedArcs[] = $arc;
618618
}
619619

@@ -628,7 +628,8 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
628628
->cannotBeEmpty()
629629
->end()
630630
->integerNode('weight')
631-
->isRequired()
631+
->defaultValue(1)
632+
->min(1)
632633
->end()
633634
->end()
634635
->end()
@@ -648,7 +649,8 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
648649
->cannotBeEmpty()
649650
->end()
650651
->integerNode('weight')
651-
->isRequired()
652+
->defaultValue(1)
653+
->min(1)
652654
->end()
653655
->end()
654656
->end()

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPUnit\Framework\Attributes\TestWith;
1717
use PHPUnit\Framework\TestCase;
1818
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Configuration;
19+
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\Workflow\Places;
1920
use Symfony\Bundle\FullStack;
2021
use Symfony\Component\AssetMapper\Compressor\CompressorInterface;
2122
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
@@ -696,6 +697,48 @@ public function testSerializerJsonDetailedErrorMessagesNotSetByDefaultWithDebugD
696697
$this->assertSame([], $config['serializer']['default_context'] ?? []);
697698
}
698699

700+
public function testWorkflowEnumArcsNormalization()
701+
{
702+
$processor = new Processor();
703+
$configuration = new Configuration(true);
704+
705+
$config = $processor->processConfiguration($configuration, [[
706+
'http_method_override' => false,
707+
'handle_all_throwables' => true,
708+
'php_errors' => ['log' => true],
709+
'workflows' => [
710+
'workflows' => [
711+
'enum' => [
712+
'supports' => [self::class],
713+
'places' => Places::cases(),
714+
'transitions' => [
715+
[
716+
'name' => 'one',
717+
'from' => [Places::A],
718+
'to' => [['place' => Places::B, 'weight' => 2]],
719+
],
720+
[
721+
'name' => 'two',
722+
'from' => ['place' => Places::B, 'weight' => 3],
723+
'to' => ['place' => Places::C],
724+
],
725+
],
726+
],
727+
],
728+
],
729+
]]);
730+
731+
$transitions = $config['workflows']['workflows']['enum']['transitions'];
732+
733+
$this->assertSame('one', $transitions[0]['name']);
734+
$this->assertSame([['place' => 'a', 'weight' => 1]], $transitions[0]['from']);
735+
$this->assertSame([['place' => 'b', 'weight' => 2]], $transitions[0]['to']);
736+
737+
$this->assertSame('two', $transitions[1]['name']);
738+
$this->assertSame([['place' => 'b', 'weight' => 3]], $transitions[1]['from']);
739+
$this->assertSame([['place' => 'c', 'weight' => 1]], $transitions[1]['to']);
740+
}
741+
699742
public function testFormCsrfProtectionFieldAttrDoNotNormalizeKeys()
700743
{
701744
$processor = new Processor();

0 commit comments

Comments
 (0)