Completed
Push — master ( 94b58b...843436 )
by Aimeos
02:17
created

LaravelTest::testReads()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.8666
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2017
6
 */
7
8
9
namespace Aimeos\MW\Filesystem;
10
11
12
class LaravelTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $mock;
15
	private $object;
16
17
18 View Code Duplication
	protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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\MW\Filesystem\Laravel( $this->mock, sys_get_temp_dir() );
29
	}
30
31
32
	protected function tearDown()
33
	{
34
		unset( $this->object );
35
	}
36
37
38 View Code Duplication
	public function testIsdir()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 View Code Duplication
	public function testIsdirFalse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
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->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
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->assertInternalType( 'array', $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->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
118
		$this->object->scan( 'test' );
119
	}
120
121
122 View Code Duplication
	public function testSize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 View Code Duplication
	public function testSizeException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
134
	{
135
		$this->mock->expects( $this->once() )->method( 'size' )
136
			->will( $this->throwException( new \RuntimeException() ) );
137
138
		$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
139
		$this->object->size( 'test' );
140
	}
141
142
143 View Code Duplication
	public function testTime()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
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->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
178
		$this->object->rm( 'test' );
179
	}
180
181
182 View Code Duplication
	public function testHas()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 View Code Duplication
	public function testHasFalse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 View Code Duplication
	public function testRead()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 View Code Duplication
	public function testReadException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
216
	{
217
		$this->mock->expects( $this->once() )->method( 'get' )
218
			->will( $this->throwException( new \RuntimeException() ) );
219
220
		$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
221
		$this->object->read( 'readinvalid' );
222
	}
223
224
225 View Code Duplication
	public function testReadf()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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->assertInternalType( 'resource', $handle );
245
		$this->assertEquals( 'test', fgets( $handle ) );
246
247
		fclose( $handle );
248
	}
249
250
251 View Code Duplication
	public function testReadsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
252
	{
253
		$this->mock->expects( $this->once() )->method( 'get' )
254
			->will( $this->throwException( new \RuntimeException() ) );
255
256
		$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
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 View Code Duplication
	public function testWriteException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
270
	{
271
		$this->mock->expects( $this->once() )->method( 'put' )
272
			->will( $this->throwException( new \RuntimeException() ) );
273
274
		$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
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->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
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->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
319
		$this->object->writes( 'file', $handle );
320
	}
321
322
323
	public function testWritesException2()
324
	{
325
		$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
326
		$this->object->writes( 'file', null );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 View Code Duplication
	public function testMoveException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
339
	{
340
		$this->mock->expects( $this->once() )->method( 'move' )
341
			->will( $this->throwException( new \RuntimeException() ) );
342
343
		$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
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 View Code Duplication
	public function testCopyException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
357
	{
358
		$this->mock->expects( $this->once() )->method( 'copy' )
359
			->will( $this->throwException( new \RuntimeException() ) );
360
361
		$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
362
		$this->object->copy( 'file1', 'file2' );
363
	}
364
}
365