Passed
Push — master ( 7dfc38...3cd1a1 )
by Aimeos
13:27 queued 06:48
created

FlyTest::testConstructException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 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( !class_exists( '\\League\\Flysystem\\Filesystem' ) ) {
15
			$this->markTestSkipped( 'Install Flysystem first' );
16
		}
17
18
		$this->object = $this->getMockBuilder( '\\Aimeos\\Base\\Filesystem\\FlyLocal' )
19
			->setConstructorArgs( array( array( 'adapter' => 'FlyLocal' ) ) )
20
			->setMethods( array( 'getProvider' ) )
21
			->getMock();
22
23
		$this->mock = $this->getMockBuilder( '\\League\\Flysystem\\Filesystem' )
24
			->disableOriginalConstructor()
25
			->getMock();
26
27
		$this->object->expects( $this->any() )->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 testConstructException()
39
	{
40
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
41
		new \Aimeos\Base\Filesystem\FlyLocal( ['tempdir' => '/test'] );
42
	}
43
44
45
	public function testIsdir()
46
	{
47
		$listStub = $this->getMockBuilder( '\\League\\Flysystem\\DirectoryListing' )
48
			->setConstructorArgs( [[new \League\Flysystem\DirectoryAttributes( 'test' )]] )
49
			->setMethods( ['filter'] )
50
			->getMock();
51
52
		$this->mock->expects( $this->once() )->method( 'listContents' )
53
			->will( $this->returnValue( $listStub ) );
54
55
		$this->assertTrue( $this->object->isdir( 'test' ) );
56
	}
57
58
59
	public function testIsdirFalse()
60
	{
61
		$listStub = $this->getMockBuilder( '\\League\\Flysystem\\DirectoryListing' )
62
			->setConstructorArgs( [[new \League\Flysystem\FileAttributes( 'test' )]] )
63
			->setMethods( ['filter'] )
64
			->getMock();
65
66
		$this->mock->expects( $this->once() )->method( 'listContents' )
67
			->will( $this->returnValue( $listStub ) );
68
69
		$this->assertFalse( $this->object->isdir( 'test' ) );
70
	}
71
72
73
	public function testIsdirException()
74
	{
75
		$this->object->expects( $this->any() )->method( 'getProvider' )
76
			->will( $this->throwException( new \RuntimeException() ) );
77
78
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
79
		$this->assertTrue( $this->object->isdir( 'test' ) );
80
	}
81
82
83
	public function testMkdir()
84
	{
85
		$this->mock->expects( $this->once() )->method( 'createDirectory' );
86
87
		$object = $this->object->mkdir( 'test' );
88
89
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\DirIface::class, $object );
90
	}
91
92
93
	public function testMkdirException()
94
	{
95
		$this->mock->expects( $this->once() )->method( 'createDirectory' )
96
			->will( $this->throwException( new \RuntimeException() ) );
97
98
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
99
		$this->object->mkdir( 'test' );
100
	}
101
102
103
	public function testRmdir()
104
	{
105
		$this->mock->expects( $this->once() )->method( 'deleteDirectory' );
106
107
		$object = $this->object->rmdir( 'test' );
108
109
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\DirIface::class, $object );
110
	}
111
112
113
	public function testRmdirException()
114
	{
115
		$this->mock->expects( $this->once() )->method( 'deleteDirectory' )
116
			->will( $this->throwException( new \RuntimeException() ) );
117
118
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
119
		$this->object->rmdir( 'test' );
120
	}
121
122
123
	public function testScan()
124
	{
125
		$listStub = $this->getMockBuilder( '\\League\\Flysystem\\DirectoryListing' )
126
			->setConstructorArgs( [[new \League\Flysystem\FileAttributes( 'test' )]] )
127
			->setMethods( ['filter'] )
128
			->getMock();
129
130
		$this->mock->expects( $this->once() )->method( 'listContents' )
131
			->will( $this->returnValue( $listStub ) );
132
133
		$this->assertEquals( ['test'], $this->object->scan() );
134
	}
135
136
137
	public function testScanException()
138
	{
139
		$this->object->expects( $this->any() )->method( 'getProvider' )
140
			->will( $this->throwException( new \RuntimeException() ) );
141
142
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
143
		$this->assertTrue( $this->object->scan( 'test' ) );
144
	}
145
146
147
	public function testSize()
148
	{
149
		$this->mock->expects( $this->once() )->method( 'fileSize' )
150
			->will( $this->returnValue( 4 ) );
151
152
		$this->assertEquals( 4, $this->object->size( 'test' ) );
153
	}
154
155
156
	public function testSizeException()
157
	{
158
		$this->mock->expects( $this->once() )->method( 'fileSize' )
159
		->will( $this->throwException( new \RuntimeException() ) );
160
161
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
162
		$this->object->size( 'test' );
163
	}
164
165
166
	public function testTime()
167
	{
168
		$this->mock->expects( $this->once() )->method( 'lastModified' )
169
			->will( $this->returnValue( 4 ) );
170
171
		$this->assertEquals( 4, $this->object->time( 'test' ) );
172
	}
173
174
175
	public function testTimeException()
