|
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 testSize() |
|
115
|
|
|
{ |
|
116
|
|
|
file_put_contents( $this->basedir . 'file2', 'test' ); |
|
117
|
|
|
|
|
118
|
|
|
$result = $this->object->size( 'file2' ); |
|
119
|
|
|
|
|
120
|
|
|
unlink( $this->basedir . 'file2' ); |
|
121
|
|
|
|
|
122
|
|
|
$this->assertEquals( 4, $result ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
public function testSizeException() |
|
127
|
|
|
{ |
|
128
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
129
|
|
|
$this->object->size( 'sizeinvalid' ); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
public function testTime() |
|
134
|
|
|
{ |
|
135
|
|
|
touch( $this->basedir . 'file4' ); |
|
136
|
|
|
|
|
137
|
|
|
$result = $this->object->time( 'file4' ); |
|
138
|
|
|
|
|
139
|
|
|
unlink( $this->basedir . 'file4' ); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertGreaterThan( 0, $result ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
public function testTimeException() |
|
146
|
|
|
{ |
|
147
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
148
|
|
|
$this->object->time( 'timeinvalid' ); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
public function testRm() |
|
153
|
|
|
{ |
|
154
|
|
|
touch( $this->basedir . 'file5' ); |
|
155
|
|
|
|
|
156
|
|
|
$object = $this->object->rm( 'file5' ); |
|
157
|
|
|
|
|
158
|
|
|
$result = file_exists( $this->basedir . 'file5' ); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object ); |
|
161
|
|
|
$this->assertFalse( $result ); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
public function testRmException() |
|
166
|
|
|
{ |
|
167
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
168
|
|
|
$this->object->rm( 'rminvalid' ); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
public function testHas() |
|
173
|
|
|
{ |
|
174
|
|
|
touch( $this->basedir . 'file6' ); |
|
175
|
|
|
|
|
176
|
|
|
$result = $this->object->has( 'file6' ); |
|
177
|
|
|
|
|
178
|
|
|
unlink( $this->basedir . 'file6' ); |
|
179
|
|
|
|
|
180
|
|
|
$this->assertTrue( $result ); |
|
181
|
|
|
$this->assertFalse( $this->object->has( 'fsinvalid' ) ); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
public function testRead() |
|
186
|
|
|
{ |
|
187
|
|
|
file_put_contents( $this->basedir . 'file7', 'test' ); |
|
188
|
|
|
|
|
189
|
|
|
$result = $this->object->read( 'file7' ); |
|
190
|
|
|
|
|
191
|
|
|
unlink( $this->basedir . 'file7' ); |
|
192
|
|
|
|
|
193
|
|
|
$this->assertEquals( 'test', $result ); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
public function testReadException() |
|
198
|
|
|
{ |
|
199
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
200
|
|
|
$this->object->read( 'readinvalid' ); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
public function testReadf() |
|
205
|
|
|
{ |
|
206
|
|
|
file_put_contents( $this->basedir . 'file77', 'test' ); |
|
207
|
|
|
|
|
208
|
|
|
$result = $this->object->readf( 'file77' ); |
|
209
|
|
|
|
|
210
|
|
|
$this->assertTrue( file_exists( $result ) ); |
|
211
|
|
|
|
|
212
|
|
|
unlink( $result ); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
public function testReadfException() |
|
217
|
|
|
{ |
|
218
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
219
|
|
|
$this->object->readf( 'readinvalid' ); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
public function testReads() |
|
224
|
|
|
{ |
|
225
|
|
|
file_put_contents( $this->basedir . 'file8', 'test' ); |
|
226
|
|
|
|
|
227
|
|
|
$handle = $this->object->reads( 'file8' ); |
|
228
|
|
|
$result = fgets( $handle ); |
|
229
|
|
|
fclose( $handle ); |
|
230
|
|
|
|
|
231
|
|
|
unlink( $this->basedir . 'file8' ); |
|
232
|
|
|
|
|
233
|
|
|
$this->assertEquals( 'test', $result ); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
public function testReadsException() |
|
238
|
|
|
{ |
|
239
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
240
|
|
|
$this->object->reads( 'readsinvalid' ); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
public function testWrite() |
|
245
|
|
|
{ |
|
246
|
|
|
$object = $this->object->write( 'file9', 'test' ); |
|
247
|
|
|
|
|
248
|
|
|
$result = file_get_contents( $this->basedir . 'file9' ); |
|
249
|
|
|
unlink( $this->basedir . 'file9' ); |
|
250
|
|
|
|
|
251
|
|
|
$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object ); |
|
252
|
|
|
$this->assertEquals( 'test', $result ); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
public function testWriteException() |
|
257
|
|
|
{ |
|
258
|
|
|
$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] ); |
|
259
|
|
|
|
|
260
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
261
|
|
|
$this->object->write( 'path', 'test' ); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
public function testWritef() |
|
266
|
|
|
{ |
|
267
|
|
|
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'file99'; |
|
268
|
|
|
file_put_contents( $file, 'test' ); |
|
269
|
|
|
|
|
270
|
|
|
$object = $this->object->writef( 'file99', $file ); |
|
271
|
|
|
|
|
272
|
|
|
$result = file_get_contents( $this->basedir . 'file99' ); |
|
273
|
|
|
unlink( $this->basedir . 'file99' ); |
|
274
|
|
|
unlink( $file ); |
|
275
|
|
|
|
|
276
|
|
|
$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object ); |
|
277
|
|
|
$this->assertEquals( 'test', $result ); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
public function testWritefException() |
|
282
|
|
|
{ |
|
283
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
284
|
|
|
$this->object->writef( '', 'test' ); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
|
|
288
|
|
|
public function testWrites() |
|
289
|
|
|
{ |
|
290
|
|
|
if( ( $handle = fopen( $this->basedir . 'file10tmp', 'w+' ) ) === false ) { |
|
291
|
|
|
throw new \RuntimeException( sprintf( 'Failed opening file "%1$s"', $this->basedir . 'file10tmp' ) ); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
fwrite( $handle, 'test' ); |
|
295
|
|
|
rewind( $handle ); |
|
296
|
|
|
|
|
297
|
|
|
$object = $this->object->writes( 'file10', $handle ); |
|
298
|
|
|
|
|
299
|
|
|
fclose( $handle ); |
|
300
|
|
|
$result = file_get_contents( $this->basedir . 'file10' ); |
|
301
|
|
|
unlink( $this->basedir . 'file10tmp' ); |
|
302
|
|
|
unlink( $this->basedir . 'file10' ); |
|
303
|
|
|
|
|
304
|
|
|
$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object ); |
|
305
|
|
|
$this->assertEquals( 'test', $result ); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
public function testWritesException() |
|
310
|
|
|
{ |
|
311
|
|
|
$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] ); |
|
312
|
|
|
|
|
313
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
314
|
|
|
$this->object->writes( 'path', null ); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
public function testWritesFileException() |
|
319
|
|
|
{ |
|
320
|
|
|
$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] ); |
|
321
|
|
|
|
|
322
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
323
|
|
|
$this->object->writes( '.', null ); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
|
|
327
|
|
|
public function testMove() |
|
328
|
|
|
{ |
|
329
|
|
|
touch( $this->basedir . 'file11' ); |
|
330
|
|
|
|
|
331
|
|
|
$object = $this->object->move( 'file11', 'file11move' ); |
|
332
|
|
|
|
|
333
|
|
|
$result = file_exists( $this->basedir . 'file11move' ); |
|
334
|
|
|
$result2 = file_exists( $this->basedir . 'file11' ); |
|
335
|
|
|
|
|
336
|
|
|
unlink( $this->basedir . 'file11move' ); |
|
337
|
|
|
|
|
338
|
|
|
$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object ); |
|
339
|
|
|
$this->assertTrue( $result ); |
|
340
|
|
|
$this->assertFalse( $result2 ); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
|
|
344
|
|
|
public function testMoveException() |
|
345
|
|
|
{ |
|
346
|
|
|
$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] ); |
|
347
|
|
|
|
|
348
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
349
|
|
|
$this->object->move( 'path', 'dest' ); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
|
|
353
|
|
|
public function testMoveRenameException() |
|
354
|
|
|
{ |
|
355
|
|
|
$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] ); |
|
356
|
|
|
|
|
357
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
358
|
|
|
$this->object->move( '.', '.' ); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
|
|
362
|
|
|
public function testCopy() |
|
363
|
|
|
{ |
|
364
|
|
|
touch( $this->basedir . 'file12' ); |
|
365
|
|
|
|
|
366
|
|
|
$object = $this->object->copy( 'file12', 'file12copy' ); |
|
367
|
|
|
|
|
368
|
|
|
$result = file_exists( $this->basedir . 'file12copy' ); |
|
369
|
|
|
$result2 = file_exists( $this->basedir . 'file12' ); |
|
370
|
|
|
|
|
371
|
|
|
unlink( $this->basedir . 'file12copy' ); |
|
372
|
|
|
unlink( $this->basedir . 'file12' ); |
|
373
|
|
|
|
|
374
|
|
|
$this->assertInstanceOf( \Aimeos\Base\Filesystem\Iface::class, $object ); |
|
375
|
|
|
$this->assertTrue( $result ); |
|
376
|
|
|
$this->assertTrue( $result2 ); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
|
|
380
|
|
|
public function testCopyException() |
|
381
|
|
|
{ |
|
382
|
|
|
$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] ); |
|
383
|
|
|
|
|
384
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
385
|
|
|
$this->object->copy( 'path', 'dest' ); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
|
|
389
|
|
|
public function testCopyRenameException() |
|
390
|
|
|
{ |
|
391
|
|
|
$this->object = new \Aimeos\Base\Filesystem\Standard( ['basedir' => '/root'] ); |
|
392
|
|
|
|
|
393
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
|
394
|
|
|
$this->object->copy( '.', '.' ); |
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
|
|
|