[Workflow][Registry] Added the 'all' method which returns all the workflows associated to a specific object #26618

This commit is contained in:
alexpozzi 2018-03-23 15:04:31 +01:00 committed by Grégoire Pineau
parent 6e95c2ad27
commit baec431fd9
3 changed files with 45 additions and 0 deletions

View File

@ -11,6 +11,7 @@ CHANGELOG
* Added TransitionBlockers as a way to pass around reasons why exactly
transitions can't be made.
* Added a `MetadataStore`.
* Added Registry::all to return all the workflows associated with the specific subject
4.0.0
-----

View File

@ -66,6 +66,23 @@ class Registry
return $matched;
}
/**
* @param object $subject
*
* @return Workflow[]
*/
public function all($subject): array
{
$matched = array();
foreach ($this->workflows as list($workflow, $supportStrategy)) {
if ($supportStrategy->supports($workflow, $subject)) {
$matched[] = $workflow;
}
}
return $matched;
}
private function supports(WorkflowInterface $workflow, $supportStrategy, $subject, $workflowName): bool
{
if (null !== $workflowName && $workflowName !== $workflow->getName()) {

View File

@ -81,6 +81,33 @@ class RegistryTest extends TestCase
$this->assertSame('workflow1', $w1->getName());
}
public function testAllWithOneMatchWithSuccess()
{
$workflows = $this->registry->all(new Subject1());
$this->assertInternalType('array', $workflows);
$this->assertCount(1, $workflows);
$this->assertInstanceOf(Workflow::class, $workflows[0]);
$this->assertSame('workflow1', $workflows[0]->getName());
}
public function testAllWithMultipleMatchWithSuccess()
{
$workflows = $this->registry->all(new Subject2());
$this->assertInternalType('array', $workflows);
$this->assertCount(2, $workflows);
$this->assertInstanceOf(Workflow::class, $workflows[0]);
$this->assertInstanceOf(Workflow::class, $workflows[1]);
$this->assertSame('workflow2', $workflows[0]->getName());
$this->assertSame('workflow3', $workflows[1]->getName());
}
public function testAllWithNoMatch()
{
$workflows = $this->registry->all(new \stdClass());
$this->assertInternalType('array', $workflows);
$this->assertCount(0, $workflows);
}
/**
* @group legacy
*/