From 5a6eba8d51d1130773321ce8dc7b1eb0822f3005 Mon Sep 17 00:00:00 2001 From: alexpozzi Date: Fri, 23 Mar 2018 17:14:53 +0100 Subject: [PATCH] [Workflow][Registry] Added a new 'all' method --- src/Symfony/Component/Workflow/Registry.php | 17 ++++++++++++ .../Component/Workflow/Tests/RegistryTest.php | 27 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/Symfony/Component/Workflow/Registry.php b/src/Symfony/Component/Workflow/Registry.php index 4bc806d200ba0..17a2b7197fd34 100644 --- a/src/Symfony/Component/Workflow/Registry.php +++ b/src/Symfony/Component/Workflow/Registry.php @@ -64,6 +64,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(Workflow $workflow, $supportStrategy, $subject, $workflowName) { 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 262e7cfe59ddf..8713153b2bf54 100644 --- a/src/Symfony/Component/Workflow/Tests/RegistryTest.php +++ b/src/Symfony/Component/Workflow/Tests/RegistryTest.php @@ -65,6 +65,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 */