Completed
Pull Request — master (#3)
by Harry
07:29
created

ReplaceTextTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 206
Duplicated Lines 37.86 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 8
dl 78
loc 206
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testInstanceOf() 0 4 1
A testCanModifyAcceptsLocalFile() 0 15 1
A testReplaceTextReplacesASingleEntry() 0 10 1
A testReplaceTextReplacesMultipleEntries() 0 10 1
A testReplaceTextReplacesMultipleEntriesWorksInCompound() 0 10 1
A testCallingReplaceTextWithArraysThatHaveMismatchedCountsThrowsAnException() 9 9 1
A testAddingAPostfixToTheEndOfTheFile() 10 10 1
A testCallingWithBlankPostfixWillReplaceInLine() 10 10 1
A testSettingKeepOldFileToFalseWillDeleteTheOldFile() 10 10 1
A testCallingModifyReplacesText() 10 10 1
A testCallingModifyWillPassThroughOptions() 0 20 1
A testCallingModifyWithNoFromTextThrowsInvalidArgumentsException() 9 9 1
A testCallingModifyWithNoToTextThrowsInvalidArgumentsException() 9 9 1
A testCallingReplaceTextOnAFileWithoutAnExtensionWorks() 11 11 1
A testCallingModifyWithANonLocalFileWillThrowAnException() 0 10 1
A testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() 0 14 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\ReplaceText;
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
class ReplaceTextTest extends AbstractFileTestCase
29
{
30
    /**
31
     * @var ReplaceText
32
     */
33
    protected $replacer;
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->replacer = new ReplaceText();
44
        $this->replacer->setProcessFactory($this->processFactory);
45
    }
46
47
    public function testInstanceOf()
48
    {
49
        static::assertInstanceOf(FileModifierInterface::class, $this->replacer);
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->replacer->canModify($localFile));
58
        static::assertFalse(
59
            $this->replacer->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->replacer->canModify($randomThing));
66
    }
67
68
    public function testReplaceTextReplacesASingleEntry()
69
    {
70
        $file = new LocalFile(static::$dir . 'simple_replace.test');
71
        $file->put('some text that text should be replaced');
72
73
        $newFile = $this->replacer->replaceText($file, 'text', 'pants');
74
75
        static::assertNotNull($newFile);
76
        static::assertEquals(['some pants that pants should be replaced'], $newFile->getContents());
77
    }
78
79
    public function testReplaceTextReplacesMultipleEntries()
80
    {
81
        $file = new LocalFile(static::$dir . 'multiple_replace.test');
82
        $file->put('some text that text should be replaced');
83
84
        $newFile = $this->replacer->replaceText($file, ['text', 'some'], ['pants', 'many']);
85
86
        static::assertNotNull($newFile);
87
        static::assertEquals(['many pants that pants should be replaced'], $newFile->getContents());
88
    }
89
90
    public function testReplaceTextReplacesMultipleEntriesWorksInCompound()
91
    {
92
        $file = new LocalFile(static::$dir . 'multiple_compound_replace.test');
93
        $file->put('some text that text should be replaced');
94
95
        $newFile = $this->replacer->replaceText($file, ['text', 'pants that'], ['pants', 'fish like']);
96
97
        static::assertNotNull($newFile);
98
        static::assertEquals(['some fish like pants should be replaced'], $newFile->getContents());
99
    }
100
101 View Code Duplication
    public function testCallingReplaceTextWithArraysThatHaveMismatchedCountsThrowsAnException()
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...
102
    {
103
        $file = new LocalFile(static::$dir . 'multiple_replace_failure.test');
104
        $file->put('some text that text should be replaced');
105
106
        $this->expectException(InvalidArgumentException::class);
107
108
        $this->replacer->replaceText($file, ['text', 'pants that'], ['pants']);
109
    }
110
111 View Code Duplication
    public function testAddingAPostfixToTheEndOfTheFile()
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...
112
    {
113
        $file = new LocalFile(static::$dir . 'postfix_test.test');
114
        $file->put('some text that text should be replaced');
115
116
        $newFile = $this->replacer->replaceText($file, 'text', 'pants', ['postfix' => 'pfixtest']);
117
118
        static::assertNotNull($newFile);
119
        static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename());
120
    }
121
122 View Code Duplication
    public function testCallingWithBlankPostfixWillReplaceInLine()
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
        $file = new LocalFile(static::$dir . 'inline_replace.test');
