2016-03-25 15:43:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Component\Workflow\Tests;
|
|
|
|
|
2017-02-20 13:34:33 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-07-31 20:19:25 +01:00
|
|
|
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
|
2016-03-25 15:43:30 +00:00
|
|
|
use Symfony\Component\Workflow\Definition;
|
|
|
|
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
|
|
|
|
use Symfony\Component\Workflow\Registry;
|
2016-12-04 22:08:15 +00:00
|
|
|
use Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface;
|
2017-10-30 06:24:47 +00:00
|
|
|
use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface;
|
2016-03-25 15:43:30 +00:00
|
|
|
use Symfony\Component\Workflow\Workflow;
|
2019-03-25 16:04:58 +00:00
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
2016-03-25 15:43:30 +00:00
|
|
|
|
2017-02-20 13:34:33 +00:00
|
|
|
class RegistryTest extends TestCase
|
2016-03-25 15:43:30 +00:00
|
|
|
{
|
2019-07-31 20:19:25 +01:00
|
|
|
use ForwardCompatTestTrait;
|
|
|
|
|
2016-03-25 15:43:30 +00:00
|
|
|
private $registry;
|
|
|
|
|
2019-07-31 20:19:25 +01:00
|
|
|
private function doSetUp()
|
2016-03-25 15:43:30 +00:00
|
|
|
{
|
|
|
|
$this->registry = new Registry();
|
|
|
|
|
2019-01-16 18:24:45 +00:00
|
|
|
$this->registry->addWorkflow(new Workflow(new Definition([], []), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), $this->createWorkflowSupportStrategy(Subject1::class));
|
|
|
|
$this->registry->addWorkflow(new Workflow(new Definition([], []), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow2'), $this->createWorkflowSupportStrategy(Subject2::class));
|
|
|
|
$this->registry->addWorkflow(new Workflow(new Definition([], []), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow3'), $this->createWorkflowSupportStrategy(Subject2::class));
|
2016-03-25 15:43:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 20:19:25 +01:00
|
|
|
private function doTearDown()
|
2016-03-25 15:43:30 +00:00
|
|
|
{
|
|
|
|
$this->registry = null;
|
|
|
|
}
|
|
|
|
|
2017-10-30 06:24:47 +00:00
|
|
|
/**
|
|
|
|
* @group legacy
|
2018-07-06 12:18:00 +01:00
|
|
|
* @expectedDeprecation The "Symfony\Component\Workflow\Registry::add()" method is deprecated since Symfony 4.1. Use addWorkflow() instead.
|
2017-10-30 06:24:47 +00:00
|
|
|
*/
|
|
|
|
public function testAddIsDeprecated()
|
|
|
|
{
|
2018-02-07 17:11:17 +00:00
|
|
|
$registry = new Registry();
|
|
|
|
|
2019-01-16 18:24:45 +00:00
|
|
|
$registry->add($w = new Workflow(new Definition([], []), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), $this->createSupportStrategy(Subject1::class));
|
2018-02-07 17:11:17 +00:00
|
|
|
|
|
|
|
$workflow = $registry->get(new Subject1());
|
|
|
|
$this->assertInstanceOf(Workflow::class, $workflow);
|
|
|
|
$this->assertSame('workflow1', $workflow->getName());
|
2017-10-30 06:24:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 15:43:30 +00:00
|
|
|
public function testGetWithSuccess()
|
|
|
|
{
|
|
|
|
$workflow = $this->registry->get(new Subject1());
|
|
|
|
$this->assertInstanceOf(Workflow::class, $workflow);
|
|
|
|
$this->assertSame('workflow1', $workflow->getName());
|
|
|
|
|
|
|
|
$workflow = $this->registry->get(new Subject1(), 'workflow1');
|
|
|
|
$this->assertInstanceOf(Workflow::class, $workflow);
|
|
|
|
$this->assertSame('workflow1', $workflow->getName());
|
|
|
|
|
|
|
|
$workflow = $this->registry->get(new Subject2(), 'workflow2');
|
|
|
|
$this->assertInstanceOf(Workflow::class, $workflow);
|
|
|
|
$this->assertSame('workflow2', $workflow->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-26 08:31:53 +01:00
|
|
|
* @expectedException \Symfony\Component\Workflow\Exception\InvalidArgumentException
|
2016-03-25 15:43:30 +00:00
|
|
|
* @expectedExceptionMessage At least two workflows match this subject. Set a different name on each and use the second (name) argument of this method.
|
|
|
|
*/
|
|
|
|
public function testGetWithMultipleMatch()
|
|
|
|
{
|
|
|
|
$w1 = $this->registry->get(new Subject2());
|
|
|
|
$this->assertInstanceOf(Workflow::class, $w1);
|
|
|
|
$this->assertSame('workflow1', $w1->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-16 12:13:16 +01:00
|
|
|
* @expectedException \Symfony\Component\Workflow\Exception\InvalidArgumentException
|
2016-03-25 15:43:30 +00:00
|
|
|
* @expectedExceptionMessage Unable to find a workflow for class "stdClass".
|
|
|
|
*/
|
|
|
|
public function testGetWithNoMatch()
|
|
|
|
{
|
|
|
|
$w1 = $this->registry->get(new \stdClass());
|
|
|
|
$this->assertInstanceOf(Workflow::class, $w1);
|
|
|
|
$this->assertSame('workflow1', $w1->getName());
|
|
|
|
}
|
2016-12-04 22:08:15 +00:00
|
|
|
|
2018-03-23 14:04:31 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-30 06:24:47 +00:00
|
|
|
/**
|
|
|
|
* @group legacy
|
|
|
|
*/
|
2016-12-04 22:08:15 +00:00
|
|
|
private function createSupportStrategy($supportedClassName)
|
|
|
|
{
|
|
|
|
$strategy = $this->getMockBuilder(SupportStrategyInterface::class)->getMock();
|
|
|
|
$strategy->expects($this->any())->method('supports')
|
2019-05-30 15:56:20 +01:00
|
|
|
->willReturnCallback(function ($workflow, $subject) use ($supportedClassName) {
|
2016-12-04 22:08:15 +00:00
|
|
|
return $subject instanceof $supportedClassName;
|
2019-05-30 15:56:20 +01:00
|
|
|
});
|
2016-12-04 22:08:15 +00:00
|
|
|
|
|
|
|
return $strategy;
|
|
|
|
}
|
2017-10-30 06:24:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group legacy
|
|
|
|
*/
|
|
|
|
private function createWorkflowSupportStrategy($supportedClassName)
|
|
|
|
{
|
|
|
|
$strategy = $this->getMockBuilder(WorkflowSupportStrategyInterface::class)->getMock();
|
|
|
|
$strategy->expects($this->any())->method('supports')
|
2019-05-30 17:06:08 +01:00
|
|
|
->willReturnCallback(function ($workflow, $subject) use ($supportedClassName) {
|
2017-10-30 06:24:47 +00:00
|
|
|
return $subject instanceof $supportedClassName;
|
2019-05-30 17:06:08 +01:00
|
|
|
});
|
2017-10-30 06:24:47 +00:00
|
|
|
|
|
|
|
return $strategy;
|
|
|
|
}
|
2016-03-25 15:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Subject1
|
|
|
|
{
|
|
|
|
}
|
|
|
|
class Subject2
|
|
|
|
{
|
|
|
|
}
|