Skip to content

Commit ab02334

Browse files
committed
[Workflow] Use a better exception message when many workflow are found
1 parent 83c73d0 commit ab02334

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

Registry.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,27 @@ public function addWorkflow(WorkflowInterface $workflow, WorkflowSupportStrategy
4747
*/
4848
public function get($subject, $workflowName = null)
4949
{
50-
$matched = null;
50+
$matched = [];
5151

5252
foreach ($this->workflows as list($workflow, $supportStrategy)) {
5353
if ($this->supports($workflow, $supportStrategy, $subject, $workflowName)) {
54-
if ($matched) {
55-
throw new InvalidArgumentException('At least two workflows match this subject. Set a different name on each and use the second (name) argument of this method.');
56-
}
57-
$matched = $workflow;
54+
$matched[] = $workflow;
5855
}
5956
}
6057

6158
if (!$matched) {
6259
throw new InvalidArgumentException(sprintf('Unable to find a workflow for class "%s".', \get_class($subject)));
6360
}
6461

65-
return $matched;
62+
if (2 <= \count($matched)) {
63+
$names = array_map(static function (WorkflowInterface $workflow): string {
64+
return $workflow->getName();
65+
}, $matched);
66+
67+
throw new InvalidArgumentException(sprintf('Too many workflows (%s) match this subject (%s); set a different name on each and use the second (name) argument of this method.', implode(', ', $names), \get_class($subject)));
68+
}
69+
70+
return $matched[0];
6671
}
6772

6873
/**

Tests/RegistryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testGetWithSuccess()
6262
public function testGetWithMultipleMatch()
6363
{
6464
$this->expectException('Symfony\Component\Workflow\Exception\InvalidArgumentException');
65-
$this->expectExceptionMessage('At least two workflows match this subject. Set a different name on each and use the second (name) argument of this method.');
65+
$this->expectExceptionMessage('Too many workflows (workflow2, workflow3) match this subject (Symfony\Component\Workflow\Tests\Subject2); set a different name on each and use the second (name) argument of this method.');
6666
$w1 = $this->registry->get(new Subject2());
6767
$this->assertInstanceOf(Workflow::class, $w1);
6868
$this->assertSame('workflow1', $w1->getName());

0 commit comments

Comments
 (0)