125
        $file->put('some text that text should be replaced');
126
127
        $newFile = $this->replacer->replaceText($file, 'text', 'pants', ['postfix' => '']);
128
129
        static::assertNotNull($newFile);
130
        static::assertEquals($file->getFilename(), $newFile->getFilename());
131
    }
132
133 View Code Duplication
    public function testSettingKeepOldFileToFalseWillDeleteTheOldFile()
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
        $file = new LocalFile(static::$dir . 'inline_replace.test');
136
        $file->put('some text that text should be replaced');
137
138
        $newFile = $this->replacer->replaceText($file, 'text', 'pants', ['keepOldFile' => false]);
139
140
        static::assertTrue($newFile->exists());
141
        static::assertFalse($file->exists());
142
    }
143
144 View Code Duplication
    public function testCallingModifyReplacesText()
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...
145
    {
146
        $file = new LocalFile(static::$dir . 'simple_replace.test');
147
        $file->put('some text that text should be replaced');
148
149
        $newFile = $this->replacer->modify($file, ['fromText' => 'text', 'toText' => 'pants']);
150
151
        static::assertNotNull($newFile);
152
        static::assertEquals(['some pants that pants should be replaced'], $newFile->getContents());
153
    }
154
155
    public function testCallingModifyWillPassThroughOptions()
156
    {
157
        $file = new LocalFile(static::$dir . 'option_pass_through.test');
158
        $file->put('some text that text should be replaced');
159
160
        $newFile = $this->replacer->modify(
161
            $file,
162
            [
163
                'fromText'    => 'text',
164
                'toText'      => 'pants',
165
                'postfix'     => 'pass',
166
                'keepOldFile' => false,
167
            ]
168
        );
169
170
        static::assertTrue($newFile->exists());
171
        static::assertFalse($file->exists());
172
        static::assertNotNull($newFile);
173
        static::assertEquals('option_pass_through-pass.test', $newFile->getFilename());
174
    }
175
176 View Code Duplication
    public function testCallingModifyWithNoFromTextThrowsInvalidArgumentsException()
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...
177
    {
178
        $this->expectException(InvalidArgumentException::class);
179
180
        $file = new LocalFile(static::$dir . 'simple_replace.test');
181
        $file->put('some text that text should be replaced');
182
183
        $this->replacer->modify($file, ['toText' => 'pants']);
184
    }
185
186 View Code Duplication
    public function testCallingModifyWithNoToTextThrowsInvalidArgumentsException()
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...
187
    {
188
        $this->expectException(InvalidArgumentException::class);
189
190
        $file = new LocalFile(static::$dir . 'simple_replace.test');
191
        $file->put('some text that text should be replaced');
192
193
        $this->replacer->modify($file, ['fromText' => 'pants']);
194
    }
195
196
    public function testCallingModifyWithANonLocalFileWillThrowAnException()
197
    {
198
        $file = m::mock(FileNodeInterface::class);
199
        $file->shouldReceive('__toString')
200
             ->andReturn('some/file/here');
201
202
        $this->expectException(InvalidArgumentException::class);
203
204
        $this->replacer->modify($file, ['fromText' => 'pants', 'toText' => 'more pants']);
205
    }
206
207 View Code Duplication
    public function testCallingReplaceTextOnAFileWithoutAnExtensionWorks()
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...
208
    {
209
        $file = new LocalFile(static::$dir . 'file_no_ext');
210
        $file->put('some text that text should be replaced');
211
212
        $newFile = $this->replacer->replaceText($file, 'text', 'pants');
213
214
        static::assertTrue($newFile->exists());
215
        static::assertNotNull($newFile);
216
        static::assertEquals(['some pants that pants should be replaced'], $newFile->getContents());
217
    }
218
219
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding()
220
    {
221
        $process = m::mock(Process::class)->makePartial();
222
        $process->shouldReceive('isSuccessful')->andReturn(false);
223
        $this->processFactory->shouldReceive('createProcess')
224
                             ->andReturn($process);
225
226
        $file = new LocalFile(static::$dir . 'failed_replace_text.test');
227
        $file->put('some text that text should be replaced');
228
229
        $this->expectException(ProcessFailedException::class);
230
231
        $this->replacer->replaceText($file, 'text', 'pants');
232
    }
233
}
234