Passed
Pull Request — master (#251)
by Gabriel
10:21
created

NotSetStateClass   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
wmc 2
lcom 0
cbo 0
rs 10
1
<?php
2
3
namespace Doctrine\Tests\Common\Cache;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\CacheProvider;
7
use Doctrine\Common\Cache\PhpFileCache;
8
use const PHP_VERSION_ID;
9
10
/**
11
 * @group DCOM-101
12
 */
13
class PhpFileCacheTest extends BaseFileCacheTest
14
{
15
    public function provideDataToCache() : array
16
    {
17
        $data = parent::provideDataToCache();
18
19
        unset($data['object'], $data['object_recursive']); // PhpFileCache only allows objects that implement __set_state() and fully support var_export()
20
21
        if (PHP_VERSION_ID < 70002) {
22
            unset($data['float_zero']); // var_export exports float(0) as int(0): https://bugs.php.net/bug.php?id=66179
23
        }
24
25
        return $data;
26
    }
27
28
    public function testImplementsSetState() : void
29
    {
30
        $cache = $this->_getCacheDriver();
31
32
        // Test save
33
        $cache->save('test_set_state', new SetStateClass([1, 2, 3]));
34
35
        //Test __set_state call
36
        self::assertCount(0, SetStateClass::$values);
37
38
        // Test fetch
39
        $value = $cache->fetch('test_set_state');
40
        self::assertInstanceOf(SetStateClass::class, $value);
41
        self::assertEquals([1, 2, 3], $value->getValue());
42
43
        //Test __set_state call
44
        self::assertCount(1, SetStateClass::$values);
45
46
        // Test contains
47
        self::assertTrue($cache->contains('test_set_state'));
48
    }
49
50
    /**
51
     * @group 154
52
     */
53
    public function testNotImplementsSetState() : void
54
    {
55
        $cache = $this->_getCacheDriver();
56
57
        $cache->save('test_not_set_state', new NotSetStateClass([5, 6, 7]));
58
        self::assertEquals(new NotSetStateClass([5, 6, 7]), $cache->fetch('test_not_set_state'));
59
    }
60
61
    /**
62
     * @group 154
63
     */
64
    public function testNotImplementsSetStateInArray() : void
65
    {
66
        $cache = $this->_getCacheDriver();
67
68
        $cache->save('test_not_set_state_in_array', [new NotSetStateClass([4, 3, 2])]);
69
        self::assertEquals([new NotSetStateClass([4, 3, 2])], $cache->fetch('test_not_set_state_in_array'));
70
        self::assertTrue($cache->contains('test_not_set_state_in_array'));
71
    }
72
73
    public function testGetStats() : void
74
    {
75
        $cache = $this->_getCacheDriver();
76
        $stats = $cache->getStats();
77
78
        self::assertNull($stats[Cache::STATS_HITS]);
79
        self::assertNull($stats[Cache::STATS_MISSES]);
80
        self::assertNull($stats[Cache::STATS_UPTIME]);
81
        self::assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
82
        self::assertGreaterThan(0, $stats[Cache::STATS_MEMORY_AVAILABLE]);
83
    }
84
85
    protected function _getCacheDriver() : CacheProvider
86
    {
87
        return new PhpFileCache($this->directory);
88
    }
89
}
90
91
class NotSetStateClass
92
{
93
    private $value;
94
95
    public function __construct($value)
96
    {
97
        $this->value = $value;
98
    }
99
100
    public function getValue()
101
    {
102
        return $this->value;
103
    }
104
}
105
106
class SetStateClass extends NotSetStateClass
107
{
108
    public static $values = [];
109
110
    public static function __set_state($data)
111
    {
112
        self::$values = $data;
113
        return new self($data['value']);
114
    }
115
}
116