1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KochTest\Cache; |
4
|
|
|
|
5
|
|
|
use Koch\Cache\Adapter\File; |
6
|
|
|
|
7
|
|
|
class AbstractCacheTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var AbstractCache |
11
|
|
|
*/ |
12
|
|
|
protected $object; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Sets up the fixture, for example, opens a network connection. |
16
|
|
|
* This method is called before a test is executed. |
17
|
|
|
*/ |
18
|
|
|
public function setUp() |
19
|
|
|
{ |
20
|
|
|
// we are using the cache adapter File here, |
21
|
|
|
// it's a class extending the abstract class |
22
|
|
|
// abstract classes cannot be instantiated |
23
|
|
|
$this->object = new File(); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Tears down the fixture, for example, closes a network connection. |
28
|
|
|
* This method is called after a test is executed. |
29
|
|
|
*/ |
30
|
|
|
public function tearDown() |
31
|
|
|
{ |
32
|
|
|
unset($this->object); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @covers Koch\Cache\AbstractCache::__construct |
37
|
|
|
* @covers Koch\Autoload\Loader::autoload |
38
|
|
|
*/ |
39
|
|
|
public function testConstructor() |
40
|
|
|
{ |
41
|
|
|
unset($this->object); |
42
|
|
|
|
43
|
|
|
$options = ['prefix' => 'value']; |
44
|
|
|
$this->object = new File($options); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->assertEquals('value', $this->object->options['prefix']); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @covers Koch\Cache\AbstractCache::setOptions |
51
|
|
|
* @covers Koch\Cache\AbstractCache::setOption |
52
|
|
|
*/ |
53
|
|
|
public function testSetOptions() |
54
|
|
|
{ |
55
|
|
|
$options = [ |
56
|
|
|
'prefix' => 'someprefix', |
57
|
|
|
'ttl' => 1337, |
58
|
|
|
]; |
59
|
|
|
$this->object->setOptions($options); |
60
|
|
|
|
61
|
|
|
$this->assertEquals('someprefix', $this->object->options['prefix']); |
62
|
|
|
$this->assertEquals(1337, $this->object->options['ttl']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function SetOptionDataprovider() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
['prefix', 'some-prefix'], |
69
|
|
|
['ttl', 1337], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @covers Koch\Cache\AbstractCache::setOption |
75
|
|
|
* @dataProvider SetOptionDataprovider |
76
|
|
|
*/ |
77
|
|
|
public function testSetOption($key, $value) |
78
|
|
|
{ |
79
|
|
|
$this->object->setOption($key, $value); |
80
|
|
|
$this->assertEquals($this->object->options[$key], $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function SetOptionDataproviderInvalidData() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
['prefix', ''], |
87
|
|
|
['ttl', -1], |
88
|
|
|
['unknown', 'unknown'], |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @covers Koch\Cache\AbstractCache::setOption |
94
|
|
|
* @covers Koch\Cache\AbstractCache::setOptions |
95
|
|
|
* @dataProvider SetOptionDataproviderInvalidData |
96
|
|
|
* @expectedException InvalidArgumentException |
97
|
|
|
*/ |
98
|
|
|
public function testSetOption_throwsException($key, $value) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$this->object->setOption($key, $value); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @covers Koch\Cache\AbstractCache::setPrefix |
105
|
|
|
* @covers Koch\Cache\AbstractCache::getPrefix |
106
|
|
|
*/ |
107
|
|
|
public function testSetPrefix() |
108
|
|
|
{ |
109
|
|
|
$prefix = 'newPrefix'; |
110
|
|
|
$this->object->setPrefix($prefix); |
111
|
|
|
$this->assertEquals($prefix, $this->object->getPrefix()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @covers Koch\Cache\AbstractCache::setPrefix |
116
|
|
|
* @expectedException \InvalidArgumentException |
117
|
|
|
* @expectedExceptionMessage Prefix must not be empty. |
118
|
|
|
*/ |
119
|
|
|
public function testSetPrefix_throwsException() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$this->object->setPrefix(''); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @covers Koch\Cache\AbstractCache::prefixKey |
126
|
|
|
*/ |
127
|
|
|
public function testApplyPrefix() |
128
|
|
|
{ |
129
|
|
|
$this->object->setPrefix('newPrefix'); |
130
|
|
|
|
131
|
|
|
$key = 'Key'; |
132
|
|
|
$this->assertEquals('newPrefixKey', $this->object->prefixKey($key)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @covers Koch\Cache\AbstractCache::__set |
137
|
|
|
* @covers Koch\Cache\AbstractCache::__isset |
138
|
|
|
* @covers Koch\Cache\AbstractCache::__get |
139
|
|
|
* @covers Koch\Cache\AbstractCache::__unset |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
public function testSet() |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
// set |
144
|
|
|
$this->object->key = 'value'; |
145
|
|
|
// isset |
146
|
|
|
$this->assertTrue(isset($this->object->key)); |
147
|
|
|
// get |
148
|
|
|
$this->assertEquals('value', $this->object->key); |
149
|
|
|
// unset |
150
|
|
|
unset($this->object->key); |
151
|
|
|
$this->assertFalse(isset($this->object->key)); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..