MergeFilesTest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 308
Duplicated Lines 63.31 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
dl 195
loc 308
c 0
b 0
f 0
wmc 15
lcom 1
cbo 12
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testInstanceOf() 0 4 1
B testSimpleMergeFiles() 30 30 1
A createCollection() 0 12 2
B testCallingContractWillPassThroughOptions() 34 34 1
B testDeleteOldFilesWillDeleteAnyEmptyDirectories() 0 33 1
A setUp() 0 6 1
A testCanContractAcceptsFileNodeCollectionInterface() 0 9 1
A testCanContractOnlyAcceptsLocalFiles() 21 21 1
A testCanContractOnlyWithFilesThatExist() 23 23 1
A testCanContractOnlyUncompressedFiles() 23 23 1
A testCallingContractWithAFileThatCannotBeContractedWillThrowAnException() 16 16 1
A testCallingContractWithANonLocalTargetWillThrowAnException() 18 18 1
B testCallingMergeWithKeepOldFilesAsFalseDeletesAllTheFilesInTheCollection() 30 30 1
A testProcessFailedThrowException() 0 17 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Integration\Modify\Contract;
15
16
use Graze\DataFile\Helper\Builder\Builder;
17
use Graze\DataFile\Helper\Builder\BuilderInterface;
18
use Graze\DataFile\Modify\Compress\CompressionFactory;
19
use Graze\DataFile\Modify\Compress\Gzip;
20
use Graze\DataFile\Modify\Contract\FileContractorInterface;
21
use Graze\DataFile\Modify\Contract\MergeFiles;
22
use Graze\DataFile\Modify\MakeDirectory;
23
use Graze\DataFile\Node\FileNodeCollection;
24
use Graze\DataFile\Node\FileNodeCollectionInterface;
25
use Graze\DataFile\Node\FileNodeInterface;
26
use Graze\DataFile\Node\LocalFile;
27
use Graze\DataFile\Test\AbstractFileTestCase;
28
use InvalidArgumentException;
29
use Mockery as m;
30
use Symfony\Component\Process\Exception\ProcessFailedException;
31
32
class MergeFilesTest extends AbstractFileTestCase
33
{
34
    /**
35
     * @var BuilderInterface|m\MockInterface
36
     */
37
    protected $builder;
38
39
    /**
40
     * @var MergeFiles
41
     */
42
    private $merge;
43
44
    public function setUp()
45
    {
46
        $this->builder = m::mock(Builder::class)->makePartial();
47
        $this->merge = new MergeFiles();
48
        $this->merge->setBuilder($this->builder);
0 ignored issues
show
Documentation introduced by
$this->builder is of type object<Mockery\Mock>, but the function expects a object<Graze\DataFile\He...ilder\BuilderInterface>.

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...
49
    }
50
51
    public function testInstanceOf()
52
    {
53
        static::assertInstanceOf(FileContractorInterface::class, $this->merge);
54
    }
55
56
    public function testCanContractAcceptsFileNodeCollectionInterface()
57
    {
58
        $collection = m::mock(FileNodeCollectionInterface::class);
59
        $collection->shouldReceive('getIterator')
60
                   ->andReturn([]);
61
62
        $node = m::mock(LocalFile::class);
63
        static::assertTrue($this->merge->canContract($collection, $node));
64
    }
65
66 View Code Duplication
    public function testCanContractOnlyAcceptsLocalFiles()
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...
67
    {
68
        $collection = new FileNodeCollection();
69
        $file1 = m::mock(LocalFile::class);
70
        $file1->shouldReceive('exists')
71
              ->andReturn(true);
72
        $file1->shouldReceive('getCompression')
73
              ->andReturn(CompressionFactory::TYPE_NONE);
74
        $collection->add($file1);
75
76
        $out = m::mock(LocalFile::class);
77
78
        static::assertTrue($this->merge->canContract($collection, $out));
79
80
        $file2 = m::mock(FileNodeInterface::class);
81
        $file2->shouldReceive('getCompression')
82
              ->andReturn(CompressionFactory::TYPE_NONE);
83
        $collection->add($file2);
84
85
        static::assertFalse($this->merge->canContract($collection, $out));
86
    }
87
88 View Code Duplication
    public function testCanContractOnlyWithFilesThatExist()
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...
89
    {
90
        $collection = new FileNodeCollection();
91
        $file1 = m::mock(LocalFile::class);
92
        $file1->shouldReceive('exists')
93
              ->andReturn(true);
94
        $file1->shouldReceive('getCompression')
95
              ->andReturn(CompressionFactory::TYPE_NONE);
96
        $collection->add($file1);
97
98
        $out = m::mock(LocalFile::class);
99
100
        static::assertTrue($this->merge->canContract($collection, $out));
101
102
        $file2 = m::mock(LocalFile::class);
103
        $file2->shouldReceive('exists')
104
              ->andReturn(false);
105
        $file2->shouldReceive('getCompression')
106
              ->andReturn(CompressionFactory::TYPE_NONE);
107
        $collection->add($file2);
108
109
        static::assertFalse($this->merge->canContract($collection, $out));
110
    }
111
112 View Code Duplication
    public function testCanContractOnlyUncompressedFiles()
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...
113
    {
114
        $collection = new FileNodeCollection();
115
        $file1 = m::mock(LocalFile::class);
116
        $file1->shouldReceive('exists')
117
              ->andReturn(true);
118
        $file1->shouldReceive('getCompression')
119
              ->andReturn(CompressionFactory::TYPE_NONE);
120
        $collection->add($file1);
121
122
        $out = m::mock(LocalFile::class);
123
124
        static::assertTrue($this->merge->canContract($collection, $out));
125
126
        $file2 = m::mock(LocalFile::class);
127
        $file2->shouldReceive('exists')
128
              ->andReturn(true);
129
        $file2->shouldReceive('getCompression')
130
              ->andReturn(Gzip::NAME);
131
        $collection->add($file2);
132
133
        static::assertFalse($this->merge->canContract($collection, $out));
134
    }
135
136 View Code Duplication
    public function testCallingContractWithAFileThatCannotBeContractedWillThrowAnException()
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...
137
    {
138
        $collection = new FileNodeCollection();
139
        $file = m::mock(LocalFile::class);
140
        $file->shouldReceive('exists')
141
             ->andReturn(false);
142
        $file->shouldReceive('getCompression')
143
             ->andReturn(CompressionFactory::TYPE_NONE);
144
        $collection->add($file);
145
146
        $target = m::mock(LocalFile::class);
147
148
        $this->expectException(InvalidArgumentException::class);
149
150
        $this->merge->contract($collection, $target);
151
    }
152
153 View Code Duplication
    public function testCallingContractWithANonLocalTargetWillThrowAnException()
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...
154
    {
155
        $collection = new FileNodeCollection();
156
        $file = m::mock(LocalFile::class);
157
        $file->shouldReceive('exists')
158
             ->andReturn(true);
159
        $file->shouldReceive('getCompression')
160
             ->andReturn(CompressionFactory::TYPE_NONE);
161
        $collection->add($file);
162
163
        $target = m::mock(FileNodeInterface::class);
164
165
        static::assertFalse($this->merge->canContract($collection, $target));
166
167
        $this->expectException(InvalidArgumentException::class);
168
169
        $this->merge->contract($collection, $target);
170
    }
171
172 View Code Duplication
    public function testSimpleMergeFiles()
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...
173
    {
174
        $collection = $this->createCollection('simple.merge/', 3);
175
176
        $outputFile = new LocalFile(static::$dir . 'simple.merge.output');
177
178
        $file = $this->merge->contract($collection, $outputFile);
179
180
        static::assertSame($file, $outputFile);
181
        static::assertEquals(
182
            [
183
                "File 1 Line 1",
184
                "File 1 Line 2",
185
                "File 1 Line 3",
186
                "File 2 Line 1",
187
                "File 2 Line 2",
188
                "File 2 Line 3",
189
                "File 3 Line 1",
190
                "File 3 Line 2",
191
                "File 3 Line 3",
192
            ],
193
            $file->getContents()
194
        );
195
196
        $exists = $collection->filter(function (FileNodeInterface $item) {
197
            return $item->exists();
198
        });
199
200
        static::assertCount(3, $exists);
201
    }
202
203
    /**
204
     * @param string $rootDir
205
     * @param int    $numFiles
206
     *
207
     * @return FileNodeCollectionInterface
208
     */
209
    private function createCollection($rootDir, $numFiles)
210
    {
211
        $mkDir = new MakeDirectory();
212
        $collection = new FileNodeCollection();
213
        for ($i = 1; $i <= $numFiles; $i++) {
214
            $file = new LocalFile(static::$dir . $rootDir . 'part_' . $i);
215
            $mkDir->makeDirectory($file);
216
            $file->put("File $i Line 1\nFile $i Line 2\nFile $i Line 3\n");
217
            $collection->add($file);
218
        }
219
        return $collection;
220
    }
221
222 View Code Duplication
    public function testCallingMergeWithKeepOldFilesAsFalseDeletesAllTheFilesInTheCollection()
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...
223
    {
224
        $collection = $this->createCollection('simple.merge.delete/', 3);
225
226
        $outputFile = new LocalFile(static::$dir . 'simple.merge.delete.output');
227
228
        $file = $this->merge->contract($collection, $outputFile, ['keepOldFiles' => false]);
229
230
        static::assertSame($file, $outputFile);
231
        static::assertEquals(
232
            [
233
                "File 1 Line 1",
234
                "File 1 Line 2",
235
                "File 1 Line 3",
236
                "File 2 Line 1",
237
                "File 2 Line 2",
238
                "File 2 Line 3",
239
                "File 3 Line 1",
240
                "File 3 Line 2",
241
                "File 3 Line 3",
242
            ],
243
            $file->getContents()
244
        );
245
246
        $exists = $collection->filter(function (FileNodeInterface $item) {
247
            return $item->exists();
248
        });
249
250
        static::assertCount(0, $exists);
251
    }
252
253
    public function testProcessFailedThrowException()
254
    {
255
        $process = m::mock('Symfony\Component\Process\Process')->makePartial();
256
        $this->builder->shouldReceive('build')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Helper\Builder\BuilderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
257
                      ->andReturn($process);
258
259
        $process->shouldReceive('isSuccessful')->andReturn(false);
260
261
        // set exception as no guarantee process will run on local system
262
        $this->expectException(ProcessFailedException::class);
263
264
        $collection = $this->createCollection('simple.merge/', 3);
265
266
        $outputFile = new LocalFile(static::$dir . 'simple.merge.output');
267
268
        $this->merge->contract($collection, $outputFile);
269
    }
270
271 View Code Duplication
    public function testCallingContractWillPassThroughOptions()
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...
272
    {
273
        $collection = $this->createCollection('simple.contract.pass.through/', 3);
274
        $outputFile = new LocalFile(static::$dir . 'simple.contract.pass.through.output');
275
276
        $file = $this->merge->contract(
277
            $collection,
278
            $outputFile,
279
            [
280
                'keepOldFiles' => true,
281
            ]
282
        );
283
284
        static::assertEquals(
285
            [
286
                "File 1 Line 1",
287
                "File 1 Line 2",
288
                "File 1 Line 3",
289
                "File 2 Line 1",
290
                "File 2 Line 2",
291
                "File 2 Line 3",
292
                "File 3 Line 1",
293
                "File 3 Line 2",
294
                "File 3 Line 3",
295
            ],
296
            $file->getContents()
297
        );
298
299
        $exists = $collection->filter(function (FileNodeInterface $item) {
300
            return $item->exists();
301
        });
302
303
        static::assertCount(3, $exists);
304
    }
305
306
    public function testDeleteOldFilesWillDeleteAnyEmptyDirectories()
307
    {
308
        $collection = $this->createCollection('simple.merge.delete.folder/', 3);
309
310
        $outputFile = new LocalFile(static::$dir . 'simple.merge.delete.output');
311
312
        $file = $this->merge->contract($collection, $outputFile, ['keepOldFiles' => false]);
313
314
        static::assertSame($file, $outputFile);
315
        static::assertEquals(
316
            [
317
                "File 1 Line 1",
318
                "File 1 Line 2",
319
                "File 1 Line 3",
320
                "File 2 Line 1",
321
                "File 2 Line 2",
322
                "File 2 Line 3",
323
                "File 3 Line 1",
324
                "File 3 Line 2",
325
                "File 3 Line 3",
326
            ],
327
            $file->getContents()
328
        );
329
330
        static::assertFalse(file_exists($collection->getAll()[0]->getDirectory()));
331
332
        $exists = $collection->filter(function (FileNodeInterface $item) {
333
            return $item->exists();
334
        });
335
336
        static::assertCount(0, $exists);
337
        static::assertTrue($file->exists(), 'output file should exist');
338
    }
339
}
340