Completed
Push — master ( 9e3c5b...9ae6ad )
by Rasmus
04:22
created

DatabaseCacheIntegrationTest::testCleanExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Kodus\Cache\Test;
4
5
use Kodus\Cache\Tests\SimpleCacheTest;
6
use Kodus\Cache\Tests\TestableDatabaseCache;
7
use PDO;
8
use PDOException;
9
10
abstract class DatabaseCacheIntegrationTest extends SimpleCacheTest
11
{
12
    const TABLE_NAME  = "test_cache";
13
    const DEFAULT_TTL = 86400;
14
15
    /**
16
     * @var TestableDatabaseCache
17
     */
18
    protected $cache;
19
20
    /**
21
     * @var PDO
22
     */
23
    private static $pdo;
24
25
    public static function setUpBeforeClass(): void
26
    {
27
        $name = static::getConnectionName();
28
29
        self::$pdo = new PDO(
30
            constant("DB_CACHE_TEST_{$name}_DSN"),
31
            constant("DB_CACHE_TEST_{$name}_USERNAME"),
32
            constant("DB_CACHE_TEST_{$name}_PASSWORD")
33
        );
34
35
        try {
36
            self::$pdo->exec("DROP TABLE " . self::TABLE_NAME);
37
        } catch (PDOException $error) {
38
            // ignored.
39
        }
40
    }
41
42
    abstract protected static function getConnectionName(): string;
43
44
    /**
45
     * @skip
46
     */
47
    public function createSimpleCache()
48
    {
49
        return new TestableDatabaseCache(
50
            self::$pdo,
51
            self::TABLE_NAME,
52
            self::DEFAULT_TTL
53
        );
54
    }
55
56
    protected function sleep(int $time)
57
    {
58
        $this->cache->timeTravel($time);
59
    }
60
61
    public function testCleanExpired()
62
    {
63
        $this->cache->set('key0', 'value', 5);
64
        $this->cache->set('key1', 'value', 10);
65
66
        $this->cache->timeTravel(5);
67
        $this->cache->cleanExpired();
68
69
        $this->assertFalse($this->cache->has('key0'), "key0 expires after 5 seconds");
70
        $this->assertTrue($this->cache->has('key1'), "key1 has not expired");
71
72
        $this->cache->timeTravel(5);
73
        $this->cache->cleanExpired();
74
75
        $this->assertFalse($this->cache->has('key1'), "key1 expires after 10 seconds");
76
    }
77
}
78