QueueTest.php in Drupal 8
File
core/tests/Drupal/KernelTests/Core/Queue/QueueTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Queue;
use Drupal\Core\Database\Database;
use Drupal\Core\Queue\DatabaseQueue;
use Drupal\Core\Queue\Memory;
use Drupal\KernelTests\KernelTestBase;
class QueueTest extends KernelTestBase {
public function testSystemQueue() {
$queue1 = new DatabaseQueue($this
->randomMachineName(), Database::getConnection());
$queue1
->createQueue();
$queue2 = new DatabaseQueue($this
->randomMachineName(), Database::getConnection());
$queue2
->createQueue();
$this
->runQueueTest($queue1, $queue2);
}
public function testMemoryQueue() {
$queue1 = new Memory($this
->randomMachineName());
$queue1
->createQueue();
$queue2 = new Memory($this
->randomMachineName());
$queue2
->createQueue();
$this
->runQueueTest($queue1, $queue2);
}
protected function runQueueTest($queue1, $queue2) {
$data = [];
for ($i = 0; $i < 4; $i++) {
$data[] = [
$this
->randomMachineName() => $this
->randomMachineName(),
];
}
$queue1
->createItem($data[0]);
$queue1
->createItem($data[1]);
$items = [];
$new_items = [];
$items[] = $item = $queue1
->claimItem();
$new_items[] = $item->data;
$items[] = $item = $queue1
->claimItem();
$new_items[] = $item->data;
$this
->assertEqual($this
->queueScore($data, $new_items), 2, 'Two items matched');
$queue1
->createItem($data[2]);
$queue1
->createItem($data[3]);
$this
->assertSame(4, $queue1
->numberOfItems(), 'Queue 1 is not empty after adding items.');
$this
->assertSame(0, $queue2
->numberOfItems(), 'Queue 2 is empty while Queue 1 has items');
$items[] = $item = $queue1
->claimItem();
$new_items[] = $item->data;
$items[] = $item = $queue1
->claimItem();
$new_items[] = $item->data;
$this
->assertEqual($this
->queueScore($data, $new_items), 4, 'Four items matched');
$this
->assertEqual($this
->queueScore($new_items, $new_items), 4, 'Four items matched');
foreach ($items as $item) {
$queue1
->deleteItem($item);
}
$this
->assertSame(0, $queue1
->numberOfItems(), 'Queue 1 is empty');
$this
->assertSame(0, $queue2
->numberOfItems(), 'Queue 2 is empty');
}
protected function queueScore($items, $new_items) {
$score = 0;
foreach ($items as $item) {
foreach ($new_items as $new_item) {
if ($item === $new_item) {
$score++;
}
}
}
return $score;
}
}
Classes
Name |
Description |
QueueTest |
Queues and dequeues a set of items to check the basic queue functionality. |