[Workflow] Use a better exception message when many workflow are found
This commit is contained in:
parent
4b701bb953
commit
1862cb3ec5
@ -47,14 +47,11 @@ class Registry
|
||||
*/
|
||||
public function get($subject, $workflowName = null)
|
||||
{
|
||||
$matched = null;
|
||||
$matched = [];
|
||||
|
||||
foreach ($this->workflows as list($workflow, $supportStrategy)) {
|
||||
if ($this->supports($workflow, $supportStrategy, $subject, $workflowName)) {
|
||||
if ($matched) {
|
||||
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.');
|
||||
}
|
||||
$matched = $workflow;
|
||||
$matched[] = $workflow;
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,7 +59,15 @@ class Registry
|
||||
throw new InvalidArgumentException(sprintf('Unable to find a workflow for class "%s".', \get_class($subject)));
|
||||
}
|
||||
|
||||
return $matched;
|
||||
if (2 <= \count($matched)) {
|
||||
$names = array_map(static function (WorkflowInterface $workflow): string {
|
||||
return $workflow->getName();
|
||||
}, $matched);
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
return $matched[0];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,7 +62,7 @@ class RegistryTest extends TestCase
|
||||
public function testGetWithMultipleMatch()
|
||||
{
|
||||
$this->expectException('Symfony\Component\Workflow\Exception\InvalidArgumentException');
|
||||
$this->expectExceptionMessage('At least two workflows match this subject. Set a different name on each and use the second (name) argument of this method.');
|
||||
$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.');
|
||||
$w1 = $this->registry->get(new Subject2());
|
||||
$this->assertInstanceOf(Workflow::class, $w1);
|
||||
$this->assertSame('workflow1', $w1->getName());
|
||||
|
Reference in New Issue
Block a user