Completed
Pull Request — master (#3)
by Harry
05:31
created

TailTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 216
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 8
dl 216
loc 216
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testCallingModifyWithANonLocalFileWillThrowAnException() 10 10 1
A testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() 14 14 1
B testBasicReadingTheLastNLines() 27 27 1
A setUp() 6 6 1
A testInstanceOf() 4 4 1
A testCanModifyAcceptsLocalFile() 15 15 1
A createFile() 19 19 1
A testOutputLinesStartingFromN() 21 21 1
A testAddingAPostfixToTheEndOfTheFile() 9 9 1
A testCallingWithBlankPostfixWillReplaceInLine() 9 9 1
A testSettingKeepOldFileToFalseWillDeleteTheOldFile() 9 9 1
A testCallingModifyDoesTail() 16 16 1
A testCallingModifyWillPassThroughOptions() 18 18 1
A testCallingModifyWithoutLinesWillThrowAnException() 8 8 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;
15
16
use Graze\DataFile\Helper\Process\ProcessFactory;
17
use Graze\DataFile\Modify\FileModifierInterface;
18
use Graze\DataFile\Modify\Tail;
19
use Graze\DataFile\Node\FileNodeInterface;
20
use Graze\DataFile\Node\LocalFile;
21
use Graze\DataFile\Test\AbstractFileTestCase;
22
use InvalidArgumentException;
23
use Mockery as m;
24
use Mockery\MockInterface;
25
use Symfony\Component\Process\Exception\ProcessFailedException;
26
use Symfony\Component\Process\Process;
27
28 View Code Duplication
class TailTest extends AbstractFileTestCase
0 ignored issues
show
Duplication introduced by
This class 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...
29
{
30
    /**
31
     * @var Tail
32
     */
33
    protected $tail;
34
35
    /**
36
     * @var ProcessFactory|MockInterface
37
     */
38
    protected $processFactory;
39
40
    public function setUp()
41
    {
42
        $this->processFactory = m::mock(ProcessFactory::class)->makePartial();
43
        $this->tail = new Tail();
44
        $this->tail->setProcessFactory($this->processFactory);
45
    }
46
47
    public function testInstanceOf()
48
    {
49
        static::assertInstanceOf(FileModifierInterface::class, $this->tail);
50
    }
51
52
    public function testCanModifyAcceptsLocalFile()
53
    {
54
        $localFile = m::mock(LocalFile::class);
55
        $localFile->shouldReceive('exists')->andReturn(true, false);
56
57
        static::assertTrue($this->tail->canModify($localFile));
58
        static::assertFalse(
59
            $this->tail->canModify($localFile),
60
            "CanExtend should return false if the file does not exist"
61
        );
62
63
        $randomThing = m::mock(FileNodeInterface::class);
64
65
        static::assertFalse($this->tail->canModify($randomThing));
66
    }
67
68
    public function testBasicReadingTheLastNLines()
69
    {
70
        $file = $this->createFile('last_five_lines');
71
72
        $newFile = $this->tail->tail($file, 5);
73
74
        static::assertEquals(
75
            [
76
                "Line 6",
77
                "Line 7",
78
                "Line 8",
79
                "Line 9",
80
                "Line 10",
81
            ],
82
            $newFile->getContents()
83
        );
84
85
        $newFile = $this->tail->tail($file, 2);
86
87
        static::assertEquals(
88
            [
89
                "Line 9",
90
                "Line 10",
91
            ],
92
            $newFile->getContents()
93
        );
94
    }
95
96
    /**
97
     * @param string $path
98
     *
99
     * @return LocalFile
100
     */
101
    private function createFile($path)
102
    {
103
        $file = new LocalFile(static::$dir . $path);
104
        $file->put(
105
            "Line 1
106
Line 2
107
Line 3
108
Line 4
109
Line 5
110
Line 6
111
Line 7
112
Line 8
113
Line 9
114
Line 10
115
"
116
        );
117
118
        return $file;
119
    }
120
121
    public function testOutputLinesStartingFromN()
122
    {
123
        $file = $this->createFile('from.second.line.onwards');
124
125
        $newFile = $this->tail->tail($file, '+2');
126
127
        static::assertEquals(
128
            [
129
                "Line 2",
130
                "Line 3",
131
                "Line 4",
132
                "Line 5",
133
                "Line 6",
134
                "Line 7",
135
                "Line 8",
136
                "Line 9",
137
                "Line 10",
138
            ],
139
            $newFile->getContents()
140
        );
141
    }
142
143
    public function testAddingAPostfixToTheEndOfTheFile()
144
    {
145
        $file = $this->createFile('postfix_test.test');
146
147
        $newFile = $this->tail->tail($file, 4, ['postfix' => 'pfixtest']);
148
149
        static::assertNotNull($newFile);
150
        static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename());
151
    }
152
153
    public function testCallingWithBlankPostfixWillReplaceInLine()
154
    {
155
        $file = $this->createFile('inline_tail.test');
156
157
        $newFile = $this->tail->tail($file, 2, ['postfix' => '']);
158
159
        static::assertNotNull($newFile);
160
        static::assertEquals($file->getFilename(), $newFile->getFilename());
161
    }
162
163
    public function testSettingKeepOldFileToFalseWillDeleteTheOldFile()
164
    {
165
        $file = $this->createFile('inline_replace.test');
166
167
        $newFile = $this->tail->tail($file, 5, ['keepOldFile' => false]);
168
169
        static::assertTrue($newFile->exists());
170
        static::assertFalse($file->exists());
171
    }
172
173
    public function testCallingModifyDoesTail()
174
    {
175
        $file = $this->createFile('simple_tail.test');
176
177
        $newFile = $this->tail->modify($file, ['lines' => 4]);
178
179
        static::assertEquals(
180
            [
181
                "Line 7",
182
                "Line 8",
183
                "Line 9",
184
                "Line 10",
185
            ],
186
            $newFile->getContents()
187
        );
188
    }
189
190
    public function testCallingModifyWillPassThroughOptions()
191
    {
192
        $file = $this->createFile('option_pass_through.test');
193
194
        $newFile = $this->tail->modify(
195
            $file,
196
            [
197
                'lines'       => 2,
198
                'postfix'     => 'pass',
199
                'keepOldFile' => false,
200
            ]
201
        );
202
203
        static::assertTrue($newFile->exists());
204
        static::assertFalse($file->exists());
205
        static::assertNotNull($newFile);
206
        static::assertEquals('option_pass_through-pass.test', $newFile->getFilename());
207
    }
208
209
    public function testCallingModifyWithoutLinesWillThrowAnException()
210
    {
211
        $file = $this->createFile('option_pass_through.test');
212
213
        $this->expectException(InvalidArgumentException::class);
214
215
        $this->tail->modify($file);
216
    }
217
218
    public function testCallingModifyWithANonLocalFileWillThrowAnException()
219
    {
220
        $file = m::mock(FileNodeInterface::class);
221
        $file->shouldReceive('__toString')
222
             ->andReturn('some/file/here');
223
224
        $this->expectException(InvalidArgumentException::class);
225
226
        $this->tail->modify($file, ['lines' => 1]);
227
    }
228
229
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding()
230
    {
231
        $process = m::mock(Process::class)->makePartial();
232
        $process->shouldReceive('isSuccessful')->andReturn(false);
233
        $this->processFactory->shouldReceive('createProcess')
234
                             ->andReturn($process);
235
236
        $file = new LocalFile(static::$dir . 'failed_tail.test');
237
        $file->put('nothing interesting here');
238
239
        $this->expectException(ProcessFailedException::class);
240
241
        $this->tail->tail($file, 3);
242
    }
243
}
244