testFetchForToReturnNoResultForAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.2
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace Onoi\Cache\Tests;
4
5
use Onoi\Cache\CompositeCache;
6
7
/**
8
 * @covers \Onoi\Cache\CompositeCache
9
 *
10
 * @group onoi-cache
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class CompositeCacheTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testCanConstruct() {
20
21
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
22
			->disableOriginalConstructor()
23
			->getMockForAbstractClass();
24
25
		$this->assertInstanceOf(
26
			'\Onoi\Cache\CompositeCache',
27
			new CompositeCache( array( $cache ) )
28
		);
29
	}
30
31
	public function testConstructForInvalidArrayKeyThrowsException() {
32
33
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
34
			->disableOriginalConstructor()
35
			->getMockForAbstractClass();
36
37
		$this->setExpectedException( 'RuntimeException' );
38
		new CompositeCache( array( 'cache' => $cache ) );
39
	}
40
41
	public function testConstructForInvalidCacheInstanceThrowsException() {
42
43
		$this->setExpectedException( 'RuntimeException' );
44
		new CompositeCache( array( 'Foo' ) );
0 ignored issues
show
Documentation introduced by
array('Foo') is of type array<integer,string,{"0":"string"}>, but the function expects a array<integer,object<Onoi\Cache\Cache>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
	}
46
47
	public function testGetName() {
48
49
		$first = $this->getMockBuilder( '\Onoi\Cache\ZendCache' )
50
			->disableOriginalConstructor()
51
			->getMockForAbstractClass();
52
53
		$second = $this->getMockBuilder( '\Onoi\Cache\DoctrineCache' )
54
			->disableOriginalConstructor()
55
			->getMockForAbstractClass();
56
57
		$instance = new CompositeCache( array( $first, $second ) );
58
59
		$this->assertInternalType(
60
			'string',
61
			$instance->getName()
62
		);
63
	}
64
65
	public function testSave() {
66
67
		$first = $this->getMockBuilder( '\Onoi\Cache\Cache' )
68
			->disableOriginalConstructor()
69
			->getMockForAbstractClass();
70
71
		$first->expects( $this->once() )
72
			->method( 'save' )
73
			->with(
74
				$this->equalTo( 'Foo' ),
75
				$this->anything() );
76
77
		$second = $this->getMockBuilder( '\Onoi\Cache\Cache' )
78
			->disableOriginalConstructor()
79
			->getMockForAbstractClass();
80
81
		$second->expects( $this->once() )
82
			->method( 'save' )
83
			->with(
84
				$this->equalTo( 'Foo' ),
85
				$this->anything() );
86
87
		$instance = new CompositeCache( array( $first, $second ) );
88
		$instance->save( 'Foo', 'Bar' );
89
	}
90
91
	public function testDelete() {
92
93
		$first = $this->getMockBuilder( '\Onoi\Cache\Cache' )
94
			->disableOriginalConstructor()
95
			->getMockForAbstractClass();
96
97
		$first->expects( $this->once() )
98
			->method( 'delete' )
99
			->with(
100
				$this->equalTo( 'Foo' ) );
101
102
		$second = $this->getMockBuilder( '\Onoi\Cache\Cache' )
103
			->disableOriginalConstructor()
104
			->getMockForAbstractClass();
105
106
		$second->expects( $this->once() )
107
			->method( 'delete' )
108
			->with(
109
				$this->equalTo( 'Foo' ) );
110
111
		$instance = new CompositeCache( array( $first, $second ) );
112
		$instance->delete( 'Foo' );
113
	}
114
115
	public function testGetStats() {
116
117
		$first = $this->getMockBuilder( '\Onoi\Cache\Cache' )
118
			->disableOriginalConstructor()
119
			->getMockForAbstractClass();
120
121
		$first->expects( $this->once() )
122
			->method( 'getStats' );
123
124
		$second = $this->getMockBuilder( '\Onoi\Cache\Cache' )
125
			->disableOriginalConstructor()
126
			->getMockForAbstractClass();
127
128
		$second->expects( $this->once() )
129
			->method( 'getStats' );
130
131
		$instance = new CompositeCache( array( $first, $second ) );
132
133
		$this->assertInternalType(
134
			'array',
135
			$instance->getStats( 'Foo' )
0 ignored issues
show
Unused Code introduced by
The call to CompositeCache::getStats() has too many arguments starting with 'Foo'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
136
		);
137
	}
138
139
	public function testContainsIsTrueForFirstCache() {
140
141
		$first = $this->getMockBuilder( '\Onoi\Cache\Cache' )
142
			->disableOriginalConstructor()
143
			->getMockForAbstractClass();
144
145
		$first->expects( $this->once() )
146
			->method( 'contains' )
147
			->with(
148
				$this->equalTo( 'Foo' ) )
149
			->will( $this->returnValue( true ) );
150
151
		$second = $this->getMockBuilder( '\Onoi\Cache\Cache' )
152
			->disableOriginalConstructor()
153
			->getMockForAbstractClass();
154
155
		$second->expects( $this->never() )
156
			->method( 'contains' );
157
158
		$instance = new CompositeCache( array( $first, $second ) );
159
160
		$this->assertTrue(
161
			$instance->contains( 'Foo' )
162
		);
163
	}
164
165
	public function testContainsIsFalseForFirstCache() {
166
167
		$first = $this->getMockBuilder( '\Onoi\Cache\Cache' )
168
			->disableOriginalConstructor()
169
			->getMockForAbstractClass();
170
171
		$first->expects( $this->once() )
172
			->method( 'contains' );
173
174
		$second = $this->getMockBuilder( '\Onoi\Cache\Cache' )
175
			->disableOriginalConstructor()
176
			->getMockForAbstractClass();
177
178
		$second->expects( $this->once() )
179
			->method( 'contains' )
180
			->with(
181
				$this->equalTo( 'Foo' ) )
182
			->will( $this->returnValue( false ) );
183
184
		$instance = new CompositeCache( array( $first, $second ) );
185
186
		$this->assertFalse(
187
			$instance->contains( 'Foo' )
188
		);
189
	}
190
191
	public function testFetchToReturnNoResultForFirstCache() {
192
193
		$first = $this->getMockBuilder( '\Onoi\Cache\Cache' )
194
			->disableOriginalConstructor()
195
			->getMockForAbstractClass();
196
197
		$first->expects( $this->once() )
198
			->method( 'contains' );
199
200
		$first->expects( $this->once() )
201
			->method( 'save' )
202
			->with(
203
				$this->equalTo( 'Foo' ),
204
				$this->anything() );
205
206
		$second = $this->getMockBuilder( '\Onoi\Cache\Cache' )
207
			->disableOriginalConstructor()
208
			->getMockForAbstractClass();
209
210
		$second->expects( $this->once() )
211
			->method( 'contains' )
212
			->will( $this->returnValue( true ) );
213
214
		$second->expects( $this->once() )
215
			->method( 'fetch' )
216
			->with(
217
				$this->equalTo( 'Foo' ) )
218
			->will( $this->returnValue( 'Bar' ) );
219
220
		$instance = new CompositeCache( array( $first, $second ) );
221
222
		$this->assertEquals(
223
			'Bar',
224
			$instance->fetch( 'Foo' )
225
		);
226
	}
227
228
	public function testFetchForToReturnNoResultForAll() {
229
230
		$first = $this->getMockBuilder( '\Onoi\Cache\Cache' )
231
			->disableOriginalConstructor()
232
			->getMockForAbstractClass();
233
234
		$first->expects( $this->once() )
235
			->method( 'contains' );
236
237
		$second = $this->getMockBuilder( '\Onoi\Cache\Cache' )
238
			->disableOriginalConstructor()
239
			->getMockForAbstractClass();
240
241
		$second->expects( $this->once() )
242
			->method( 'contains' );
243
244
		$instance = new CompositeCache( array( $first, $second ) );
245
246
		$this->assertFalse(
247
			$instance->fetch( 'Foo' )
248
		);
249
	}
250
251
}
252