Passed
Push — master ( 894e98...cda682 )
by Aimeos
18:42 queued 10:10
created

FlyTest::testReadsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Aimeos\Base\Filesystem;
4
5
6
class FlyTest extends \PHPUnit\Framework\TestCase
7
{
8
	private $mock;
9
	private $object;
10
11
12
	protected function setUp() : void
13
	{
14
		if( !interface_exists( '\\League\\Flysystem\\FilesystemInterface' ) ) {
15
			$this->markTestSkipped( 'Install Flysystem first' );
16
		}
17
18
		$this->object = $this->getMockBuilder( '\\Aimeos\\Base\\Filesystem\\FlyNone' )
19
			->setConstructorArgs( array( array( 'adapter' => 'FlyNone' ) ) )
20
			->setMethods( array( 'getProvider' ) )
21
			->getMock();
22
23
		$this->mock = $this->getMockBuilder( '\\League\\Flysystem\\FilesystemInterface' )
24
			->disableOriginalConstructor()
25
			->getMock();
26
27
		$this->object->expects( $this->once() )->method( 'getProvider' )
28
			->will( $this->returnValue( $this->mock ) );
29
	}
30
31
32
	protected function tearDown() : void
33
	{
34
		unset( $this->object, $this->mock );
35
	}
36
37
38
	public function testIsdir()
39
	{
40
		$this->mock->expects( $this->once() )->method( 'getMetadata' )
41
			->will( $this->returnValue( array( 'type' => 'dir' ) ) );
42
43
		$this->assertTrue( $this->object->isdir( 'test' ) );
44
	}
45
46
47
	public function testIsdirFalse()
48
	{
49
		$this->mock->expects( $this->once() )->method( 'getMetadata' )
50
			->will( $this->returnValue( array( 'type' => 'file' ) ) );
51
52
		$this->assertFalse( $this->object->isdir( 'test' ) );
53
	}
54
55
56
	public function testMkdir()
57
	{
58
		$this->mock->expects( $this->once() )->method( 'createDir' )
59
			->will( $this->returnValue( true ) );
60
61
		$object = $this->object->mkdir( 'test' );
62
63
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\DirIface::class, $object );
64
	}
65
66
67
	public function testMkdirException()
68
	{
69
		$this->mock->expects( $this->once() )->method( 'createDir' )
70
			->will( $this->returnValue( false ) );
71
72
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
73
		$this->object->mkdir( 'test' );
74
	}
75
76
77
	public function testRmdir()
78
	{
79
		$this->mock->expects( $this->once() )->method( 'deleteDir' )
80
			->will( $this->returnValue( true ) );
81
82
		$object = $this->object->rmdir( 'test' );
83
84
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\DirIface::class, $object );
85
	}
86
87
88
	public function testRmdirException()
89
	{
90
		$this->mock->expects( $this->once() )->method( 'deleteDir' )
91
			->will( $this->returnValue( false ) );
92
93
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
94
		$this->object->rmdir( 'test' );
95
	}
96
97
98
	public function testScan()
99
	{
100
		$this->mock->expects( $this->once() )->method( 'listContents' )
101
			->will( $this->returnValue( array( array( 'basename' => 'test' ) ) ) );
102
103
		$this->assertEquals( array( 'test' ), $this->object->scan() );
104
	}
105
106
107
	public function testSize()
108
	{
109
		$this->mock->expects( $this->once() )->method( 'getSize' )
110
			->will( $this->returnValue( 4 ) );
111
112
		$this->assertEquals( 4, $this->object->size( 'test' ) );
113
	}
114
115
116
	public function testSizeException()
117
	{
118
		$this->mock->expects( $this->once() )->method( 'getSize' )
119
			->will( $this->returnValue( false ) );
120
121
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
122
		$this->object->size( 'test' );
123
	}
124
125
126
	public function testSizeException2()
127
	{
128
		$this->mock->expects( $this->once() )->method( 'getSize' )
129
		->will( $this->throwException( new \RuntimeException() ) );
130
131
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
132
		$this->object->size( 'test' );
133
	}
134
135
136
	public function testTime()
137
	{
138
		$this->mock->expects( $this->once() )->method( 'getTimestamp' )
139
			->will( $this->returnValue( 4 ) );
140
141
		$this->assertEquals( 4, $this->object->time( 'test' ) );
142
	}
143
144
145
	public function testTimeException()
146
	{
147
		$this->mock->expects( $this->once() )->method( 'getTimestamp' )
148
			->will( $this->returnValue( false ) );
149
150
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
151
		$this->object->time( 'test' );
152
	}
153
154
155
	public function testTimeException2()
156
	{
157
		$this->mock->expects( $this->once() )->method( 'getTimestamp' )
158
			->will( $this->throwException( new \RuntimeException() ) );
159
160
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
161
		$this->object->time( 'test' );
162
	}
163
164
165
	public function testRm()
166
	{
167
		$this->mock->expects( $this->once() )->method( 'delete' )
168
		->will( $this->returnValue( 4 ) );
169
170
		$object = $this->object->rm( 'test' );
171
172
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
173
	}
174
175
176
	public function testRmException()
177
	{
178
		$this->mock->expects( $this->once() )->method( 'delete' )
179
		->will( $this->throwException( new \RuntimeException() ) );
180
181
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
182
		$this->object->rm( 'test' );
183
	}
184
185
186
	public function testHas()
187
	{
188
		$this->mock->expects( $this->once() )->method( 'has' )
189
			->will( $this->returnValue( true ) );
190
191
		$result = $this->object->has( 'test' );
192
193
		$this->assertTrue( $result );
194
	}
195
196
197
	public function testHasFalse()
