ZendCacheTest::testSave()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
namespace Onoi\Cache\Tests;
4
5
use Onoi\Cache\ZendCache;
6
7
/**
8
 * @covers \Onoi\Cache\ZendCache
9
 *
10
 * @group onoi-cache
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.1
14
 *
15
 * @author mwjames
16
 */
17
class ZendCacheTest extends \PHPUnit_Framework_TestCase {
18
19
	private $cache;
20
21
	protected function setUp() {
22
		parent::setUp();
23
24
		if ( !interface_exists( '\Zend\Cache\Storage\StorageInterface' ) ) {
25
			$this->markTestSkipped( 'Zend StorageInterface is not available' );
26
		}
27
28
		$this->cache = $this->getMockBuilder( '\Zend\Cache\Storage\StorageInterface' )
29
			->disableOriginalConstructor()
30
			->getMockForAbstractClass();
31
	}
32
33
	public function testCanConstruct() {
34
35
		$this->assertInstanceOf(
36
			'\Onoi\Cache\ZendCache',
37
		 new ZendCache( $this->cache )
38
		);
39
	}
40
41
	public function testGetName() {
42
43
		$instance = new ZendCache( $this->cache );
44
45
		$this->assertInternalType(
46
			'string',
47
			$instance->getName()
48
		);
49
	}
50
51
	public function testSave() {
52
53
		$adapterOptions = $this->getMockBuilder( '\Zend\Cache\Storage\Adapter\AdapterOptions' )
54
			->disableOriginalConstructor()
55
			->getMock();
56
57
		$this->cache->expects( $this->any() )
58
			->method( 'getOptions' )
59
			->will( $this->returnValue( $adapterOptions ) );
60
61
		$this->cache->expects( $this->once() )
62
			->method( 'setItem' )
63
			->with(
64
				$this->equalTo( 'Foo' ),
65
				$this->equalTo( 'Bar' ) );
66
67
		$instance = new ZendCache( $this->cache );
68
		$instance->save( 'Foo', 'Bar', 42 );
69
70
		$expected = array(
71
			'inserts' => 1,
72
			'deletes' => 0,
73
			'hits'    => 0,
74
			'misses'  => 0
75
		);
76
77
		$this->assertEquals(
78
			$expected,
79
			$instance->getStats()
80
		);
81
	}
82
83
	public function testSaveBySetItemThrowingException() {
84
85
		$adapterOptions = $this->getMockBuilder( '\Zend\Cache\Storage\Adapter\AdapterOptions' )
86
			->disableOriginalConstructor()
87
			->getMock();
88
89
		$this->cache->expects( $this->any() )
90
			->method( 'getOptions' )
91
			->will( $this->returnValue( $adapterOptions ) );
92
93
		$this->cache->expects( $this->once() )
94
			->method( 'setItem' )
95
			->with(
96
				$this->equalTo( 'Foo' ),
97
				$this->equalTo( 'Bar' ) )
98
			->will( $this->throwException( new \Exception() ) );
99
100
		$instance = new ZendCache( $this->cache );
101
		$instance->save( 'Foo', 'Bar', 42 );
102
103
		$expected = array(
104
			'inserts' => 0,
105
			'deletes' => 0,
106
			'hits'    => 0,
107
			'misses'  => 0
108
		);
109
110
		$this->assertEquals(
111
			$expected,
112
			$instance->getStats()
113
		);
114
	}
115
116
	public function testDelete() {
117
118
		$this->cache->expects( $this->once() )
119
			->method( 'removeItem' )
120
			->with(
121
				$this->equalTo( 'Foo' ) );
122
123
		$instance = new ZendCache( $this->cache );
124
		$instance->delete( 'Foo' );
125
	}
126
127
	public function testContains() {
128
129
		$this->cache->expects( $this->once() )
130
			->method( 'hasItem' )
131
			->with(
132
				$this->equalTo( 'Foo' ) );
133
134
		$instance = new ZendCache( $this->cache );
135
		$instance->contains( 'Foo' );
136
	}
137
138
	public function testFetch() {
139
140
		$this->cache->expects( $this->once() )
141
			->method( 'hasItem' )
142
			->will( $this->returnValue( true ) );
143
144
		$this->cache->expects( $this->once() )
145
			->method( 'getItem' )
146
			->with(
147
				$this->equalTo( 'Foo' ) )
148
			->will( $this->returnValue( 'Bar' ) );
149
150
		$instance = new ZendCache( $this->cache );
151
152
		$this->assertEquals(
153
			'Bar',
154
			$instance->fetch( 'Foo' )
155
		);
156
	}
157
158
	public function testFetchForNonExistingItem() {
159
160
		$this->cache->expects( $this->once() )
161
			->method( 'hasItem' )
162
			->will( $this->returnValue( false ) );
163
164
		$this->cache->expects( $this->never() )
165
			->method( 'getItem' );
166
167
		$instance = new ZendCache( $this->cache );
168
169
		$this->assertFalse(
170
			$instance->fetch( 'Foo' )
171
		);
172
173
		$expected = array(
174
			'inserts' => 0,
175
			'deletes' => 0,
176
			'hits'    => 0,
177
			'misses'  => 1
178
		);
179
180
		$this->assertEquals(
181
			$expected,
182
			$instance->getStats()
183
		);
184
	}
185
186
	public function testGetStats() {
187
188
		$instance = new ZendCache( $this->cache );
189
190
		$this->assertInternalType(
191
			'array',
192
			$instance->getStats()
193
		);
194
	}
195
196
}
197