class ContentHubExportQueueControllerTest in Acquia Content Hub 8
PHPUnit test for the ContentHubExportQueueController class.
@coversDefaultClass \Drupal\acquia_contenthub\Controller\ContentHubExportQueueController
@group acquia_contenthub
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\acquia_contenthub\Unit\Controller\ContentHubExportQueueControllerTest
Expanded class hierarchy of ContentHubExportQueueControllerTest
File
- tests/
src/ Unit/ Controller/ ContentHubExportQueueControllerTest.php, line 18
Namespace
Drupal\Tests\acquia_contenthub\Unit\ControllerView source
class ContentHubExportQueueControllerTest extends UnitTestCase {
/**
* The dependency injection container.
*
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* The mock config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The Queue Factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $queueFactory;
/**
* The Queue Manager.
*
* @var \Drupal\Core\Queue\QueueWorkerManager
*/
protected $queueManager;
/**
* The Content Hub Export Queue Controller.
*
* @var \Drupal\acquia_contenthub\Controller\ContentHubExportQueueController
*/
protected $contentHubExportQueueController;
/**
* The acquia_contenthub.entity_config array.
*
* @var array
*/
protected $configEntity = [
'dependency_depth' => 3,
'user_role' => AccountInterface::ANONYMOUS_ROLE,
'export_queue_entities_per_item' => 1,
'export_queue_batch_size' => 1,
'export_queue_waiting_time' => 5,
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
$this->container = $this
->createMock('Drupal\\Core\\DependencyInjection\\Container');
$logger_factory = $this
->createMock(LoggerChannelFactoryInterface::class);
$logger_factory
->method('get')
->with('acquia_contenthub')
->willReturn($this
->createMock(LoggerChannelInterface::class));
\Drupal::setContainer($this->container);
$this->configFactory = $this
->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$this->configFactory
->method('get')
->with('acquia_contenthub.entity_config')
->willReturn($this
->createMockForContentHubEntityConfig());
$this->queueFactory = $this
->getMockBuilder('Drupal\\Core\\Queue\\QueueFactory')
->disableOriginalConstructor()
->getMock();
$this->queueManager = $this
->getMockBuilder('Drupal\\Core\\Queue\\QueueWorkerManager')
->disableOriginalConstructor()
->getMock();
$this->contentHubExportQueueController = new ContentHubExportQueueController($this->queueFactory, $this->queueManager, $this->configFactory, $logger_factory);
}
/**
* Test the getQueueCount method.
*
* @covers ::getQueueCount
*/
public function testGetQueueCount() {
$queue = $this
->createMock('Drupal\\Core\\Queue\\QueueInterface');
$queue
->method('numberOfItems')
->willReturn(100);
$this->queueFactory
->method('get')
->with('acquia_contenthub_export_queue')
->willReturn($queue);
$count = $this->contentHubExportQueueController
->getQueueCount();
$this
->assertEquals($count, 100);
}
/**
* Test the getWaitingTime method.
*
* @covers ::getWaitingTime
*/
public function testGetWaitingTime() {
$waiting_time = $this->contentHubExportQueueController
->getWaitingTime();
$this
->assertEquals($waiting_time, 5);
}
/**
* Test the enqueueExportEntities method.
*
* @covers ::enqueueExportEntities
*/
public function testEnqueueExportEntities() {
$queued_items = [
$this
->createQueueItem('node', 1, '00000000-0000-0000-0000-000000000000'),
$this
->createQueueItem('node', 2, '00000000-0000-1111-0000-000000000000'),
$this
->createQueueItem('node', 3, '00000000-0000-2222-0000-000000000000'),
$this
->createQueueItem('node', 4, '00000000-0000-3333-0000-000000000000'),
$this
->createQueueItem('node', 5, '00000000-0000-4444-0000-000000000000'),
$this
->createQueueItem('node', 6, '00000000-0000-5555-0000-000000000000'),
$this
->createQueueItem('node', 7, '00000000-0000-6666-0000-000000000000'),
];
// Defining the queue.
$queue = $this
->createMock('Drupal\\Core\\Queue\\QueueInterface');
$queue
->method('numberOfItems')
->willReturn(100);
$queue
->expects($this
->at(0))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[0];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(1))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[1];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(2))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[2];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(3))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[3];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(4))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[4];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(5))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[5];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(6))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[6];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$this->queueFactory
->method('get')
->with('acquia_contenthub_export_queue')
->willReturn($queue);
// Defining candidate entities.
$candidate_entities = [
'00000000-0000-0000-0000-000000000000' => $this
->createMockForContentEntity('node', 1, '00000000-0000-0000-0000-000000000000'),
'00000000-0000-1111-0000-000000000000' => $this
->createMockForContentEntity('node', 2, '00000000-0000-1111-0000-000000000000'),
'00000000-0000-2222-0000-000000000000' => $this
->createMockForContentEntity('node', 3, '00000000-0000-2222-0000-000000000000'),
'00000000-0000-3333-0000-000000000000' => $this
->createMockForContentEntity('node', 4, '00000000-0000-3333-0000-000000000000'),
'00000000-0000-4444-0000-000000000000' => $this
->createMockForContentEntity('node', 5, '00000000-0000-4444-0000-000000000000'),
'00000000-0000-5555-0000-000000000000' => $this
->createMockForContentEntity('node', 6, '00000000-0000-5555-0000-000000000000'),
'00000000-0000-6666-0000-000000000000' => $this
->createMockForContentEntity('node', 7, '00000000-0000-6666-0000-000000000000'),
];
// Enqueue entities.
$this->contentHubExportQueueController
->enqueueExportEntities($candidate_entities);
}
/**
* Returns a fake ContentHubEntityConfig object.
*
* @return \Drupal\Core\Config\ImmutableConfig
* The fake config.
*/
protected function createMockForContentHubEntityConfig() {
$contenthub_entity_config = $this
->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
->disableOriginalConstructor()
->setMethods([
'get',
])
->getMockForAbstractClass();
$contenthub_entity_config
->method('get')
->willReturnCallback(function ($argument) {
if (isset($this->configEntity[$argument])) {
return $this->configEntity[$argument];
}
return NULL;
});
return $contenthub_entity_config;
}
/**
* Creates a mock content entity.
*
* @param string $entity_type_id
* The Entity type ID.
* @param string $id
* The Entity ID.
* @param string $uuid
* The Entity UUID.
*
* @return \PHPUnit_Framework_MockObject_MockObject
* The fake ContentEntity.
*/
protected function createMockForContentEntity($entity_type_id, $id, $uuid) {
$enabled_methods = [
'getEntityTypeId',
'id',
'uuid',
'getFields',
];
$content_entity_mock = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
->disableOriginalConstructor()
->setMethods($enabled_methods)
->getMockForAbstractClass();
$content_entity_mock
->method('getEntityTypeId')
->willReturn($entity_type_id);
$content_entity_mock
->method('id')
->willReturn($id);
$content_entity_mock
->method('uuid')
->willReturn($uuid);
// No fields. We are just testing we can add item to the queue.
$content_entity_mock
->method('getFields')
->willReturn([]);
return $content_entity_mock;
}
/**
* Creates a queue item object.
*
* @param string $entity_type
* The entity type.
* @param string $entity_id
* The entity ID.
* @param string $entity_uuid
* The entity UUID.
*
* @return object
* The queue item.
*/
protected function createQueueItem($entity_type, $entity_id, $entity_uuid) {
return (object) [
'data' => [
0 => [
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'entity_uuid' => $entity_uuid,
],
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentHubExportQueueControllerTest:: |
protected | property | The acquia_contenthub.entity_config array. | |
ContentHubExportQueueControllerTest:: |
protected | property | The mock config factory. | |
ContentHubExportQueueControllerTest:: |
protected | property | The dependency injection container. | |
ContentHubExportQueueControllerTest:: |
protected | property | The Content Hub Export Queue Controller. | |
ContentHubExportQueueControllerTest:: |
protected | property | The Queue Factory. | |
ContentHubExportQueueControllerTest:: |
protected | property | The Queue Manager. | |
ContentHubExportQueueControllerTest:: |
protected | function | Creates a mock content entity. | |
ContentHubExportQueueControllerTest:: |
protected | function | Returns a fake ContentHubEntityConfig object. | |
ContentHubExportQueueControllerTest:: |
protected | function | Creates a queue item object. | |
ContentHubExportQueueControllerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ContentHubExportQueueControllerTest:: |
public | function | Test the enqueueExportEntities method. | |
ContentHubExportQueueControllerTest:: |
public | function | Test the getQueueCount method. | |
ContentHubExportQueueControllerTest:: |
public | function | Test the getWaitingTime method. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |