Completed
Push — master ( 857202...d87b02 )
by Harry
03:21
created

  A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

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