Passed
Push — main ( bce8dd...f4f8ca )
by Sílvio
56s
created

test_migrate_creates_custom_table()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 25
rs 9.6666
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);
0 ignored issues
show
Bug introduced by
It seems like $this->pdo can also be of type null; however, parameter $connection of Silviooosilva\CacheerPhp...ationManager::migrate() does only seem to accept PDO, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
    MigrationManager::migrate(/** @scrutinizer ignore-type */ $this->pdo, $this->table);
Loading history...
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'");
0 ignored issues
show
Bug introduced by
The method query() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
    /** @scrutinizer ignore-call */ 
59
    $stmt = $this->pdo->query("SELECT name FROM sqlite_master WHERE type='table' AND name = 'cacheer_table'");

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.

Loading history...
59
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
60
    $this->assertNotFalse($row);
61
  }
62
}
63