176
	{
177
		$this->mock->expects( $this->once() )->method( 'lastModified' )
178
			->will( $this->throwException( new \RuntimeException() ) );
179
180
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
181
		$this->object->time( 'test' );
182
	}
183
184
185
	public function testRm()
186
	{
187
		$this->mock->expects( $this->once() )->method( 'delete' );
188
189
		$object = $this->object->rm( 'test' );
190
191
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
192
	}
193
194
195
	public function testRmException()
196
	{
197
		$this->mock->expects( $this->once() )->method( 'delete' )
198
			->will( $this->throwException( new \RuntimeException() ) );
199
200
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
201
		$this->object->rm( 'test' );
202
	}
203
204
205
	public function testHas()
206
	{
207
		$this->mock->expects( $this->once() )->method( 'fileExists' )
208
			->will( $this->returnValue( true ) );
209
210
		$result = $this->object->has( 'test' );
211
212
		$this->assertTrue( $result );
213
	}
214
215
216
	public function testHasFalse()
217
	{
218
		$this->mock->expects( $this->once() )->method( 'fileExists' )
219
			->will( $this->returnValue( false ) );
220
221
		if( method_exists( $this->mock, 'directoryExists' ) )
222
		{
223
			$this->mock->expects( $this->once() )->method( 'directoryExists' )
224
				->will( $this->returnValue( false ) );
225
		}
226
227
		$result = $this->object->has( 'test' );
228
229
		$this->assertFalse( $result );
230
	}
231
232
233
	public function testHasException()
234
	{
235
		$this->mock->expects( $this->once() )->method( 'fileExists' )
236
			->will( $this->throwException( new \RuntimeException() ) );
237
238
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
239
		$this->object->has( 'test' );
240
	}
241
242
243
	public function testRead()
244
	{
245
		$this->mock->expects( $this->once() )->method( 'read' )
246
			->will( $this->returnValue( 'value' ) );
247
248
		$this->assertEquals( 'value', $this->object->read( 'test' ) );
249
	}
250
251
252
	public function testReadException()
253
	{
254
		$this->mock->expects( $this->once() )->method( 'read' )
255
			->will( $this->throwException( new \RuntimeException() ) );
256
257
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
258
		$this->object->read( 'test' );
259
	}
260
261
262
	public function testReadf()
263
	{
264
		$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'flytest';
265
		file_put_contents( $file, 'test' );
266
267
		$this->mock->expects( $this->once() )->method( 'readStream' )
268
			->will( $this->returnValue( fopen( $file, 'r' ) ) );
269
270
		$result = $this->object->readf( 'file' );
271
272
		$this->assertEquals( 'test', file_get_contents( $result ) );
273
274
		unlink( $result );
275
		unlink( $file );
276
	}
277
278
279
	public function testReads()
280
	{
281
		$this->mock->expects( $this->once() )->method( 'readStream' )
282
			->will( $this->returnValue( 1 ) );
283
284
		$this->assertEquals( 1, $this->object->reads( 'test' ) );
285
	}
286
287
288
	public function testReadsException()
289
	{
290
		$this->mock->expects( $this->once() )->method( 'readStream' )
291
			->will( $this->throwException( new \RuntimeException() ) );
292
293
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
294
		$this->object->reads( 'test' );
295
	}
296
297
298
	public function testWrite()
299
	{
300
		$this->mock->expects( $this->once() )->method( 'write' );
301
302
		$object = $this->object->write( 'test', 'value' );
303
304
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
305
	}
306
307
308
	public function testWriteException()
309
	{
310
		$this->mock->expects( $this->once() )->method( 'write' )
311
			->will( $this->throwException( new \RuntimeException() ) );
312
313
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
314
		$this->object->write( 'test', 'value' );
315
	}
316
317
318
	public function testWritef()
319
	{
320
		$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'flytest';
321
		file_put_contents( $file, 'test' );
322
323
		$this->mock->expects( $this->once() )->method( 'writeStream' );
324
325
		$object = $this->object->writef( 'file', $file );
326
327
		unlink( $file );
328
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
329
	}
330
331
332
	public function testWritefException()
333
	{
334
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
335
		$this->object->writef( 'file', '/test' );
336
	}
337
338
339
	public function testWrites()
340
	{
341
		$this->mock->expects( $this->once() )->method( 'writeStream' );
342
343
		$object = $this->object->writes( 'test', 1 );
344
345
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
346
	}
347
348
349
	public function testWritesException()
350
	{
351
		$this->mock->expects( $this->once() )->method( 'writeStream' )
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( 'move' )
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( 'move' )
373
			->will( $this->throwException( new \RuntimeException() ) );
374
375
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
376
		$this->object->move( 'file1', 'file2' );
377
	}
378
379
380
	public function testCopy()
381
	{
382
		$this->mock->expects( $this->once() )->method( 'copy' );
383
384
		$object = $this->object->copy( 'file1', 'file2' );
385
386
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
387
	}
388
389
390
	public function testCopyException()
391
	{
392
		$this->mock->expects( $this->once() )->method( 'copy' )
393
			->will( $this->throwException( new \RuntimeException() ) );
394
395
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
396
		$this->object->copy( 'file1', 'file2' );
397
	}
398
}
399