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
|
|
|
public function testRmdir() |
59
|
|
|
{ |
60
|
|
|
$this->mock->expects( $this->once() )->method( 'deleteDirectory' ); |
61
|
|
|
|
62
|
|
|
$this->object->rmdir( 'test' ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
public function testScan() |
67
|
|
|
{ |
68
|
|
|
$list = array( 't', 'es', 'st' ); |
69
|
|
|
|
70
|
|
|
$this->mock->expects( $this->once() )->method( 'directories' ) |
71
|
|
|
->will( $this->returnValue( array( 't', 'es' ) ) ); |
72
|
|
|
|
73
|
|
|
$this->mock->expects( $this->once() )->method( 'files' ) |
74
|
|
|
->will( $this->returnValue( array( 'st' ) ) ); |
75
|
|
|
|
76
|
|
|
$result = $this->object->scan(); |
77
|
|
|
|
78
|
|
|
$this->assertInternalType( 'array', $result ); |
79
|
|
|
|
80
|
|
|
foreach( $result as $entry ) { |
81
|
|
|
$this->assertTrue( in_array( $entry, $list ) ); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
View Code Duplication |
public function testSize() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$this->mock->expects( $this->once() )->method( 'size' ) |
89
|
|
|
->will( $this->returnValue( 4 ) ); |
90
|
|
|
|
91
|
|
|
$result = $this->object->size( 'test' ); |
92
|
|
|
|
93
|
|
|
$this->assertEquals( 4, $result ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
View Code Duplication |
public function testTime() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$this->mock->expects( $this->once() )->method( 'lastModified' ) |
100
|
|
|
->will( $this->returnValue( 1 ) ); |
101
|
|
|
|
102
|
|
|
$result = $this->object->time( 'test' ); |
103
|
|
|
|
104
|
|
|
$this->assertGreaterThan( 0, $result ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
public function testRm() |
109
|
|
|
{ |
110
|
|
|
$this->mock->expects( $this->once() )->method( 'delete' ); |
111
|
|
|
|
112
|
|
|
$this->object->rm( 'test' ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
View Code Duplication |
public function testHas() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$this->mock->expects( $this->once() )->method( 'exists' ) |
119
|
|
|
->will( $this->returnValue( true ) ); |
120
|
|
|
|
121
|
|
|
$result = $this->object->has( 'test' ); |
122
|
|
|
|
123
|
|
|
$this->assertTrue( $result ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
View Code Duplication |
public function testHasFalse() |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$this->mock->expects( $this->once() )->method( 'exists' ) |
130
|
|
|
->will( $this->returnValue( false ) ); |
131
|
|
|
|
132
|
|
|
$result = $this->object->has( 'test' ); |
133
|
|
|
|
134
|
|
|
$this->assertFalse( $result ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
View Code Duplication |
public function testRead() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
141
|
|
|
->will( $this->returnValue( 'test' ) ); |
142
|
|
|
|
143
|
|
|
$result = $this->object->read( 'file' ); |
144
|
|
|
|
145
|
|
|
$this->assertEquals( 'test', $result ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
public function testReadException() |
150
|
|
|
{ |
151
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
152
|
|
|
->will( $this->throwException( new \Exception() ) ); |
153
|
|
|
|
154
|
|
|
$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' ); |
155
|
|
|
$this->object->read( 'readinvalid' ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
View Code Duplication |
public function testReadf() |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
162
|
|
|
->will( $this->returnValue( 'test' ) ); |
163
|
|
|
|
164
|
|
|
$result = $this->object->readf( 'file' ); |
165
|
|
|
|
166
|
|
|
$this->assertEquals( 'test', file_get_contents( $result ) ); |
167
|
|
|
unlink( $result ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
public function testReads() |
172
|
|
|
{ |
173
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
174
|
|
|
->will( $this->returnValue( 'test' ) ); |
175
|
|
|
|
176
|
|
|
$handle = $this->object->reads( 'file' ); |
177
|
|
|
|
178
|
|
|
$this->assertInternalType( 'resource', $handle ); |
179
|
|
|
$this->assertEquals( 'test', fgets( $handle ) ); |
180
|
|
|
|
181
|
|
|
fclose( $handle ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
public function testReadsException() |
186
|
|
|
{ |
187
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
188
|
|
|
->will( $this->throwException( new \Exception() ) ); |
189
|
|
|
|
190
|
|
|
$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' ); |
191
|
|
|
$this->object->reads( 'readinvalid' ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
public function testWrite() |
196
|
|
|
{ |
197
|
|
|
$this->mock->expects( $this->once() )->method( 'put' ); |
198
|
|
|
|
199
|
|
|
$this->object->write( 'file', 'test' ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
View Code Duplication |
public function testWriteException() |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
$this->mock->expects( $this->once() )->method( 'put' ) |
206
|
|
|
->will( $this->throwException( new \Exception() ) ); |
207
|
|
|
|
208
|
|
|
$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' ); |
209
|
|
|
$this->object->write( '', 'test' ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
public function testWritef() |
214
|
|
|
{ |
215
|
|
|
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'file99'; |
216
|
|
|
file_put_contents( $file, 'test' ); |
217
|
|
|
|
218
|
|
|
$this->mock->expects( $this->once() )->method( 'put' ); |
219
|
|
|
|
220
|
|
|
$this->object->writef( 'file', $file ); |
221
|
|
|
|
222
|
|
|
unlink( $file ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
public function testWritefException() |
227
|
|
|
{ |
228
|
|
|
$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' ); |
229
|
|
|
$this->object->writef( '', 'invalid' ); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
public function testWrites() |
234
|
|
|
{ |
235
|
|
|
$this->mock->expects( $this->once() )->method( 'put' ); |
236
|
|
|
|
237
|
|
|
$handle = fopen( __FILE__, 'r' ); |
238
|
|
|
|
239
|
|
|
$this->object->writes( 'file', $handle ); |
240
|
|
|
|
241
|
|
|
fclose( $handle ); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
View Code Duplication |
public function testWritesException() |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
$this->mock->expects( $this->once() )->method( 'put' ) |
248
|
|
|
->will( $this->throwException( new \Exception() ) ); |
249
|
|
|
|
250
|
|
|
$handle = fopen( __FILE__, 'r' ); |
251
|
|
|
|
252
|
|
|
$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' ); |
253
|
|
|
$this->object->writes( 'file', $handle ); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
public function testWritesException2() |
258
|
|
|
{ |
259
|
|
|
$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' ); |
260
|
|
|
$this->object->writes( 'file', null ); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
public function testMove() |
265
|
|
|
{ |
266
|
|
|
$this->mock->expects( $this->once() )->method( 'move' ); |
267
|
|
|
|
268
|
|
|
$this->object->move( 'file1', 'file2' ); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
|
272
|
|
|
public function testCopy() |
273
|
|
|
{ |
274
|
|
|
$this->mock->expects( $this->once() )->method( 'copy' ); |
275
|
|
|
|
276
|
|
|
$this->object->copy( 'file1', 'file2' ); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
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.