This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Workflow/Tests/RegistryTest.php

86 lines
3.4 KiB
PHP
Raw Normal View History

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;
2016-03-25 15:43:30 +00:00
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface;
2016-03-25 15:43:30 +00:00
use Symfony\Component\Workflow\Workflow;
2017-02-20 13:34:33 +00:00
class RegistryTest extends TestCase
2016-03-25 15:43:30 +00:00
{
private $registry;
protected function setUp()
{
$this->registry = new Registry();
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), $this->createSupportStrategy(Subject1::class));
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow2'), $this->createSupportStrategy(Subject2::class));
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow3'), $this->createSupportStrategy(Subject2::class));
2016-03-25 15:43:30 +00:00
}
protected function tearDown()
{
$this->registry = null;
}
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());
}
/**
* @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());
}
private function createSupportStrategy($supportedClassName)
{
$strategy = $this->getMockBuilder(SupportStrategyInterface::class)->getMock();
$strategy->expects($this->any())->method('supports')
->will($this->returnCallback(function ($workflow, $subject) use ($supportedClassName) {
return $subject instanceof $supportedClassName;
}));
return $strategy;
}
2016-03-25 15:43:30 +00:00
}
class Subject1
{
}
class Subject2
{
}