BlobStoreTest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 228
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 228
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testDropDoesNothing() 0 11 1
A testReadContainerForKnownId() 0 22 1
A testSaveContainer() 0 16 1
A setUp() 0 6 1
A testCanConstruct() 0 7 1
A testInvalidNamespaceInConstructorThrowsException() 0 9 1
A testInvalidKeyForContainerThrowsException() 0 10 1
A testCanUse() 0 14 1
A testReadContainerForUnknownId() 0 16 1
A testReadContainerForKnownIdToAlwaysReturnArrayType() 0 21 1
A testGetStats() 0 9 1
A testSaveContainerWithExpiry() 0 18 1
A testTransferExpiry() 0 12 1
A testDeleteContainerForSpecificId() 0 10 1
B testDeleteMembersOfLinkedListAsWell() 0 27 1
1
<?php
2
3
namespace Onoi\BlobStore\Tests;
4
5
use Onoi\BlobStore\BlobStore;
6
use Onoi\BlobStore\Container;
7
8
/**
9
 * @covers \Onoi\BlobStore\BlobStore
10
 *
11
 * @group onoi-blobstore
12
 *
13
 * @license GNU GPL v2+
14
 * @since 1.0
15
 *
16
 * @author mwjames
17
 */
18
class BlobStoreTest extends \PHPUnit_Framework_TestCase {
19
20
	private $cache;
21
22
	protected function setUp() {
23
24
		$this->cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
25
			->disableOriginalConstructor()
26
			->getMockForAbstractClass();
27
	}
28
29
	public function testCanConstruct() {
30
31
		$this->assertInstanceOf(
32
			'\Onoi\BlobStore\BlobStore',
33
			new BlobStore( 'Foo', $this->cache )
34
		);
35
	}
36
37
	public function testInvalidNamespaceInConstructorThrowsException() {
38
39
		$this->setExpectedException( 'InvalidArgumentException' );
40
41
		new BlobStore(
42
			array(),
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

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...
43
			$this->cache
44
		);
45
	}
46
47
	public function testInvalidKeyForContainerThrowsException() {
48
49
		$instance = new BlobStore(
50
			'Foo',
51
			$this->cache
52
		);
53
54
		$this->setExpectedException( 'InvalidArgumentException' );
55
		$instance->read( array() );
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

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...
56
	}
57
58
	public function testCanUse() {
59
60
		$instance = new BlobStore( 'Foo', $this->cache );
61
62
		$this->assertTrue(
63
			$instance->canUse()
64
		);
65
66
		$instance->setUsageState( false );
67
68
		$this->assertFalse(
69
			$instance->canUse()
70
		);
71
	}
72
73
	public function testReadContainerForUnknownId() {
74
75
		$container = new Container( 'blobstore:Foo:bar', array() );
76
77
		$instance = new BlobStore( 'Foo', $this->cache );
78
79
		$this->assertInstanceOf(
80
			'\Onoi\BlobStore\Container',
81
			$instance->read( 'bar' )
82
		);
83
84
		$this->assertEquals(
85
			$container,
86
			$instance->read( 'bar' )
87
		);
88
	}
89
90
	public function testReadContainerForKnownId() {
91
92
		$container = new Container( 'coffee:Foo:bar', array() );
93
94
		$this->cache->expects( $this->once() )
95
			->method( 'contains' )
96
			->with(
97
				$this->equalTo( 'coffee:Foo:bar' ) )
98
			->will( $this->returnValue( true ) );
99
100
		$this->cache->expects( $this->once() )
101
			->method( 'fetch' )
102
			->will( $this->returnValue( serialize( array() ) ) );
103
104
		$instance = new BlobStore( 'Foo', $this->cache );
105
		$instance->setNamespacePrefix( 'coffee' );
106
107
		$this->assertEquals(
108
			$container,
109
			$instance->read( 'bar' )
110
		);
111
	}
112
113
	public function testReadContainerForKnownIdToAlwaysReturnArrayType() {
114
115
		$container = new Container( 'blobstore:Foo:bar', array( false ) );
116
117
		$this->cache->expects( $this->once() )
118
			->method( 'contains' )
119
			->with(
120
				$this->equalTo( 'blobstore:Foo:bar' ) )
121
			->will( $this->returnValue( true ) );
122
123
		$this->cache->expects( $this->once() )
124
			->method( 'fetch' )
125
			->will( $this->returnValue( serialize( false ) ) );
126
127
		$instance = new BlobStore( 'Foo', $this->cache );
128
129
		$this->assertEquals(
130
			$container,
131
			$instance->read( 'bar' )
132
		);
133
	}
134
135
	public function testGetStats() {
136
137
		$this->cache->expects( $this->once() )
138
			->method( 'getStats' )
139
			->will( $this->returnValue( array() ) );
140
141
		$instance = new BlobStore( 'Foo', $this->cache );
142
		$instance->getStats();
143
	}
144
145
	public function testSaveContainer() {
146
147
		$container = array( 'Foobar', new \stdClass, array() );
148
149
		$this->cache->expects( $this->once() )
150
			->method( 'save' )
151
			->with(
152
				$this->equalTo( 'Foo:bar' ),
153
				$this->equalTo( serialize( $container ) ),
154
				$this->equalTo( 0 ) );
155
156
		$container = new Container( 'Foo:bar', $container );
157
158
		$instance = new BlobStore( 'Foo', $this->cache );
159
		$instance->save( $container );
160
	}
161
162
	public function testSaveContainerWithExpiry() {
163
164
		$container = array( 'Foobar', new \stdClass, array() );
165
166
		$this->cache->expects( $this->once() )
167
			->method( 'save' )
168
			->with(
169
				$this->equalTo( 'Foo:bar' ),
170
				$this->anything(),
171
				$this->equalTo( 42 ) );
172
173
		$container = new Container( 'Foo:bar', $container );
174
		$container->setExpiryInSeconds( 42 );
175
176
		$instance = new BlobStore( 'Foo', $this->cache );
177
178
		$instance->save( $container );
179
	}
180
181
	public function testTransferExpiry() {
182
183
		$instance = new BlobStore( 'Foo', $this->cache );
184
		$instance->setExpiryInSeconds( 42 );
185
186
		$container = $instance->read( 'Bar' );
187
188
		$this->assertEquals(
189
			42,
190
			$container->getExpiry()
191
		);
192
	}
193
194
	public function testDeleteContainerForSpecificId() {
195
196
		$this->cache->expects( $this->once() )
197
			->method( 'delete' )
198
			->with(
199
				$this->equalTo( 'blobstore:Foo:bar' ) );
200
201
		$instance = new BlobStore( 'Foo', $this->cache );
202
		$instance->delete( 'bar' );
203
	}
204
205
	public function testDropDoesNothing() {
206
207
		$this->cache->expects( $this->never() )
208
			->method( 'fetch' );
209
210
		$this->cache->expects( $this->never() )
211
			->method( 'delete' );
212
213
		$instance = new BlobStore( 'Foo', $this->cache );
214
		$instance->drop();
215
	}
216
217
	public function testDeleteMembersOfLinkedListAsWell() {
218
219
		$linkedContainer = serialize(
220
			array( '@linkedList' => array( 'a42b' => true ) )
221
		);
222
223
		$this->cache->expects( $this->once() )
224
			->method( 'contains' )
225
			->with( $this->equalTo( 'blobstore:Foo:Bar' ) )
226
			->will( $this->returnValue( true ) );
227
228
		$this->cache->expects( $this->once() )
229
			->method( 'fetch' )
230
			->will( $this->returnValue( $linkedContainer ) );
231
232
		$this->cache->expects( $this->at( 2 ) )
233
			->method( 'delete' )
234
			->with(	$this->equalTo( 'blobstore:Foo:a42b' ) );
235
236
		$this->cache->expects( $this->at( 3 ) )
237
			->method( 'delete' )
238
			->with( $this->equalTo( 'blobstore:Foo:Bar' ) );
239
240
		$instance = new BlobStore( 'Foo', $this->cache );
241
242
		$instance->delete( 'Bar' );
243
	}
244
245
}
246