1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
4
|
|
|
use Silviooosilva\CacheerPhp\Cacheer; |
5
|
|
|
use Silviooosilva\CacheerPhp\Config\Option\Builder\OptionBuilder; |
6
|
|
|
use Silviooosilva\CacheerPhp\Core\Connect; |
7
|
|
|
|
8
|
|
|
class DatabaseOptionBuilderStoreTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
private ?PDO $pdo = null; |
11
|
|
|
private string $table = 'cache_items'; |
12
|
|
|
|
13
|
|
|
protected function setUp(): void |
14
|
|
|
{ |
15
|
|
|
// Ensure SQLite connection and create a custom table for this test |
16
|
|
|
$this->pdo = Connect::getInstance(); |
17
|
|
|
|
18
|
|
|
// Create table compatible with SQLite |
19
|
|
|
$this->pdo->exec("CREATE TABLE IF NOT EXISTS {$this->table} ( |
20
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT, |
21
|
|
|
cacheKey VARCHAR(255) NOT NULL, |
22
|
|
|
cacheData TEXT NOT NULL, |
23
|
|
|
cacheNamespace VARCHAR(255), |
24
|
|
|
expirationTime DATETIME NOT NULL, |
25
|
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, |
26
|
|
|
UNIQUE(cacheKey, cacheNamespace) |
27
|
|
|
); |
28
|
|
|
CREATE INDEX IF NOT EXISTS idx_{$this->table}_cacheKey ON {$this->table} (cacheKey); |
29
|
|
|
CREATE INDEX IF NOT EXISTS idx_{$this->table}_cacheNamespace ON {$this->table} (cacheNamespace); |
30
|
|
|
CREATE INDEX IF NOT EXISTS idx_{$this->table}_expirationTime ON {$this->table} (expirationTime); |
31
|
|
|
CREATE INDEX IF NOT EXISTS idx_{$this->table}_key_namespace ON {$this->table} (cacheKey, cacheNamespace); |
32
|
|
|
"); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function tearDown(): void |
36
|
|
|
{ |
37
|
|
|
if ($this->pdo instanceof PDO) { |
38
|
|
|
$this->pdo->exec("DROP TABLE IF EXISTS {$this->table}"); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function test_database_store_uses_custom_table_from_option_builder() |
43
|
|
|
{ |
44
|
|
|
$options = OptionBuilder::forDatabase() |
45
|
|
|
->table($this->table) |
46
|
|
|
->build(); |
47
|
|
|
|
48
|
|
|
$cache = new Cacheer($options); |
49
|
|
|
$cache->setDriver()->useDatabaseDriver(); |
50
|
|
|
|
51
|
|
|
$key = 'opt_table_key'; |
52
|
|
|
$data = ['x' => 1]; |
53
|
|
|
|
54
|
|
|
$cache->putCache($key, $data, '', 3600); |
55
|
|
|
$this->assertTrue($cache->isSuccess()); |
56
|
|
|
|
57
|
|
|
// Validate via direct SQL against the custom table |
58
|
|
|
$nowFunction = "DATETIME('now', 'localtime')"; |
59
|
|
|
$stmt = $this->pdo->prepare("SELECT cacheData FROM {$this->table} WHERE cacheKey = :k AND cacheNamespace = '' AND expirationTime > $nowFunction LIMIT 1"); |
|
|
|
|
60
|
|
|
$stmt->bindValue(':k', $key); |
61
|
|
|
$stmt->execute(); |
62
|
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC); |
63
|
|
|
|
64
|
|
|
$this->assertNotFalse($row); |
65
|
|
|
$this->assertEquals($data, unserialize($row['cacheData'])); |
66
|
|
|
|
67
|
|
|
// And ensure Cacheer can read it back through the store |
68
|
|
|
$read = $cache->getCache($key); |
69
|
|
|
$this->assertEquals($data, $read); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.