Passed
Push — master ( b62909...01ab92 )
by Aimeos
02:37
created

StandardTest::testRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Aimeos\Base\Filesystem;
4
5
6
class StandardTest extends \PHPUnit\Framework\TestCase
7
{
8
	private $basedir;
9
	private $object;
10
11
12
	protected function setUp() : void
13
	{
14
		$this->basedir = dirname( __DIR__ ) . '/tmp/';
15
		$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => $this->basedir] );
16
	}
17
18
19
	protected function tearDown() : void
20
	{
21
		unset( $this->object );
22
	}
23
24
25
	public function testTempdirException()
26
	{
27
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
28
		new \Aimeos\Base\Filesystem\Standard( ['tempdir' => '/invalid'] );
29
	}
30
31
32
	public function testBasedirException()
33
	{
34
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
35
		new \Aimeos\Base\Filesystem\Standard( ['tempdir' => $this->basedir] );
36
	}
37
38
39
	public function testBasedirPermissionException()
40
	{
41
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
42
		new \Aimeos\Base\Filesystem\Standard( ['tempdir' => $this->basedir, 'basedir' => '/invalid'] );
43
	}
44
45
46
	public function testIsdir()
47
	{
48
		$this->assertTrue( $this->object->isdir( '' ) );
49
	}
50
51
52
	public function testIsdirException()
53
	{
54
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
55
		$this->object->isdir( '..' );
56
	}
57
58
59
	public function testMkdir()
60
	{
61
		$object = $this->object->mkdir( 'fstest' );
62
		$result = is_dir( $this->basedir . 'fstest' );
63
		rmdir( $this->basedir . 'fstest' );
64
65
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\DirIface::class, $object );
66
		$this->assertTrue( $result );
67
	}
68
69
70
	public function testMkdirException()
71
	{
72
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
73
		$this->object->mkdir( '' );
74
	}
75
76
77
	public function testRmdir()
78
	{
79
		mkdir( $this->basedir . 'fstest2' );
80
81
		$object = $this->object->rmdir( 'fstest2' );
82
		$result = is_dir( $this->basedir . 'fstest2' );
83
84
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\DirIface::class, $object );
85
		$this->assertFalse( $result );
86
	}
87
88
89
	public function testRmdirException()
90
	{
91
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
92
		$this->object->rmdir( 'rmdirinvalid' );
93
	}
94
95
96
	public function testScan()
97
	{
98
		touch( $this->basedir . 'file1' );
99
100
		$result = $this->object->scan();
101
102
		$this->assertInstanceof( 'Iterator', $result );
103
104
		$list = [];
105
		foreach( $result as $entry ) {
106
			$list[] = (string) $entry;
107
		}
108
		unlink( $this->basedir . 'file1' );
109
110
		$this->assertTrue( in_array( 'file1', $list ) );
111
	}
112
113
114
	public function testScanException()
115
	{
116
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
117
		$this->object->scan( 'scaninvalid' );
118
	}
119
120
121
	public function testSize()
122
	{
123
		file_put_contents( $this->basedir . 'file2', 'test' );
124
125
		$result = $this->object->size( 'file2' );
126
127
		unlink( $this->basedir . 'file2' );
128
129
		$this->assertEquals( 4, $result );
130
	}
131
132
133
	public function testSizeException()
134
	{
135
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
136
		$this->object->size( 'sizeinvalid' );
137
	}
138
139
140
	public function testTime()
141
	{
142
		touch( $this->basedir . 'file4' );
143
144
		$result = $this->object->time( 'file4' );
145
146
		unlink( $this->basedir . 'file4' );
147
148
		$this->assertGreaterThan( 0, $result );
149
	}
150
151
152
	public function testTimeException()
153
	{
154
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
155
		$this->object->time( 'timeinvalid' );
156
	}
157
158
159
	public function testRm()
160
	{
161
		touch( $this->basedir . 'file5' );
162
163
		$object = $this->object->rm( 'file5' );
164
165
		$result = file_exists( $this->basedir . 'file5' );
166
167
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
168
		$this->assertFalse( $result );
169
	}
170
171
172
	public function testRmException()
173
	{
174
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
175
		$this->object->rm( 'rminvalid' );
176
	}
177
178
179
	public function testHas()
180
	{
181
		touch( $this->basedir . 'file6' );
182
183
		$result = $this->object->has( 'file6' );
184
185
		unlink( $this->basedir . 'file6' );
186
187
		$this->assertTrue( $result );
188
		$this->assertFalse( $this->object->has( 'fsinvalid' ) );
189
	}
190
191
192
	public function testRead()
193
	{
194
		file_put_contents( $this->basedir . 'file7', 'test' );
195
196
		$result = $this->object->read( 'file7' );
197
198
		unlink( $this->basedir . 'file7' );
199
200
		$this->assertEquals( 'test', $result );
201
	}
