aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/lib/classes/StudipCachedArrayTest.php
blob: c98c1bd6b5c151bc9f34656047edec3e4b989e74 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
 * StudipCachedArrayTest.php - unit tests for the StudipCachedArray class
 *
 * @author   Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license  GPL2 or any later version
 *
 * @covers StudipCachedArray
 * @uses StudipMemoryCache
 */

class StudipCachedArrayTest extends \Codeception\Test\Unit
{
    private function getCachedArray()
    {
        return new StudipCachedArray(__CLASS__);
    }

    /**
     * @dataProvider StorageProvider
     */
    public function testStorage($key, $value)
    {
        $cache = $this->getCachedArray();

        // Cache should be empty
        $this->assertFalse(isset($cache[$key]));

        // Set value
        $cache[$key] = $value;

        // Immediate response
        $this->assertTrue(isset($cache[$key]));
        $this->assertEquals($value, $cache[$key]);

        // When reading back from cache
        $cache->reset();

        $this->assertTrue(isset($cache[$key]));
        $this->assertEquals($value, $cache[$key]);

        // Remove value
        unset($cache[$key]);
        $this->assertFalse(isset($cache[$key]));

        $cache->reset();

        $this->assertFalse(isset($cache[$key]));
    }

    /**
     * @depends testStorage
     * @dataProvider StorageProvider
     */
    public function testExpiration($key, $value)
    {
        $cache = $this->getCachedArray();

        $cache[$key] = $value;

        $cache->expire();

        $this->assertFalse(isset($cache[$key]));
    }

    public function StorageProvider(): array
    {
        return [
            // 'null'   => [1, null], // Null is not really testable
            'true'   => [2, true],
            'false'  => [3, false],
            'int'    => [4, 42],
            'string' => ['string', 'bar'],
            'array'  => ['array', ['foo']],
            'object' => ['object', new StudipCachedArrayTestClass()],
        ];
    }
}

// Simple test class
class StudipCachedArrayTestClass
{
    private $foo = 42;
}