Completed
Push — master ( 9bc3b7...a15851 )
by Harry
02:46
created

MergeFilesTest::testInstanceOf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Graze\DataFile\Test\Integration\Modify\Contract;
4
5
use Graze\DataFile\Helper\Process\ProcessFactory;
6
use Graze\DataFile\Modify\Compress\CompressionType;
7
use Graze\DataFile\Modify\Contract\FileContractorInterface;
8
use Graze\DataFile\Modify\Contract\MergeFiles;
9
use Graze\DataFile\Modify\MakeDirectory;
10
use Graze\DataFile\Node\FileNodeCollection;
11
use Graze\DataFile\Node\FileNodeCollectionInterface;
12
use Graze\DataFile\Node\FileNodeInterface;
13
use Graze\DataFile\Node\LocalFile;
14
use Graze\DataFile\Test\FileTestCase;
15
use InvalidArgumentException;
16
use Mockery as m;
17
use Symfony\Component\Process\Exception\ProcessFailedException;
18
19
class MergeFilesTest extends FileTestCase
20
{
21
    /**
22
     * @var ProcessFactory|m\MockInterface
23
     */
24
    protected $processFactory;
25
26
    /**
27
     * @var MergeFiles
28
     */
29
    private $merge;
30
31
    public function setUp()
32
    {
33
        $this->processFactory = m::mock(ProcessFactory::class)->makePartial();
34
        $this->merge = new MergeFiles();
35
        $this->merge->setProcessFactory($this->processFactory);
1 ignored issue
show
Documentation introduced by
$this->processFactory is of type object<Mockery\Mock>, but the function expects a object<Graze\DataFile\He...Process\ProcessFactory>.

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...
36
    }
37
38
    public function testInstanceOf()
39
    {
40
        static::assertInstanceOf(FileContractorInterface::class, $this->merge);
41
    }
42
43
    public function testCanContractAcceptsFileNodeCollectionInterface()
44
    {
45
        $collection = m::mock(FileNodeCollectionInterface::class);
46
        $collection->shouldReceive('getIterator')
47
                   ->andReturn([]);
48
49
        $node = m::mock(FileNodeInterface::class);
50
        $merger = new MergeFiles($this->processFactory, $node);
0 ignored issues
show
Unused Code introduced by
The call to MergeFiles::__construct() has too many arguments starting with $this->processFactory.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
51
        static::assertTrue($merger->canContract($collection));
52
    }
53
54 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...
55
    {
56
        $collection = new FileNodeCollection();
57
        $file1 = m::mock(LocalFile::class);
58
        $file1->shouldReceive('exists')
59
              ->andReturn(true);
60
        $file1->shouldReceive('getCompression')
61
              ->andReturn(CompressionType::NONE);
62
        $collection->add($file1);
63
64
        static::assertTrue($this->merge->canContract($collection));
65
66
        $file2 = m::mock(FileNodeInterface::class);
67
        $file2->shouldReceive('getCompression')
68
              ->andReturn(CompressionType::NONE);
69
        $collection->add($file2);
70
71
        static::assertFalse($this->merge->canContract($collection));
72
    }
73
74 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...
75
    {
76
        $collection = new FileNodeCollection();
77
        $file1 = m::mock(LocalFile::class);
78
        $file1->shouldReceive('exists')
79
              ->andReturn(true);
80
        $file1->shouldReceive('getCompression')
81
              ->andReturn(CompressionType::NONE);
82
        $collection->add($file1);
83
84
        static::assertTrue($this->merge->canContract($collection));
85
86
        $file2 = m::mock(LocalFile::class);
87
        $file2->shouldReceive('exists')
88
              ->andReturn(false);
89
        $file2->shouldReceive('getCompression')
90
              ->andReturn(CompressionType::NONE);
91
        $collection->add($file2);
92
93
        static::assertFalse($this->merge->canContract($collection));
94
    }
95
96 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...
97
    {
98
        $collection = new FileNodeCollection();
99
        $file1 = m::mock(LocalFile::class);
100
        $file1->shouldReceive('exists')
101
              ->andReturn(true);
102
        $file1->shouldReceive('getCompression')
103
              ->andReturn(CompressionType::NONE);
104
        $collection->add($file1);
105
106
        static::assertTrue($this->merge->canContract($collection));
107
108
        $file2 = m::mock(LocalFile::class);
109
        $file2->shouldReceive('exists')
110
              ->andReturn(true);
111
        $file2->shouldReceive('getCompression')
112
              ->andReturn(CompressionType::GZIP);
113
        $collection->add($file2);
114
115
        static::assertFalse($this->merge->canContract($collection));
116
    }
117
118 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...
119
    {
120
        $collection = new FileNodeCollection();
121
        $file = m::mock(LocalFile::class);
122
        $file->shouldReceive('exists')
123
             ->andReturn(false);
124
        $file->shouldReceive('getCompression')
125
             ->andReturn(CompressionType::NONE);
126
        $collection->add($file);
127
128
        $target = m::mock(LocalFile::class);
129
130
        $this->expectException(InvalidArgumentException::class);
131
132
        $this->merge->contract($collection, $target);
133
    }
134
135 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...
136
    {
137
        $collection = new FileNodeCollection();
138
        $file = m::mock(LocalFile::class);
139
        $file->shouldReceive('exists')
140
             ->andReturn(true);
141
        $file->shouldReceive('getCompression')
142
             ->andReturn(CompressionType::NONE);
143
        $collection->add($file);
144
145
        $target = m::mock(FileNodeInterface::class);
146
147
        $this->expectException(InvalidArgumentException::class);
148
149
        $this->merge->contract($collection, $target);
150
    }
151
152 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...
153
    {
154
        $collection = $this->createCollection('simple.merge/', 3);
155
156
        $outputFile = new LocalFile(static::$dir . 'simple.merge.output');
157
158
        $file = $this->merge->contract($collection, $outputFile);
159
160
        static::assertSame($file, $outputFile);
161
        static::assertEquals(
162
            [
163
                "File 1 Line 1",
164
                "File 1 Line 2",
165
                "File 1 Line 3",
166
                "File 2 Line 1",
167
                "File 2 Line 2",
168
                "File 2 Line 3",
169
                "File 3 Line 1",
170
                "File 3 Line 2",
171
                "File 3 Line 3",
172
            ],
173
            $file->getContents()
174
        );
175
176
        $exists = $collection->filter(function (FileNodeInterface $item) {
177
            return $item->exists();
178
        });
179
180
        static::assertCount(3, $exists);
181
    }
182
183
    /**
184
     * @param string $rootDir
185
     * @param int    $numFiles
186
     *
187
     * @return FileNodeCollectionInterface
188
     */
189
    private function createCollection($rootDir, $numFiles)
190
    {
191
        $mkDir = new MakeDirectory();
192
        $collection = new FileNodeCollection();
193
        for ($i = 1; $i <= $numFiles; $i++) {
194
            $file = new LocalFile(static::$dir . $rootDir . 'part_' . $i);
195
            $mkDir->makeDirectory($file);
196
            $file->put("File $i Line 1\nFile $i Line 2\nFile $i Line 3\n");
197
            $collection->add($file);
198
        }
199
        return $collection;
200
    }
201
202 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...
203
    {
204
        $collection = $this->createCollection('simple.merge.delete/', 3);
205
206
        $outputFile = new LocalFile(static::$dir . 'simple.merge.delete.output');
207
208
        $file = $this->merge->contract($collection, $outputFile, ['keepOldFiles' => false]);
209
210
        static::assertSame($file, $outputFile);
211
        static::assertEquals(
212
            [
213
                "File 1 Line 1",
214
                "File 1 Line 2",
215
                "File 1 Line 3",
216
                "File 2 Line 1",
217
                "File 2 Line 2",
218
                "File 2 Line 3",
219
                "File 3 Line 1",
220
                "File 3 Line 2",
221
                "File 3 Line 3",
222
            ],
223
            $file->getContents()
224
        );
225
226
        $exists = $collection->filter(function (FileNodeInterface $item) {
227
            return $item->exists();
228
        });
229
230
        static::assertCount(0, $exists);
231
    }
232
233 View Code Duplication
    public function testProcessFailedThrowException()
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...
234
    {
235
        $process = m::mock('Symfony\Component\Process\Process')->makePartial();
236
        $this->processFactory->shouldReceive('createProcess')
237
                             ->andReturn($process);
238
239
        $process->shouldReceive('isSuccessful')->andReturn(false);
240
241
        // set exception as no guarantee process will run on local system
242
        $this->expectException(ProcessFailedException::class);
243
244
        $collection = $this->createCollection('simple.merge/', 3);
245
246
        $outputFile = new LocalFile(static::$dir . 'simple.merge.output');
247
248
        $this->merge->contract($collection, $outputFile);
249
    }
250
251 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...
252
    {
253
        $collection = $this->createCollection('simple.contract.pass.through/', 3);
254
        $outputFile = new LocalFile(static::$dir . 'simple.contract.pass.through.output');
255
256
        $file = $this->merge->contract(
257
            $collection,
258
            $outputFile,
259
            [
260
                'keepOldFiles' => true,
261
            ]
262
        );
263
264
        static::assertEquals(
265
            [
266
                "File 1 Line 1",
267
                "File 1 Line 2",
268
                "File 1 Line 3",
269
                "File 2 Line 1",
270
                "File 2 Line 2",
271
                "File 2 Line 3",
272
                "File 3 Line 1",
273
                "File 3 Line 2",
274
                "File 3 Line 3",
275
            ],
276
            $file->getContents()
277
        );
278
279
        $exists = $collection->filter(function (FileNodeInterface $item) {
280
            return $item->exists();
281
        });
282
283
        static::assertCount(3, $exists);
284
    }
285
286
    public function testDeleteOldFilesWillDeleteAnyEmptyDirectories()
287
    {
288
        $collection = $this->createCollection('simple.merge.delete.folder/', 3);
289
290
        $outputFile = new LocalFile(static::$dir . 'simple.merge.delete.output');
291
292
        $file = $this->merge->contract($collection, $outputFile, ['keepOldFiles' => false]);
293
294
        static::assertSame($file, $outputFile);
295
        static::assertEquals(
296
            [
297
                "File 1 Line 1",
298
                "File 1 Line 2",
299
                "File 1 Line 3",
300
                "File 2 Line 1",
301
                "File 2 Line 2",
302
                "File 2 Line 3",
303
                "File 3 Line 1",
304
                "File 3 Line 2",
305
                "File 3 Line 3",
306
            ],
307
            $file->getContents()
308
        );
309
310
        static::assertFalse(file_exists($collection->getAll()[0]->getDirectory()));
311
312
        $exists = $collection->filter(function (FileNodeInterface $item) {
313
            return $item->exists();
314
        });
315
316
        static::assertCount(0, $exists);
317
        static::assertTrue($file->exists(), 'output file should exist');
318
    }
319
}
320