View source
<?php
namespace Drupal\Tests\purge\Kernel\Queue;
use Drupal\purge\Plugin\Purge\Queue\PluginManager;
use Drupal\Tests\purge\Kernel\KernelPluginManagerTestBase;
abstract class PluginTestBase extends KernelPluginManagerTestBase {
protected $pluginId;
protected $pluginManagerClass = PluginManager::class;
protected $queue;
public function setUp($switch_to_memory_queue = TRUE) : void {
parent::setUp($switch_to_memory_queue);
$this
->setUpQueuePlugin();
}
protected function setUpQueuePlugin() : void {
if (is_null($this->queue)) {
$this->queue = $this->pluginManager
->createInstance($this->pluginId);
$this
->assertNull($this->queue
->createQueue());
}
}
public function testDataStorageIntegrity() : void {
$samples = [
'a' => 'string',
'b' => 'StrinG with Capitalization',
'c' => 1,
'd' => -1500,
'e' => 0.15,
'f' => -99999,
'g' => NULL,
'h' => FALSE,
'i' => TRUE,
];
foreach ($samples as $sample) {
$this->queue
->createItem($sample);
$reference = $this->queue
->claimItem(3600);
$this
->assertSame($reference->data, $sample);
}
$this->queue
->createItem($samples);
$reference = $this->queue
->claimItem(3600);
$this
->assertSame($reference->data, $samples);
$this->queue
->deleteQueue();
}
public function testQueueCountBehavior() : void {
$this
->assertNull($this->queue
->deleteQueue());
$this
->assertSame(TRUE, is_int($this->queue
->numberOfItems()));
$this
->assertEquals(0, $this->queue
->numberOfItems());
for ($i = 1; $i <= 5; $i++) {
$id = $this->queue
->createItem($i);
$this
->assertSame(TRUE, is_scalar($id));
$this
->assertSame(TRUE, $id !== FALSE);
$this
->assertEquals($i, $this->queue
->numberOfItems());
}
$this
->assertSame(TRUE, is_object($this->queue
->claimItem(1)));
$this
->assertSame(TRUE, is_int($this->queue
->numberOfItems()));
$this
->assertEquals(5, $this->queue
->numberOfItems());
$this
->assertNull($this->queue
->deleteQueue());
$this
->assertEquals(0, $this->queue
->numberOfItems());
for ($i = 1; $i <= 10; $i++) {
$this->queue
->createItem($i);
}
for ($i = 10; $i > 5; $i--) {
$claim = $this->queue
->claimItem();
$this
->assertNull($this->queue
->deleteItem($claim));
$this
->assertEquals($i - 1, $this->queue
->numberOfItems());
}
$claims = $this->queue
->claimItemMultiple(5);
$this->queue
->deleteItemMultiple($claims);
$this
->assertEquals(0, $this->queue
->numberOfItems());
$this->queue
->deleteQueue();
}
public function testCreateQueue() : void {
$this->queue
->createItem([
1,
2,
3,
]);
$this->queue
->createQueue();
$this
->assertEquals(1, $this->queue
->numberOfItems());
$this->queue
->deleteQueue();
}
public function testCreatingClaimingAndReleasing() : void {
$this->queue
->createItem([
1,
2,
3,
]);
$claim = $this->queue
->claimItem(3600);
$claim->data = [
4,
5,
6,
];
$this
->assertSame(FALSE, $this->queue
->claimItem(3600));
$this
->assertSame(TRUE, $this->queue
->releaseItem($claim));
$this
->assertSame(TRUE, is_object($claim = $this->queue
->claimItem(3600)));
$this
->assertSame($claim->data, [
4,
5,
6,
]);
$this->queue
->releaseItem($claim);
$this
->assertSame(count($this->queue
->createItemMultiple([
1,
2,
3,
4,
])), 4);
$claims = $this->queue
->claimItemMultiple(5, 3600);
foreach ($claims as $i => $claim) {
$claim->data = 9;
$claims[$i] = $claim;
}
$this
->assertSame($this->queue
->claimItemMultiple(5, 3600), []);
$this
->assertSame($this->queue
->releaseItemMultiple($claims), []);
$claims = $this->queue
->claimItemMultiple(5, 3600);
$this
->assertSame(count($claims), 5);
foreach ($claims as $i => $claim) {
$this
->assertEquals(9, $claim->data);
}
$this->queue
->deleteQueue();
}
public function testLeaseTime() : void {
$this
->assertSame(FALSE, $this->queue
->claimItem());
$this->queue
->createItem($this
->randomString());
$this
->assertEquals(1, $this->queue
->numberOfItems());
$this
->assertSame(TRUE, is_object($this->queue
->claimItem(5)));
$this
->assertSame(FALSE, $this->queue
->claimItem());
sleep(6);
$this
->assertSame(TRUE, is_object($this->queue
->claimItem(2)));
$this
->assertSame(FALSE, $this->queue
->claimItem(1));
sleep(3);
$this
->assertSame(TRUE, is_object($this->queue
->claimItem(2)));
$this->queue
->deleteQueue();
$this
->assertSame(TRUE, empty($this->queue
->claimItemMultiple(2)));
for ($i = 1; $i <= 5; $i++) {
$this->queue
->createItem($this
->randomString());
}
$this
->assertSame(count($this->queue
->claimItemMultiple(5, 5)), 5);
$this
->assertSame(TRUE, empty($this->queue
->claimItemMultiple(2)));
sleep(6);
$this
->assertSame(count($this->queue
->claimItemMultiple(5, 5)), 5);
$this->queue
->deleteQueue();
}
public function testPaging() : void {
$this
->assertEquals(0, $this->queue
->numberOfItems());
$this
->assertEquals($this->queue
->selectPageLimit(), 15);
$this
->assertEquals($this->queue
->selectPageLimit(37), 37);
$this
->assertEquals($this->queue
->selectPageLimit(), 37);
$this
->assertEquals($this->queue
->selectPageLimit(7), 7);
$this
->assertEquals($this->queue
->selectPageLimit(), 7);
$this
->assertEquals($this->queue
->selectPageMax(), 0);
$this
->assertEquals($this->queue
->selectPage(), []);
$this
->assertSame(count($this->queue
->createItemMultiple([
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
])), 25);
$this
->assertEquals($this->queue
->selectPageMax(), 4);
$this
->assertEquals($this->queue
->selectPageLimit(5), 5);
$this
->assertEquals($this->queue
->selectPageMax(), 5);
$this
->assertEquals($this->queue
->selectPageLimit(7), 7);
$page_1 = $this->queue
->selectPage();
$this
->assertEquals(count($page_1), 7);
$this
->assertEquals($page_1[0]->item_id, 1);
$this
->assertEquals($page_1[1]->item_id, 2);
$this
->assertEquals($page_1[2]->item_id, 3);
$this
->assertEquals($page_1[3]->item_id, 4);
$this
->assertEquals($page_1[4]->item_id, 5);
$this
->assertEquals($page_1[5]->item_id, 6);
$this
->assertEquals($page_1[6]->item_id, 7);
$this
->assertEquals($page_1[0]->data, 1);
$this
->assertEquals($page_1[1]->data, 2);
$this
->assertEquals($page_1[2]->data, 3);
$this
->assertEquals($page_1[3]->data, 4);
$this
->assertEquals($page_1[4]->data, 5);
$this
->assertEquals($page_1[5]->data, 6);
$this
->assertEquals($page_1[6]->data, 7);
$this
->assertEquals($page_1[0]->expire, 0);
$this
->assertEquals($page_1[1]->expire, 0);
$this
->assertEquals($page_1[2]->expire, 0);
$this
->assertEquals($page_1[3]->expire, 0);
$this
->assertEquals($page_1[4]->expire, 0);
$this
->assertEquals($page_1[5]->expire, 0);
$this
->assertEquals($page_1[6]->expire, 0);
$page_2 = $this->queue
->selectPage(2);
$this
->assertEquals(count($page_2), 7);
$this
->assertEquals($page_2[0]->data, 8);
$this
->assertEquals($page_2[1]->data, 9);
$this
->assertEquals($page_2[2]->data, 10);
$this
->assertEquals($page_2[3]->data, 11);
$this
->assertEquals($page_2[4]->data, 12);
$this
->assertEquals($page_2[5]->data, 13);
$this
->assertEquals($page_2[6]->data, 14);
$page_3 = $this->queue
->selectPage(3);
$this
->assertEquals(count($page_3), 7);
$this
->assertEquals($page_3[0]->data, 15);
$this
->assertEquals($page_3[1]->data, 16);
$this
->assertEquals($page_3[2]->data, 17);
$this
->assertEquals($page_3[3]->data, 18);
$this
->assertEquals($page_3[4]->data, 19);
$this
->assertEquals($page_3[5]->data, 20);
$this
->assertEquals($page_3[6]->data, 21);
$page_4 = $this->queue
->selectPage(4);
$this
->assertEquals(count($page_4), 4);
$this
->assertEquals($page_4[0]->data, 22);
$this
->assertEquals($page_4[1]->data, 23);
$this
->assertEquals($page_4[2]->data, 24);
$this
->assertEquals($page_4[3]->data, 25);
$this
->assertEquals($this->queue
->selectPage(5), []);
$this->queue
->deleteQueue();
}
}