FileQueueTest.php in Purge 8.3
File
tests/src/Kernel/Queue/FileQueueTest.php
View source
<?php
namespace Drupal\Tests\purge\Kernel\Queue;
use Drupal\Core\DestructableInterface;
use Drupal\Core\StreamWrapper\PublicStream;
class FileQueueTest extends PluginTestBase {
protected $pluginId = 'file';
protected $file = 'purge-file.queue';
public function setUp($switch_to_memory_queue = TRUE) : void {
parent::setUp($switch_to_memory_queue);
$this->file = DRUPAL_ROOT . '/' . PublicStream::basePath() . '/' . $this->file;
}
public function testBufferReadingAndWriting() : void {
$this
->assertTrue($this->queue instanceof DestructableInterface);
$this
->assertFalse(file_exists($this->file));
$payloads = function ($base, $time) {
return [
$base . ($time - 2) . "\n",
$base . ($time - 1) . "\n",
$base . $time . "\n",
$base . ($time + 1) . "\n",
$base . ($time + 2) . "\n",
];
};
$this->queue
->createItem('s1');
$this
->assertFalse(file_exists($this->file));
$this->queue
->destruct();
$this
->assertTrue(file_exists($this->file));
$this
->assertTrue(in_array(file_get_contents($this->file), $payloads('1|s:2:"s1";|0|', time())));
$this->queue
->deleteQueue();
$this
->assertFalse(file_exists($this->file));
$this->queue
->createItem('s2');
$i = $this->queue
->claimItem();
$this->queue
->destruct();
$this
->assertTrue(file_exists($this->file));
$this
->assertTrue(in_array(file_get_contents($this->file), $payloads('1|s:2:"s2";|' . $i->expire . '|', $i->created)));
$this->queue
->deleteQueue();
$this->queue = NULL;
file_put_contents($this->file, '1|s:6:"qwerty";|0|12345' . "\n");
$this
->assertTrue(file_exists($this->file));
$this
->setUpQueuePlugin();
$claim = $this->queue
->claimItem(1);
$this
->assertTrue(is_object($claim));
$this
->assertEquals(1, $claim->item_id);
$this
->assertEquals('qwerty', $claim->data);
$this
->assertEquals(12345, $claim->created);
$this->queue
->deleteQueue();
}
}