1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Cache\Tests; |
4
|
|
|
|
5
|
|
|
use Onoi\Cache\MediaWikiCache; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Onoi\Cache\MediaWikiCache |
9
|
|
|
* |
10
|
|
|
* @group onoi-cache |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 1.0 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class MediaWikiCacheTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
private $cache; |
20
|
|
|
|
21
|
|
|
protected function setUp() { |
22
|
|
|
parent::setUp(); |
23
|
|
|
|
24
|
|
|
if ( !class_exists( '\BagOstuff' ) ) { |
25
|
|
|
$this->markTestSkipped( 'BagOstuff interface is not available' ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$this->cache = $this->getMockBuilder( '\BagOstuff' ) |
29
|
|
|
->disableOriginalConstructor() |
30
|
|
|
->getMockForAbstractClass(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testCanConstruct() { |
34
|
|
|
|
35
|
|
|
$this->assertInstanceOf( |
36
|
|
|
'\Onoi\Cache\MediaWikiCache', |
37
|
|
|
new MediaWikiCache( $this->cache ) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testGetName() { |
42
|
|
|
|
43
|
|
|
$instance = new MediaWikiCache( $this->cache ); |
44
|
|
|
|
45
|
|
|
$this->assertInternalType( |
46
|
|
|
'string', |
47
|
|
|
$instance->getName() |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testSave() { |
52
|
|
|
|
53
|
|
|
$this->cache->expects( $this->once() ) |
54
|
|
|
->method( 'set' ) |
55
|
|
|
->with( |
56
|
|
|
$this->equalTo( 'Foo' ), |
57
|
|
|
$this->anything(), |
58
|
|
|
$this->equalTo( 42 ) ); |
59
|
|
|
|
60
|
|
|
$instance = new MediaWikiCache( $this->cache ); |
61
|
|
|
$instance->save( 'Foo', 'Bar', 42 ); |
62
|
|
|
|
63
|
|
|
$this->assertFalse( |
64
|
|
|
$instance->contains( 'Foo' ) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testDelete() { |
69
|
|
|
|
70
|
|
|
$this->cache->expects( $this->once() ) |
71
|
|
|
->method( 'delete' ) |
72
|
|
|
->with( |
73
|
|
|
$this->equalTo( 'Foo' ) ); |
74
|
|
|
|
75
|
|
|
$instance = new MediaWikiCache( $this->cache ); |
76
|
|
|
$instance->delete( 'Foo' ); |
77
|
|
|
|
78
|
|
|
$this->assertFalse( |
79
|
|
|
$instance->contains( 'Foo' ) |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$expected = array( |
83
|
|
|
'inserts' => 0, |
84
|
|
|
'deletes' => 1, |
85
|
|
|
'hits' => 0, |
86
|
|
|
'misses' => 0 |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$this->assertEquals( |
90
|
|
|
$expected, |
91
|
|
|
$instance->getStats() |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testContains() { |
96
|
|
|
|
97
|
|
|
$this->cache->expects( $this->once() ) |
98
|
|
|
->method( 'get' ) |
99
|
|
|
->with( |
100
|
|
|
$this->equalTo( 'Foo' ) ); |
101
|
|
|
|
102
|
|
|
$instance = new MediaWikiCache( $this->cache ); |
103
|
|
|
$instance->contains( 'Foo' ); |
104
|
|
|
|
105
|
|
|
// Internally the access is cached |
106
|
|
|
$this->assertTrue( |
107
|
|
|
$instance->contains( 'Foo' ) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testFetch() { |
112
|
|
|
|
113
|
|
|
$this->cache->expects( $this->once() ) |
114
|
|
|
->method( 'get' ) |
115
|
|
|
->with( |
116
|
|
|
$this->equalTo( 'Foo' ) ) |
117
|
|
|
->will( $this->returnValue( 'Bar' ) ); |
118
|
|
|
|
119
|
|
|
$instance = new MediaWikiCache( $this->cache ); |
120
|
|
|
|
121
|
|
|
$this->assertEquals( |
122
|
|
|
'Bar', |
123
|
|
|
$instance->fetch( 'Foo' ) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
// Internally the access is cached |
127
|
|
|
$this->assertTrue( |
128
|
|
|
$instance->contains( 'Foo' ) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testFetchForFalse() { |
133
|
|
|
|
134
|
|
|
$this->cache->expects( $this->once() ) |
135
|
|
|
->method( 'get' ); |
136
|
|
|
|
137
|
|
|
$instance = new MediaWikiCache( $this->cache ); |
138
|
|
|
|
139
|
|
|
$this->assertFalse( |
140
|
|
|
$instance->fetch( 'Bar' ) |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$expected = array( |
144
|
|
|
'inserts' => 0, |
145
|
|
|
'deletes' => 0, |
146
|
|
|
'hits' => 0, |
147
|
|
|
'misses' => 1 |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
$this->assertEquals( |
151
|
|
|
$expected, |
152
|
|
|
$instance->getStats() |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testGetStats() { |
157
|
|
|
|
158
|
|
|
$this->cache->expects( $this->once() ) |
159
|
|
|
->method( 'get' ) |
160
|
|
|
->with( |
161
|
|
|
$this->equalTo( 'Foo' ) ) |
162
|
|
|
->will( $this->returnValue( 'Bar' ) ); |
163
|
|
|
|
164
|
|
|
$instance = new MediaWikiCache( $this->cache ); |
165
|
|
|
$instance->fetch( 'Foo' ); |
166
|
|
|
|
167
|
|
|
$expected = array( |
168
|
|
|
'inserts' => 0, |
169
|
|
|
'deletes' => 0, |
170
|
|
|
'hits' => 1, |
171
|
|
|
'misses' => 0 |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
$this->assertEquals( |
175
|
|
|
$expected, |
176
|
|
|
$instance->getStats() |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|