public function WorkflowManagerCacheTest::testCreateInstance in State Machine 8
Tests that workflow plugins are cached upon creation.
@covers ::createInstance
File
- tests/
src/ Unit/ WorkflowManagerCacheTest.php, line 98
Class
- WorkflowManagerCacheTest
- Tests the cache of the workflow manager.
Namespace
Drupal\Tests\state_machine\UnitCode
public function testCreateInstance() {
// In this test we will ask the workflow manager repeatedly for an instance
// of a single workflow plugin. Since the manager is supposed to cache the
// plugins, it is expected that the GuardFactory (which is a dependency of
// the Workflow plugin) is not retrieved more than once from the container.
$guard_factory = $this
->prophesize(GuardFactoryInterface::class);
$this->container
->get('state_machine.guard_factory')
->willReturn($guard_factory
->reveal())
->shouldBeCalledOnce();
// It is expected that the workflow manager will retrieve the complete list
// of workflows from the cache backend in order to look up the definition of
// the workflow plugin that we are creating. Even though we are creating
// multiple instances, this should only be called once since the result
// should be cached in memory.
$this->cacheBackend
->get('workflow')
->willReturn((object) [
'data' => $this
->getMockWorkflowDefinitions(),
])
->shouldBeCalledOnce();
// Once it found the workflow definition, it is expected that the workflow
// group manager will be asked for the definition of the workflow plugin
// that is used by the group. The workflow manager requires this data in
// order to discover the class name of the workflow plugin so that it can be
// instantiated. This too should be called only once.
$this->workflowGroupManager
->getDefinition($this->groupId)
->willReturn([
'workflow_class' => Workflow::class,
])
->shouldBeCalledOnce();
// Request the same plugin instance multiple times from the workflow
// manager. The first instance is cached, and all subsequent invocations
// will retrieve the instance from cache.
for ($i = 0; $i < 5; $i++) {
$plugin = $this->workflowManager
->createInstance($this->pluginId);
$this
->assertInstanceOf(WorkflowInterface::class, $plugin);
}
}