1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2022 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Base\Filesystem; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class LaravelTest extends \PHPUnit\Framework\TestCase |
13
|
|
|
{ |
14
|
|
|
private $mock; |
15
|
|
|
private $object; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
protected function setUp() : void |
19
|
|
|
{ |
20
|
|
|
if( !interface_exists( '\\Illuminate\\Contracts\\Filesystem\\Filesystem' ) ) { |
21
|
|
|
$this->markTestSkipped( 'Install Laravel framework first' ); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$this->mock = $this->getMockBuilder( '\\Illuminate\\Contracts\\Filesystem\\Filesystem' ) |
25
|
|
|
->disableOriginalConstructor() |
26
|
|
|
->getMock(); |
27
|
|
|
|
28
|
|
|
$this->object = new \Aimeos\Base\Filesystem\Laravel( $this->mock, sys_get_temp_dir() ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
protected function tearDown() : void |
33
|
|
|
{ |
34
|
|
|
unset( $this->object ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
public function testIsdir() |
39
|
|
|
{ |
40
|
|
|
$this->mock->expects( $this->once() )->method( 'directories' ) |
41
|
|
|
->will( $this->returnValue( array( 't', 'test', 'es' ) ) ); |
42
|
|
|
|
43
|
|
|
$this->assertTrue( $this->object->isdir( 'test' ) ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
public function testIsdirFalse() |
48
|
|
|
{ |
49
|
|
|
$this->mock->expects( $this->once() )->method( 'directories' ) |
50
|
|
|
->will( $this->returnValue( array( 't', 'es' ) ) ); |
51
|
|
|
|
52
|
|
|
$this->assertFalse( $this->object->isdir( 'test' ) ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
public function testMkdir() |
57
|
|
|
{ |
58
|
|
|
$this->mock->expects( $this->once() )->method( 'makeDirectory' ); |
59
|
|
|
|
60
|
|
|
$this->object->mkdir( 'test' ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
public function testMkdirException() |
65
|
|
|
{ |
66
|
|
|
$this->mock->expects( $this->once() )->method( 'makeDirectory' ) |
67
|
|
|
->will( $this->throwException( new \RuntimeException() ) ); |
68
|
|
|
|
69
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
70
|
|
|
$this->object->mkdir( 'test' ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
public function testRmdir() |
75
|
|
|
{ |
76
|
|
|
$this->mock->expects( $this->once() )->method( 'deleteDirectory' ); |
77
|
|
|
|
78
|
|
|
$this->object->rmdir( 'test' ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
public function testRmdirException() |
83
|
|
|
{ |
84
|
|
|
$this->mock->expects( $this->once() )->method( 'deleteDirectory' ) |
85
|
|
|
->will( $this->throwException( new \RuntimeException() ) ); |
86
|
|
|
|
87
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
88
|
|
|
$this->object->rmdir( 'test' ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
public function testScan() |
93
|
|
|
{ |
94
|
|
|
$list = array( 't', 'es', 'st' ); |
95
|
|
|
|
96
|
|
|
$this->mock->expects( $this->once() )->method( 'directories' ) |
97
|
|
|
->will( $this->returnValue( array( 't', 'es' ) ) ); |
98
|
|
|
|
99
|
|
|
$this->mock->expects( $this->once() )->method( 'files' ) |
100
|
|
|
->will( $this->returnValue( array( 'st' ) ) ); |
101
|
|
|
|
102
|
|
|
$result = $this->object->scan(); |
103
|
|
|
|
104
|
|
|
$this->assertIsArray( $result ); |
105
|
|
|
|
106
|
|
|
foreach( $result as $entry ) { |
107
|
|
|
$this->assertTrue( in_array( $entry, $list ) ); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
public function testScanException() |
113
|
|
|
{ |
114
|
|
|
$this->mock->expects( $this->once() )->method( 'directories' ) |
115
|
|
|
->will( $this->throwException( new \RuntimeException() ) ); |
116
|
|
|
|
117
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
118
|
|
|
$this->object->scan( 'test' ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
public function testSize() |
123
|
|
|
{ |
124
|
|
|
$this->mock->expects( $this->once() )->method( 'size' ) |
125
|
|
|
->will( $this->returnValue( 4 ) ); |
126
|
|
|
|
127
|
|
|
$result = $this->object->size( 'test' ); |
128
|
|
|
|
129
|
|
|
$this->assertEquals( 4, $result ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
public function testSizeException() |
134
|
|
|
{ |
135
|
|
|
$this->mock->expects( $this->once() )->method( 'size' ) |
136
|
|
|
->will( $this->throwException( new \RuntimeException() ) ); |
137
|
|
|
|
138
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
139
|
|
|
$this->object->size( 'test' ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
public function testTime() |
144
|
|
|
{ |
145
|
|
|
$this->mock->expects( $this->once() )->method( 'lastModified' ) |
146
|
|
|
->will( $this->returnValue( 1 ) ); |
147
|
|
|
|
148
|
|
|
$result = $this->object->time( 'test' ); |
149
|
|
|
|
150
|
|
|
$this->assertGreaterThan( 0, $result ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
public function testTimeException() |
155
|
|
|
{ |
156
|
|
|
$this->mock->expects( $this->once() )->method( 'lastModified' ) |
157
|
|
|
->will( $this->throwException( new \RuntimeException() ) ); |
158
|
|
|
|
159
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
160
|
|
|
$this->object->time( 'test' ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
public function testRm() |
165
|
|
|
{ |
166
|
|
|
$this->mock->expects( $this->once() )->method( 'delete' ); |
167
|
|
|
|
168
|
|
|
$this->object->rm( 'test' ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
public function testRmException() |
173
|
|
|
{ |
174
|
|
|
$this->mock->expects( $this->once() )->method( 'delete' ) |
175
|
|
|
->will( $this->throwException( new \RuntimeException() ) ); |
176
|
|
|
|
177
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
178
|
|
|
$this->object->rm( 'test' ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
public function testHas() |
183
|
|
|
{ |
184
|
|
|
$this->mock->expects( $this->once() )->method( 'exists' ) |
185
|
|
|
->will( $this->returnValue( true ) ); |
186
|
|
|
|
187
|
|
|
$result = $this->object->has( 'test' ); |
188
|
|
|
|
189
|
|
|
$this->assertTrue( $result ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
public function testHasFalse() |
194
|
|
|
{ |
195
|
|
|
$this->mock->expects( $this->once() )->method( 'exists' ) |
196
|
|
|
->will( $this->returnValue( false ) ); |
197
|
|
|
|
198
|
|
|
$result = $this->object->has( 'test' ); |
199
|
|
|
|
200
|
|
|
$this->assertFalse( $result ); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
public function testRead() |
205
|
|
|
{ |
206
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
207
|
|
|
->will( $this->returnValue( 'test' ) ); |
208
|
|
|
|
209
|
|
|
$result = $this->object->read( 'file' ); |
210
|
|
|
|
211
|
|
|
$this->assertEquals( 'test', $result ); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
public function testReadException() |
216
|
|
|
{ |
217
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
218
|
|
|
->will( $this->throwException( new \RuntimeException() ) ); |
219
|
|
|
|
220
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
221
|
|
|
$this->object->read( 'readinvalid' ); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
public function testReadf() |
226
|
|
|
{ |
227
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
228
|
|
|
->will( $this->returnValue( 'test' ) ); |
229
|
|
|
|
230
|
|
|
$result = $this->object->readf( 'file' ); |
231
|
|
|
|
232
|
|
|
$this->assertEquals( 'test', file_get_contents( $result ) ); |
233
|
|
|
unlink( $result ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
public function testReads() |
238
|
|
|
{ |
239
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
240
|
|
|
->will( $this->returnValue( 'test' ) ); |
241
|
|
|
|
242
|
|
|
$handle = $this->object->reads( 'file' ); |
243
|
|
|
|
244
|
|
|
$this->assertIsResource( $handle ); |
245
|
|
|
$this->assertEquals( 'test', fgets( $handle ) ); |
246
|
|
|
|
247
|
|
|
fclose( $handle ); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
public function testReadsException() |
252
|
|
|
{ |
253
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
254
|
|
|
->will( $this->throwException( new \RuntimeException() ) ); |
255
|
|
|
|
256
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
257
|
|
|
$this->object->reads( 'readinvalid' ); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
public function testWrite() |
262
|
|
|
{ |
263
|
|
|
$this->mock->expects( $this->once() )->method( 'put' ); |
264
|
|
|
|
265
|
|
|
$this->object->write( 'file', 'test' ); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
public function testWriteException() |
270
|
|
|
{ |
271
|
|
|
$this->mock->expects( $this->once() )->method( 'put' ) |
272
|
|
|
->will( $this->throwException( new \RuntimeException() ) ); |
273
|
|
|
|
274
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
275
|
|
|
$this->object->write( '', 'test' ); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
public function testWritef() |
280
|
|
|
{ |
281
|
|
|
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'file99'; |
282
|
|
|
file_put_contents( $file, 'test' ); |
283
|
|
|
|
284
|
|
|
$this->mock->expects( $this->once() )->method( 'put' ); |
285
|
|
|
|
286
|
|
|
$this->object->writef( 'file', $file ); |
287
|
|
|
|
288
|
|
|
unlink( $file ); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
|
292
|
|
|
public function testWritefException() |
293
|
|
|
{ |
294
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
295
|
|
|
$this->object->writef( '', 'invalid' ); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
|
299
|
|
|
public function testWrites() |
300
|
|
|
{ |
301
|
|
|
$this->mock->expects( $this->once() )->method( 'put' ); |
302
|
|
|
|
303
|
|
|
$handle = fopen( __FILE__, 'r' ); |
304
|
|
|
|
305
|
|
|
$this->object->writes( 'file', $handle ); |
306
|
|
|
|
307
|
|
|
fclose( $handle ); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
public function testWritesException() |
312
|
|
|
{ |
313
|
|
|
$this->mock->expects( $this->once() )->method( 'put' ) |
314
|
|
|
->will( $this->throwException( new \RuntimeException() ) ); |
315
|
|
|
|
316
|
|
|
$handle = fopen( __FILE__, 'r' ); |
317
|
|
|
|
318
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
319
|
|
|
$this->object->writes( 'file', $handle ); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
|
323
|
|
|
public function testWritesException2() |
324
|
|
|
{ |
325
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
326
|
|
|
$this->object->writes( 'file', null ); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
|
330
|
|
|
public function testMove() |
331
|
|
|
{ |
332
|
|
|
$this->mock->expects( $this->once() )->method( 'move' ); |
333
|
|
|
|
334
|
|
|
$this->object->move( 'file1', 'file2' ); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
|
338
|
|
|
public function testMoveException() |
339
|
|
|
{ |
340
|
|
|
$this->mock->expects( $this->once() )->method( 'move' ) |
341
|
|
|
->will( $this->throwException( new \RuntimeException() ) ); |
342
|
|
|
|
343
|
|
|
$this->expectException( \Aimeos\Base\Filesystem\Exception::class ); |
344
|
|
|
$this->object->move( 'file1', 'file2' ); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
|
348
|
|
|
public function testCopy() |
349
|
|
|
{ |
350
|
|
|
$this->mock->expects( $this->once() )->method( 'copy' ); |
351
|
|
|
|
352
|
|
|
$this->object->copy( 'file1', 'file2' ); |
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
|
|
|
|