202
203
204
	public function testReadException()
205
	{
206
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
207
		$this->object->read( 'readinvalid' );
208
	}
209
210
211
	public function testReadf()
212
	{
213
		file_put_contents( $this->basedir . 'file77', 'test' );
214
215
		$result = $this->object->readf( 'file77' );
216
217
		$this->assertTrue( file_exists( $result ) );
218
219
		unlink( $result );
220
	}
221
222
223
	public function testReadfException()
224
	{
225
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
226
		$this->object->readf( 'readinvalid' );
227
	}
228
229
230
	public function testReads()
231
	{
232
		file_put_contents( $this->basedir . 'file8', 'test' );
233
234
		$handle = $this->object->reads( 'file8' );
235
		$result = fgets( $handle );
236
		fclose( $handle );
237
238
		unlink( $this->basedir . 'file8' );
239
240
		$this->assertEquals( 'test', $result );
241
	}
242
243
244
	public function testReadsException()
245
	{
246
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
247
		$this->object->reads( 'readsinvalid' );
248
	}
249
250
251
	public function testWrite()
252
	{
253
		$object = $this->object->write( 'file9', 'test' );
254
255
		$result = file_get_contents( $this->basedir . 'file9' );
256
		unlink( $this->basedir . 'file9' );
257
258
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
259
		$this->assertEquals( 'test', $result );
260
	}
261
262
263
	public function testWriteException()
264
	{
265
		$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] );
266
267
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
268
		$this->object->write( 'path', 'test' );
269
	}
270
271
272
	public function testWritef()
273
	{
274
		$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'file99';
275
		file_put_contents( $file, 'test' );
276
277
		$object = $this->object->writef( 'file99', $file );
278
279
		$result = file_get_contents( $this->basedir . 'file99' );
280
		unlink( $this->basedir . 'file99' );
281
		unlink( $file );
282
283
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
284
		$this->assertEquals( 'test', $result );
285
	}
286
287
288
	public function testWritefException()
289
	{
290
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
291
		$this->object->writef( '', 'test' );
292
	}
293
294
295
	public function testWrites()
296
	{
297
		if( ( $handle = fopen( $this->basedir . 'file10tmp', 'w+' ) ) === false ) {
298
			throw new \RuntimeException( sprintf( 'Failed opening file "%1$s"', $this->basedir . 'file10tmp' ) );
299
		}
300
301
		fwrite( $handle, 'test' );
302
		rewind( $handle );
303
304
		$object = $this->object->writes( 'file10', $handle );
305
306
		fclose( $handle );
307
		$result = file_get_contents( $this->basedir . 'file10' );
308
		unlink( $this->basedir . 'file10tmp' );
309
		unlink( $this->basedir . 'file10' );
310
311
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
312
		$this->assertEquals( 'test', $result );
313
	}
314
315
316
	public function testWritesException()
317
	{
318
		$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] );
319
320
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
321
		$this->object->writes( 'path', null );
322
	}
323
324
325
	public function testWritesFileException()
326
	{
327
		$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] );
328
329
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
330
		$this->object->writes( '.', null );
331
	}
332
333
334
	public function testMove()
335
	{
336
		touch( $this->basedir . 'file11' );
337
338
		$object = $this->object->move( 'file11', 'file11move' );
339
340
		$result = file_exists( $this->basedir . 'file11move' );
341
		$result2 = file_exists( $this->basedir . 'file11' );
342
343
		unlink( $this->basedir . 'file11move' );
344
345
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
346
		$this->assertTrue( $result );
347
		$this->assertFalse( $result2 );
348
	}
349
350
351
	public function testMoveException()
352
	{
353
		$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] );
354
355
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
356
		$this->object->move( 'path', 'dest' );
357
	}
358
359
360
	public function testMoveRenameException()
361
	{
362
		$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] );
363
364
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
365
		$this->object->move( '.', '.' );
366
	}
367
368
369
	public function testCopy()
370
	{
371
		touch( $this->basedir . 'file12' );
372
373
		$object = $this->object->copy( 'file12', 'file12copy' );
374
375
		$result = file_exists( $this->basedir . 'file12copy' );
376
		$result2 = file_exists( $this->basedir . 'file12' );
377
378
		unlink( $this->basedir . 'file12copy' );
379
		unlink( $this->basedir . 'file12' );
380
381
		$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object );
382
		$this->assertTrue( $result );
383
		$this->assertTrue( $result2 );
384
	}
385
386
387
	public function testCopyException()
388
	{
389
		$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] );
390
391
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
392
		$this->object->copy( 'path', 'dest' );
393
	}
394
395
396
	public function testCopyRenameException()
397
	{
398
		$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] );
399
400
		$this->expectException( \Aimeos\Base\Filesystem\Exception::class );
401
		$this->object->copy( '.', '.' );
402
	}
403
}
404