[Workflow] Added Registry::has() to check if a workflow exists

This commit is contained in:
Grégoire Pineau 2019-11-25 14:41:16 +01:00
parent 7719fc709e
commit 8a4f03dee8
3 changed files with 22 additions and 0 deletions

View File

@ -5,6 +5,7 @@ CHANGELOG
----- -----
* Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()` * Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()`
* Added `Registry::has()` to check if a workflow exists
5.0.0 5.0.0
----- -----

View File

@ -27,6 +27,17 @@ class Registry
$this->workflows[] = [$workflow, $supportStrategy]; $this->workflows[] = [$workflow, $supportStrategy];
} }
public function has(object $subject, string $workflowName = null): bool
{
foreach ($this->workflows as list($workflow, $supportStrategy)) {
if ($this->supports($workflow, $supportStrategy, $subject, $workflowName)) {
return true;
}
}
return false;
}
/** /**
* @return Workflow * @return Workflow
*/ */

View File

@ -28,6 +28,16 @@ class RegistryTest extends TestCase
$this->registry = null; $this->registry = null;
} }
public function testHasWithMatch()
{
$this->assertTrue($this->registry->has(new Subject1()));
}
public function testHasWithoutMatch()
{
$this->assertFalse($this->registry->has(new Subject1(), 'nope'));
}
public function testGetWithSuccess() public function testGetWithSuccess()
{ {
$workflow = $this->registry->get(new Subject1()); $workflow = $this->registry->get(new Subject1());