-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqliteStorageTest.php
More file actions
66 lines (51 loc) · 1.84 KB
/
SqliteStorageTest.php
File metadata and controls
66 lines (51 loc) · 1.84 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
use Slowmove\SimplePhpQueue\Storage\Adapters\SqliteStorage;
beforeEach(function () {
$this->tempDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'simple-php-queue-' . bin2hex(random_bytes(8));
mkdir($this->tempDir, 0777, true);
});
afterEach(function () {
$cleanup = function (string $path) use (&$cleanup): void {
if (!is_dir($path)) {
if (is_file($path)) {
unlink($path);
}
return;
}
$entries = array_diff(scandir($path), ['.', '..']);
foreach ($entries as $entry) {
$childPath = $path . DIRECTORY_SEPARATOR . $entry;
if (is_dir($childPath)) {
$cleanup($childPath);
continue;
}
unlink($childPath);
}
rmdir($path);
};
if (isset($this->tempDir) && is_dir($this->tempDir)) {
$cleanup($this->tempDir);
}
});
it('creates a sqlite database file when initialized with a directory path', function () {
new SqliteStorage($this->tempDir);
expect(is_file($this->tempDir . DIRECTORY_SEPARATOR . 'queue.db'))->toBeTrue();
});
it('stores values in fifo order and reports queue state', function () {
$storage = new SqliteStorage($this->tempDir);
expect($storage->length())->toBe(0);
expect($storage->exist('first'))->toBeFalse();
expect($storage->enqueue('first'))->toBeTrue();
expect($storage->enqueue('second'))->toBeTrue();
expect($storage->length())->toBe(2);
expect($storage->exist('first'))->toBeTrue();
expect($storage->exist('second'))->toBeTrue();
expect($storage->content())->toBe([
'first',
'second',
]);
expect($storage->dequeue())->toBe('first');
expect($storage->dequeue())->toBe('second');
expect($storage->dequeue())->toBeNull();
expect($storage->length())->toBe(0);
});