198
	{
199
		$this->mock->expects( $this->once() )->method( 'has' )
200
			->will( $this->returnValue( false ) );
201
202
		$result = $this->object->has( 'test' );
203
204
		$this->assertFalse( $result );
205
	}
206
207
208
	public function testRead()
209
	{
210
		$this->mock->expects( $this->once() )->method( 'read' )
211
			->will( $this->returnValue( 'value' ) );
212
213
		$this->assertEquals( 'value', $this->object->read( 'test' ) );
214
	}
215
216
217
	public function testReadException()
218
	{
219
		$this->mock->expects( $this->once() )->method( 'read' )
220
			->will( $this->returnValue( false ) );
221
222
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
223
		$this->object->read( 'test' );
224
	}
225
226
227
	public function testReadException2()
228
	{
229
		$this->mock->expects( $this->once() )->method( 'read' )
230
			->will( $this->throwException( new \RuntimeException() ) );
231
232
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
233
		$this->object->read( 'test' );
234
	}
235
236
237
	public function testReadf()
238
	{
239
		$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'flytest';
240
		file_put_contents( $file, 'test' );
241
242
		$this->mock->expects( $this->once() )->method( 'readStream' )
243
			->will( $this->returnValue( fopen( $file, 'r' ) ) );
244
245
		$result = $this->object->readf( 'file' );
246
247
		$this->assertEquals( 'test', file_get_contents( $result ) );
248
249
		unlink( $result );
250
		unlink( $file );
251
	}
252
253
254
	public function testReads()
255
	{
256
		$this->mock->expects( $this->once() )->method( 'readStream' )
257
			->will( $this->returnValue( 1 ) );
258
259
		$this->assertEquals( 1, $this->object->reads( 'test' ) );
260
	}
261
262
263
	public function testReadsException()
264
	{
265
		$this->mock->expects( $this->once() )->method( 'readStream' )
266
			->will( $this->returnValue( false ) );
267
268
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
269
		$this->object->reads( 'test' );
270
	}
271
272
273
	public function testReadsException2()
274
	{
275
		$this->mock->expects( $this->once() )->method( 'readStream' )
276
			->will( $this->throwException( new \RuntimeException() ) );
277
278
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
279
		$this->object->reads( 'test' );
280
	}
281
282
283
	public function testWrite()
284
	{
285
		$this->mock->expects( $this->once() )->method( 'put' )
286
			->will( $this->returnValue( true ) );
287
288
		$object = $this->object->write( 'test', 'value' );
289
290
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
291
	}
292
293
294
	public function testWriteException()
295
	{
296
		$this->mock->expects( $this->once() )->method( 'put' )
297
			->will( $this->returnValue( false ) );
298
299
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
300
		$this->object->write( 'test', 'value' );
301
	}
302
303
304
	public function testWriteException2()
305
	{
306
		$this->mock->expects( $this->once() )->method( 'put' )
307
			->will( $this->throwException( new \RuntimeException() ) );
308
309
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
310
		$this->object->write( 'test', 'value' );
311
	}
312
313
314
	public function testWritef()
315
	{
316
		$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'flytest';
317
		file_put_contents( $file, 'test' );
318
319
		$this->mock->expects( $this->once() )->method( 'putStream' );
320
321
		$object = $this->object->writef( 'file', $file );
322
323
		unlink( $file );
324
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
325
	}
326
327
328
	public function testWrites()
329
	{
330
		$this->mock->expects( $this->once() )->method( 'putStream' )
331
			->will( $this->returnValue( true ) );
332
333
		$object = $this->object->writes( 'test', 1 );
334
335
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
336
	}
337
338
339
	public function testWritesException()
340
	{
341
		$this->mock->expects( $this->once() )->method( 'putStream' )
342
			->will( $this->returnValue( false ) );
343
344
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
345
		$this->object->writes( 'test', 2 );
346
	}
347
348
349
	public function testWritesException2()
350
	{
351
		$this->mock->expects( $this->once() )->method( 'putStream' )
352
			->will( $this->throwException( new \RuntimeException() ) );
353
354
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
355
		$this->object->writes( 'test', null );
356
	}
357
358
359
	public function testMove()
360
	{
361
		$this->mock->expects( $this->once() )->method( 'rename' )
362
			->will( $this->returnValue( true ) );
363
364
		$object = $this->object->move( 'file1', 'file2' );
365
366
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
367
	}
368
369
370
	public function testMoveException()
371
	{
372
		$this->mock->expects( $this->once() )->method( 'rename' )
373
			->will( $this->returnValue( false ) );
374
375
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
376
		$this->object->move( 'file1', 'file2' );
377
	}
378
379
380
	public function testMoveException2()
381
	{
382
		$this->mock->expects( $this->once() )->method( 'rename' )
383
			->will( $this->throwException( new \RuntimeException() ) );
384
385
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
386
		$this->object->move( 'file1', 'file2' );
387
	}
388
389
390
	public function testCopy()
391
	{
392
		$this->mock->expects( $this->once() )->method( 'copy' )
393
		->will( $this->returnValue( true ) );
394
395
		$object = $this->object->copy( 'file1', 'file2' );
396
397
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
398
	}
399
400
401
	public function testCopyException()
402
	{
403
		$this->mock->expects( $this->once() )->method( 'copy' )
404
		->will( $this->returnValue( false ) );
405
406
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
407
		$this->object->copy( 'file1', 'file2' );
408
	}
409
410
411
	public function testCopyException2()
412
	{
413
		$this->mock->expects( $this->once() )->method( 'copy' )
414
		->will( $this->throwException( new \RuntimeException() ) );
415
416
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
417
		$this->object->copy( 'file1', 'file2' );
418
	}
419
}
420