Commit 03303c5a authored by pcambra's avatar pcambra

Added basic test support for WorkflowGroup

parent 80111e28
......@@ -2,51 +2,68 @@
/**
* @file
* Contains \Drupal\Tests\state_machine\Unit\WorkflowGroupTest.
* Contains \Drupal\Tests\state_machine\Unit\Plugin\WorkflowGroup\WorkflowGroupTest.
*/
namespace Drupal\Tests\state_machine\Unit;
namespace Drupal\Tests\state_machine\Unit\Plugin\WorkflowGroup;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use org\bovigo\vfs\vfsStream;
use Drupal\state_machine\Plugin\WorkflowGroup\WorkflowGroup;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\state_machine\Plugin\WorkflowGroup\WorkflowGroup
* @group Workflow
*/
class WorkflowGroupTest extends WorkflowBaseTestCase {
class WorkflowGroupTest extends UnitTestCase {
/**
* Tests the group with a config YAML file
* The workflow group.
*
* @var \Drupal\state_machine\Plugin\WorkflowGroup\WorkflowGroup
*/
protected $workflowGroup;
/**
* The plugin definition array.
*
* @var array
*/
protected $definition = [
'id' => 'order',
'label' => 'Order',
'entity_type' => 'commerce_order',
'class' => 'Drupal\state_machine\Plugin\WorkflowGroup\WorkflowGroup',
'workflow_class' => '\Drupal\state_machine\Plugin\Workflow\Workflow',
'provider' => 'state_machine_test',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->workflowGroup = new WorkflowGroup([], 'order', $this->definition);
}
/**
* @covers ::getLabel
*/
public function testGetLabel() {
$this->assertEquals($this->definition['label'], $this->workflowGroup->getLabel(), 'Workflow group label matches the expected one');
}
/**
* @covers ::getEntityTypeId
*/
public function testGetEntityTypeId() {
$this->assertEquals($this->definition['entity_type'], $this->workflowGroup->getEntityTypeId(), 'Workflow group entity type id matches the expected one');
}
/**
* @covers ::getWorkflowClass
*/
public function testProcessValidDefinition() {
vfsStream::setup('root');
$group_config = [
'order' => [
'label' => 'Order',
'entity_type' => 'commerce_order',
]
];
$file = Yaml::encode($group_config);
vfsStream::create([
'state_machine_test' => [
'state_machine_test.workflow_groups.yml' => $file,
]]
);
$discovery = new YamlDiscovery('workflow_groups', ['state_machine_test' => vfsStream::url('root/state_machine_test')]);
$this->groupManager->setDiscovery($discovery);
/** @var $workflow_group \Drupal\state_machine\Plugin\WorkflowGroup\WorkflowGroup */
$workflow_group = $this->groupManager->createInstance('order');
$this->assertEquals('Order', $workflow_group->getLabel(), 'Workflow group label matches the expected one');
$this->assertEquals('commerce_order', $workflow_group->getEntityTypeId(), 'Workflow group entity type id matches the expected one');
$this->assertEquals('\Drupal\state_machine\Plugin\Workflow\Workflow', $workflow_group->getWorkflowClass(), 'Workflow group class matches the expected one');
public function testGetWorkflowClass() {
$this->assertEquals($this->definition['workflow_class'], $this->workflowGroup->getWorkflowClass(), 'Workflow group class matches the expected one');
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\state_machine\Unit\WorkflowBaseTest.
*/
namespace Drupal\Tests\state_machine\Unit;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\state_machine\WorkflowGroupManager;
use Drupal\Tests\UnitTestCase;
class WorkflowBaseTestCase extends UnitTestCase {
/**
* The cache backend to use.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $cache;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* The plugin discovery.
*
* @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $discovery;
/**
* The group manager under test.
*
* @var \Drupal\Tests\state_machine\Unit\TestWorkflowGroupManager
*/
protected $groupManager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Prepare the default constructor arguments required by
// WorkflowGroupManager.
$this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
$this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
$this->moduleHandler->moduleExists('state_machine_test')->willReturn(TRUE);
$this->groupManager = new TestWorkflowGroupManager($this->moduleHandler->reveal(), $this->cache);
}
}
/**
* Provides a testing version of WorkflowGroupManager with an empty constructor.
*/
class TestWorkflowGroupManager extends WorkflowGroupManager {
/**
* Sets the discovery for the manager.
*
* @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
* The discovery object.
*/
public function setDiscovery(DiscoveryInterface $discovery) {
$this->discovery = $discovery;
}
}
......@@ -7,15 +7,40 @@
namespace Drupal\Tests\state_machine\Unit;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\state_machine\WorkflowGroupManager;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
/**
* @coversDefaultClass \Drupal\state_machine\WorkflowGroupManager
* @group Workflow
*/
class WorkflowGroupManagerTest extends WorkflowBaseTestCase {
class WorkflowGroupManagerTest extends UnitTestCase {
/**
* The cache backend to use.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $cache;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* The plugin discovery.
*
* @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
*/
protected $discovery;
/**
* The group manager under test.
......@@ -41,10 +66,23 @@ class WorkflowGroupManagerTest extends WorkflowBaseTestCase {
];
/**
* Provide a set of invalid config workflow groups to test the process
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Prepare the default constructor arguments required by
// WorkflowGroupManager.
$this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
$this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
$this->moduleHandler->moduleExists('state_machine_test')->willReturn(TRUE);
$this->groupManager = new TestWorkflowGroupManager($this->moduleHandler->reveal(), $this->cache);
}
/**
* Provide a set of incomplete config workflow groups to test the process
* definitions.
*/
public function invalidConfigWorkflowGroups() {
public function configWorkflowGroups() {
return [
[['workflow_group_1' => [
'entity_type' => 'commerce_order',
......@@ -64,7 +102,7 @@ class WorkflowGroupManagerTest extends WorkflowBaseTestCase {
* @covers ::processDefinition
* @dataProvider invalidConfigWorkflowGroups
*/
public function testProcessInvalidDefinitions($group_config) {
public function testProcessIncompleteDefinitions($group_config) {
vfsStream::setup('root');
$file = Yaml::encode($group_config);
vfsStream::create([
......@@ -107,4 +145,48 @@ class WorkflowGroupManagerTest extends WorkflowBaseTestCase {
$this->assertEquals($this->expectedDefinitions, $this->groupManager->getDefinitionsByEntityType('commerce_order'), 'Workflow group definition matches the expectations');
}
/**
* Tests that the workflow group manager returns the right object.
*/
public function testProcessValidDefinition() {
vfsStream::setup('root');
$group_config = [
'order' => [
'label' => 'Order',
'entity_type' => 'commerce_order',
]
];
$file = Yaml::encode($group_config);
vfsStream::create([
'state_machine_test' => [
'state_machine_test.workflow_groups.yml' => $file,
]]
);
$discovery = new YamlDiscovery('workflow_groups', ['state_machine_test' => vfsStream::url('root/state_machine_test')]);
$this->groupManager->setDiscovery($discovery);
/** @var $workflow_group \Drupal\state_machine\Plugin\WorkflowGroup\WorkflowGroup */
$workflow_group = $this->groupManager->createInstance('order');
$this->assertEquals('Order', $workflow_group->getLabel(), 'Workflow group label matches the expected one');
$this->assertEquals('commerce_order', $workflow_group->getEntityTypeId(), 'Workflow group entity type id matches the expected one');
$this->assertEquals('\Drupal\state_machine\Plugin\Workflow\Workflow', $workflow_group->getWorkflowClass(), 'Workflow group class matches the expected one');
}
}
/**
* Provides a testing version of WorkflowGroupManager with an empty constructor.
*/
class TestWorkflowGroupManager extends WorkflowGroupManager {
/**
* Sets the discovery for the manager.
*
* @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
* The discovery object.
*/
public function setDiscovery(DiscoveryInterface $discovery) {
$this->discovery = $discovery;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment