Skip to content

Commit

Permalink
feature #26656 [Workflow][Registry] Added a new 'all' method (alexpoz…
Browse files Browse the repository at this point in the history
…zi, lyrixx)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[Workflow][Registry] Added a new 'all' method

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

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26618
| License       | MIT
| Doc PR        |

As explained in [26628](#26618 (comment))  I added the 'all' method which returns all the workflows associated to a specific object.

Commits
-------

ca1352d Fixed CHANGELOG
baec431 [Workflow][Registry] Added the 'all' method which returns all the workflows associated to a specific object #26618
  • Loading branch information
lyrixx committed Apr 2, 2018
2 parents 6e95c2a + ca1352d commit 0ff2f8e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Expand Up @@ -11,6 +11,8 @@ 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
-----
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Workflow/Registry.php
Expand Up @@ -66,6 +66,23 @@ public function get($subject, $workflowName = null)
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()) {
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Workflow/Tests/RegistryTest.php
Expand Up @@ -81,6 +81,33 @@ public function testGetWithNoMatch()
$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
*/
Expand Down

0 comments on commit 0ff2f8e

Please sign in to comment.