Passed
Push — master ( ff8f2e...aaaf39 )
by Aimeos
02:27
created

FlyTest::testSizeException2()   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( !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->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
		$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( 'has' )
181
			->will( $this->returnValue( true ) );
182
183
		$result = $this->object->has( 'test' );
184
185
		$this->assertTrue( $result );
186
	}
187
188
189
	public function testHasFalse()
190
	{
191
		$this->mock->expects( $this->once() )->method( 'has' )
192
			->will( $this->returnValue( false ) );
193
194
		$result = $this->object->has( 'test' );
195
196
		$this->assertFalse( $result );
197
	}
198
199
200
	public function testRead()
201
	{
202
		$this->mock->expects( $this->once() )->method( 'read' )
203
			->will( $this->returnValue( 'value' ) );
204
205
		$this->assertEquals( 'value', $this->object->read( 'test' ) );
206
	}
207
208
209
	public function testReadException()
210
	{
211
		$this->mock->expects( $this->once() )->method( 'read' )
212
			->will( $this->throwException( new \RuntimeException() ) );
213
214
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
215
		$this->object->read( 'test' );
216
	}
217
218
219
	public function testReadf()
220
	{
221
		$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'flytest';
222
		file_put_contents( $file, 'test' );
223
224
		$this->mock->expects( $this->once() )->method( 'readStream' )
225
			->will( $this->returnValue( fopen( $file, 'r' ) ) );
226
227
		$result = $this->object->readf( 'file' );
228
229
		$this->assertEquals( 'test', file_get_contents( $result ) );
230
231
		unlink( $result );
232
		unlink( $file );
233
	}
234
235
236
	public function testReads()
237
	{
238
		$this->mock->expects( $this->once() )->method( 'readStream' )
239
			->will( $this->returnValue( 1 ) );
240
241
		$this->assertEquals( 1, $this->object->reads( 'test' ) );
242
	}
243
244
245
	public function testReadsException()
246
	{
247
		$this->mock->expects( $this->once() )->method( 'readStream' )
248
			->will( $this->throwException( new \RuntimeException() ) );
249
250
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
251
		$this->object->reads( 'test' );
252
	}
253
254
255
	public function testWrite()
256
	{
257
		$this->mock->expects( $this->once() )->method( 'write' );
258
259
		$object = $this->object->write( 'test', 'value' );
260
261
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
262
	}
263
264
265
	public function testWriteException()
266
	{
267
		$this->mock->expects( $this->once() )->method( 'write' )
268
			->will( $this->throwException( new \RuntimeException() ) );
269
270
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
271
		$this->object->write( 'test', 'value' );
272
	}
273
274
275
	public function testWritef()
276
	{
277
		$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'flytest';
278
		file_put_contents( $file, 'test' );
279
280
		$this->mock->expects( $this->once() )->method( 'writeStream' );
281
282
		$object = $this->object->writef( 'file', $file );
283
284
		unlink( $file );
285
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
286
	}
287
288
289
	public function testWrites()
290
	{
291
		$this->mock->expects( $this->once() )->method( 'writeStream' );
292
293
		$object = $this->object->writes( 'test', 1 );
294
295
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
296
	}
297
298
299
	public function testWritesException()
300
	{
301
		$this->mock->expects( $this->once() )->method( 'writeStream' )
302
			->will( $this->throwException( new \RuntimeException() ) );
303
304
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
305
		$this->object->writes( 'test', null );
306
	}
307
308
309
	public function testMove()
310
	{
311
		$this->mock->expects( $this->once() )->method( 'move' )
312
			->will( $this->returnValue( true ) );
313
314
		$object = $this->object->move( 'file1', 'file2' );
315
316
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
317
	}
318
319
320
	public function testMoveException()
321
	{
322
		$this->mock->expects( $this->once() )->method( 'move' )
323
			->will( $this->throwException( new \RuntimeException() ) );
324
325
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
326
		$this->object->move( 'file1', 'file2' );
327
	}
328
329
330
	public function testCopy()
331
	{
332
		$this->mock->expects( $this->once() )->method( 'copy' );
333
334
		$object = $this->object->copy( 'file1', 'file2' );
335
336
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
337
	}
338
339
340
	public function testCopyException()
341
	{
342
		$this->mock->expects( $this->once() )->method( 'copy' )
343
			->will( $this->throwException( new \RuntimeException() ) );
344
345
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
346
		$this->object->copy( 'file1', 'file2' );
347
	}
348
}
349