From baec431fd9e88d7b0736282677833ba7cd1bad87 Mon Sep 17 00:00:00 2001 From: alexpozzi Date: Fri, 23 Mar 2018 15:04:31 +0100 Subject: [PATCH 1/2] [Workflow][Registry] Added the 'all' method which returns all the workflows associated to a specific object #26618 --- src/Symfony/Component/Workflow/CHANGELOG.md | 1 + src/Symfony/Component/Workflow/Registry.php | 17 ++++++++++++ .../Component/Workflow/Tests/RegistryTest.php | 27 +++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/Symfony/Component/Workflow/CHANGELOG.md b/src/Symfony/Component/Workflow/CHANGELOG.md index 4908db1b412b..63af1428987f 100644 --- a/src/Symfony/Component/Workflow/CHANGELOG.md +++ b/src/Symfony/Component/Workflow/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/Workflow/Registry.php b/src/Symfony/Component/Workflow/Registry.php index 3cb2ea88bf62..f3fd384e0d5c 100644 --- a/src/Symfony/Component/Workflow/Registry.php +++ b/src/Symfony/Component/Workflow/Registry.php @@ -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()) { diff --git a/src/Symfony/Component/Workflow/Tests/RegistryTest.php b/src/Symfony/Component/Workflow/Tests/RegistryTest.php index c122b5e2ed71..e61d9b6b6372 100644 --- a/src/Symfony/Component/Workflow/Tests/RegistryTest.php +++ b/src/Symfony/Component/Workflow/Tests/RegistryTest.php @@ -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 */ From ca1352d7abdaf4e75ebe76b1df3280f22a16294a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Mon, 2 Apr 2018 13:38:08 +0200 Subject: [PATCH 2/2] Fixed CHANGELOG --- src/Symfony/Component/Workflow/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Workflow/CHANGELOG.md b/src/Symfony/Component/Workflow/CHANGELOG.md index 63af1428987f..8ee3fd5903d3 100644 --- a/src/Symfony/Component/Workflow/CHANGELOG.md +++ b/src/Symfony/Component/Workflow/CHANGELOG.md @@ -11,7 +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 + * Added `Registry::all` to return all the workflows associated with the + specific subject. 4.0.0 -----