Passed
Push — master ( 353565...a6ecf0 )
by Aimeos
02:33
created

FlyTest::testHasException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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