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