Skip to content

Commit

Permalink
[Workflow] Use a better exception message when many workflow are found
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Sep 12, 2019
1 parent 4b701bb commit 7f19913
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Workflow\Exception;

use Symfony\Component\Workflow\WorkflowInterface;

/**
* @author Fabien Potencier <fabien@symfony.com>
* @author Grégoire Pineau <lyrixx@lyrixx.info>
Expand Down
17 changes: 11 additions & 6 deletions src/Symfony/Component/Workflow/Registry.php
Expand Up @@ -47,22 +47,27 @@ public function addWorkflow(WorkflowInterface $workflow, WorkflowSupportStrategy
*/
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;
}
}

if (!$matched) {
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];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Workflow/Tests/RegistryTest.php
Expand Up @@ -62,7 +62,7 @@ public function testGetWithSuccess()
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());
Expand Down

0 comments on commit 7f19913

Please sign in to comment.