1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
4
|
|
|
use Silviooosilva\CacheerPhp\Core\Connect; |
5
|
|
|
use Silviooosilva\CacheerPhp\Core\MigrationManager; |
6
|
|
|
use Silviooosilva\CacheerPhp\Repositories\CacheDatabaseRepository; |
7
|
|
|
|
8
|
|
|
class MigrationManagerDynamicTableTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
private ?PDO $pdo = null; |
11
|
|
|
private string $table; |
12
|
|
|
|
13
|
|
|
protected function setUp(): void |
14
|
|
|
{ |
15
|
|
|
$this->pdo = Connect::getInstance(); |
16
|
|
|
$this->table = uniqid('mm_custom_table_'); |
17
|
|
|
// Ensure clean start |
18
|
|
|
$this->pdo->exec("DROP TABLE IF EXISTS {$this->table}"); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function tearDown(): void |
22
|
|
|
{ |
23
|
|
|
if ($this->pdo) { |
24
|
|
|
$this->pdo->exec("DROP TABLE IF EXISTS {$this->table}"); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function test_migrate_creates_custom_table() |
29
|
|
|
{ |
30
|
|
|
MigrationManager::migrate($this->pdo, $this->table); |
|
|
|
|
31
|
|
|
|
32
|
|
|
// Verify table exists (SQLite check) |
33
|
|
|
$stmt = $this->pdo->prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = :t"); |
34
|
|
|
$stmt->bindValue(':t', $this->table); |
35
|
|
|
$stmt->execute(); |
36
|
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC); |
37
|
|
|
$this->assertNotFalse($row); |
38
|
|
|
|
39
|
|
|
// Verify basic insert/select using PDO |
40
|
|
|
$expiration = date('Y-m-d H:i:s', time() + 60); |
41
|
|
|
$stmt = $this->pdo->prepare("INSERT INTO {$this->table} (cacheKey, cacheData, cacheNamespace, expirationTime, created_at) VALUES (:k, :d, '', :e, :c)"); |
42
|
|
|
$stmt->bindValue(':k', 'mk'); |
43
|
|
|
$stmt->bindValue(':d', serialize(['a' => 1])); |
44
|
|
|
$stmt->bindValue(':e', $expiration); |
45
|
|
|
$stmt->bindValue(':c', date('Y-m-d H:i:s')); |
46
|
|
|
$this->assertTrue($stmt->execute()); |
47
|
|
|
|
48
|
|
|
$stmt = $this->pdo->prepare("SELECT cacheData FROM {$this->table} WHERE cacheKey = :k LIMIT 1"); |
49
|
|
|
$stmt->bindValue(':k', 'mk'); |
50
|
|
|
$stmt->execute(); |
51
|
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC); |
52
|
|
|
$this->assertEquals(['a' => 1], unserialize($row['cacheData'])); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function test_default_constant_table_exists() |
56
|
|
|
{ |
57
|
|
|
// With boot autoload, the default CACHEER_TABLE should be created via Connect::getInstance() |
58
|
|
|
$stmt = $this->pdo->query("SELECT name FROM sqlite_master WHERE type='table' AND name = 'cacheer_table'"); |
|
|
|
|
59
|
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC); |
60
|
|
|
$this->assertNotFalse($row); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|