Completed
Pull Request — master (#4)
by Harry
03:18
created

testCallingModifyWithANonLocalFileWillThrowAnException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
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 testCallingReplaceTextWithAnArrayAndStringThrowsAnException()
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 . 'multiple_replace_failure.test');
114
        $file->put('some text that text should be replaced');
115
116
        $this->expectException(InvalidArgumentException::class);
117
        $this->replacer->replaceText($file, ['text', 'pants that'], 'pants');
118
    }
119
120 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...
121
    {
122
        $file = new LocalFile(static::$dir . 'postfix_test.test');
123
        $file->put('some text that text should be replaced');
124
125
        $newFile = $this->replacer->replaceText($file, 'text', 'pants', ['postfix' => 'pfixtest']);
126
127
        static::assertNotNull($newFile);
128
        static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename());
129
    }
130
131 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...
132
    {
133
        $file = new LocalFile(static::$dir . 'inline_replace.test');
134
        $file->put('some text that text should be replaced');
135
136
        $newFile = $this->replacer->replaceText($file, 'text', 'pants', ['postfix' => '']);
137
138
        static::assertNotNull($newFile);
139
        static::assertEquals($file->getFilename(), $newFile->getFilename());
140
    }
141
142 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...
143
    {
144
        $file = new LocalFile(static::$dir . 'inline_replace.test');
145
        $file->put('some text that text should be replaced');
146
147
        $newFile = $this->replacer->replaceText($file, 'text', 'pants', ['keepOldFile' => false]);
148
149
        static::assertTrue($newFile->exists());
150
        static::assertFalse($file->exists());
151
    }
152
153 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...
154
    {
155
        $file = new LocalFile(static::$dir . 'simple_replace.test');
156
        $file->put('some text that text should be replaced');
157
158
        $newFile = $this->replacer->modify($file, ['fromText' => 'text', 'toText' => 'pants']);
159
160
        static::assertNotNull($newFile);
161
        static::assertEquals(['some pants that pants should be replaced'], $newFile->getContents());
162
    }
163
164
    public function testCallingModifyWillPassThroughOptions()
165
    {
166
        $file = new LocalFile(static::$dir . 'option_pass_through.test');
167
        $file->put('some text that text should be replaced');
168
169
        $newFile = $this->replacer->modify(
170
            $file,
171
            [
172
                'fromText'    => 'text',
173
                'toText'      => 'pants',
174
                'postfix'     => 'pass',
175
                'keepOldFile' => false,
176
            ]
177
        );
178
179
        static::assertTrue($newFile->exists());
180
        static::assertFalse($file->exists());
181
        static::assertNotNull($newFile);
182
        static::assertEquals('option_pass_through-pass.test', $newFile->getFilename());
183
    }
184
185 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...
186
    {
187
        $this->expectException(InvalidArgumentException::class);
188
189
        $file = new LocalFile(static::$dir . 'simple_replace.test');
190
        $file->put('some text that text should be replaced');
191
192
        $this->replacer->modify($file, ['toText' => 'pants']);
193
    }
194
195 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...
196
    {
197
        $this->expectException(InvalidArgumentException::class);
198
199
        $file = new LocalFile(static::$dir . 'simple_replace.test');
200
        $file->put('some text that text should be replaced');
201
202
        $this->replacer->modify($file, ['fromText' => 'pants']);
203
    }
204
205
    public function testCallingModifyWithANonLocalFileWillThrowAnException()
206
    {
207
        $file = m::mock(FileNodeInterface::class);
208
        $file->shouldReceive('__toString')
209
             ->andReturn('some/file/here');
210
211
        $this->expectException(InvalidArgumentException::class);
212
213
        $this->replacer->modify($file, ['fromText' => 'pants', 'toText' => 'more pants']);
214
    }
215
216 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...
217
    {
218
        $file = new LocalFile(static::$dir . 'file_no_ext');
219
        $file->put('some text that text should be replaced');
220
221
        $newFile = $this->replacer->replaceText($file, 'text', 'pants');
222
223
        static::assertTrue($newFile->exists());
224
        static::assertNotNull($newFile);
225
        static::assertEquals(['some pants that pants should be replaced'], $newFile->getContents());
226
    }
227
228
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding()
229
    {
230
        $process = m::mock(Process::class)->makePartial();
231
        $process->shouldReceive('isSuccessful')->andReturn(false);
232
        $this->processFactory->shouldReceive('createProcess')
233
                             ->andReturn($process);
234
235
        $file = new LocalFile(static::$dir . 'failed_replace_text.test');
236
        $file->put('some text that text should be replaced');
237
238
        $this->expectException(ProcessFailedException::class);
239
240
        $this->replacer->replaceText($file, 'text', 'pants');
241
    }
242
}
243