testCallingModifyWillPassThroughOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 1
eloc 13
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\Builder\Builder;
17
use Graze\DataFile\Helper\Builder\BuilderInterface;
18
use Graze\DataFile\Helper\Process\ProcessFactory;
19
use Graze\DataFile\Modify\FileModifierInterface;
20
use Graze\DataFile\Modify\ReplaceText;
21
use Graze\DataFile\Node\FileNodeInterface;
22
use Graze\DataFile\Node\LocalFile;
23
use Graze\DataFile\Test\AbstractFileTestCase;
24
use InvalidArgumentException;
25
use Mockery as m;
26
use Mockery\MockInterface;
27
use Symfony\Component\Process\Exception\ProcessFailedException;
28
use Symfony\Component\Process\Process;
29
30
class ReplaceTextTest extends AbstractFileTestCase
31
{
32
    /**
33
     * @var ReplaceText
34
     */
35
    protected $replacer;
36
37
    /**
38
     * @var BuilderInterface|MockInterface
39
     */
40
    protected $builder;
41
42
    public function setUp()
43
    {
44
        $this->builder = m::mock(Builder::class)->makePartial();
45
        $this->replacer = new ReplaceText();
46
        $this->replacer->setBuilder($this->builder);
0 ignored issues
show
Documentation introduced by
$this->builder is of type object<Mockery\Mock>, but the function expects a object<Graze\DataFile\He...ilder\BuilderInterface>.

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...
47
    }
48
49
    public function testInstanceOf()
50
    {
51
        static::assertInstanceOf(FileModifierInterface::class, $this->replacer);
52
    }
53
54
    public function testCanModifyAcceptsLocalFile()
55
    {
56
        $localFile = m::mock(LocalFile::class);
57
        $localFile->shouldReceive('exists')->andReturn(true, false);
58
59
        static::assertTrue($this->replacer->canModify($localFile));
60
        static::assertFalse(
61
            $this->replacer->canModify($localFile),
62
            "CanExtend should return false if the file does not exist"
63
        );
64
65
        $randomThing = m::mock(FileNodeInterface::class);
66
67
        static::assertFalse($this->replacer->canModify($randomThing));
68
    }
69
70
    public function testReplaceTextReplacesASingleEntry()
71
    {
72
        $file = new LocalFile(static::$dir . 'simple_replace.test');
73
        $file->put('some text that text should be replaced');
74
75
        $newFile = $this->replacer->replaceText($file, 'text', 'pants');
76
77
        static::assertNotNull($newFile);
78
        static::assertEquals(['some pants that pants should be replaced'], $newFile->getContents());
79
    }
80
81
    public function testReplaceTextReplacesMultipleEntries()
82
    {
83
        $file = new LocalFile(static::$dir . 'multiple_replace.test');
84
        $file->put('some text that text should be replaced');
85
86
        $newFile = $this->replacer->replaceText($file, ['text', 'some'], ['pants', 'many']);
87
88
        static::assertNotNull($newFile);
89
        static::assertEquals(['many pants that pants should be replaced'], $newFile->getContents());
90
    }
91
92
    public function testReplaceTextReplacesMultipleEntriesWorksInCompound()
93
    {
94
        $file = new LocalFile(static::$dir . 'multiple_compound_replace.test');
95
        $file->put('some text that text should be replaced');
96
97
        $newFile = $this->replacer->replaceText($file, ['text', 'pants that'], ['pants', 'fish like']);
98
99
        static::assertNotNull($newFile);
100
        static::assertEquals(['some fish like pants should be replaced'], $newFile->getContents());
101
    }
102
103 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...
104
    {
105
        $file = new LocalFile(static::$dir . 'multiple_replace_failure.test');
106
        $file->put('some text that text should be replaced');
107
108
        $this->expectException(InvalidArgumentException::class);
109
110
        $this->replacer->replaceText($file, ['text', 'pants that'], ['pants']);
111
    }
112
113 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...
114
    {
115
        $file = new LocalFile(static::$dir . 'multiple_replace_failure.test');
116
        $file->put('some text that text should be replaced');
117
118
        $this->expectException(InvalidArgumentException::class);
119
        $this->replacer->replaceText($file, ['text', 'pants that'], 'pants');
120
    }
121
122 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...
123
    {
124
        $file = new LocalFile(static::$dir . 'postfix_test.test');
125
        $file->put('some text that text should be replaced');
126
127
        $newFile = $this->replacer->replaceText($file, 'text', 'pants', ['postfix' => 'pfixtest']);
128
129
        static::assertNotNull($newFile);
130
        static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename());
131
    }
132
133 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...
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', ['postfix' => '']);
139
140
        static::assertNotNull($newFile);
141
        static::assertEquals($file->getFilename(), $newFile->getFilename());
142
    }
143
144 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...
145
    {
146
        $file = new LocalFile(static::$dir . 'inline_replace.test');
147
        $file->put('some text that text should be replaced');
148
149
        $newFile = $this->replacer->replaceText($file, 'text', 'pants', ['keepOldFile' => false]);
150
151
        static::assertTrue($newFile->exists());
152
        static::assertFalse($file->exists());
153
    }
154
155 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...
156
    {
157
        $file = new LocalFile(static::$dir . 'simple_replace.test');
158
        $file->put('some text that text should be replaced');
159
160
        $newFile = $this->replacer->modify($file, ['fromText' => 'text', 'toText' => 'pants']);
161
162
        static::assertNotNull($newFile);
163
        static::assertEquals(['some pants that pants should be replaced'], $newFile->getContents());
164
    }
165
166
    public function testCallingModifyWillPassThroughOptions()
167
    {
168
        $file = new LocalFile(static::$dir . 'option_pass_through.test');
169
        $file->put('some text that text should be replaced');
170
171
        $newFile = $this->replacer->modify(
172
            $file,
173
            [
174
                'fromText'    => 'text',
175
                'toText'      => 'pants',
176
                'postfix'     => 'pass',
177
                'keepOldFile' => false,
178
            ]
179
        );
180
181
        static::assertTrue($newFile->exists());
182
        static::assertFalse($file->exists());
183
        static::assertNotNull($newFile);
184
        static::assertEquals('option_pass_through-pass.test', $newFile->getFilename());
185
    }
186
187 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...
188
    {
189
        $this->expectException(InvalidArgumentException::class);
190
191
        $file = new LocalFile(static::$dir . 'simple_replace.test');
192
        $file->put('some text that text should be replaced');
193
194
        $this->replacer->modify($file, ['toText' => 'pants']);
195
    }
196
197 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...
198
    {
199
        $this->expectException(InvalidArgumentException::class);
200
201
        $file = new LocalFile(static::$dir . 'simple_replace.test');
202
        $file->put('some text that text should be replaced');
203
204
        $this->replacer->modify($file, ['fromText' => 'pants']);
205
    }
206
207
    public function testCallingModifyWithANonLocalFileWillThrowAnException()
208
    {
209
        $file = m::mock(FileNodeInterface::class);
210
        $file->shouldReceive('__toString')
211
             ->andReturn('some/file/here');
212
213
        $this->expectException(InvalidArgumentException::class);
214
215
        $this->replacer->modify($file, ['fromText' => 'pants', 'toText' => 'more pants']);
216
    }
217
218 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...
219
    {
220
        $file = new LocalFile(static::$dir . 'file_no_ext');
221
        $file->put('some text that text should be replaced');
222
223
        $newFile = $this->replacer->replaceText($file, 'text', 'pants');
224
225
        static::assertTrue($newFile->exists());
226
        static::assertNotNull($newFile);
227
        static::assertEquals(['some pants that pants should be replaced'], $newFile->getContents());
228
    }
229
230
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding()
231
    {
232
        $process = m::mock(Process::class)->makePartial();
233
        $process->shouldReceive('isSuccessful')->andReturn(false);
234
        $this->builder->shouldReceive('build')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Helper\Builder\BuilderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
235
                      ->andReturn($process);
236
237
        $file = new LocalFile(static::$dir . 'failed_replace_text.test');
238
        $file->put('some text that text should be replaced');
239
240
        $this->expectException(ProcessFailedException::class);
241
242
        $this->replacer->replaceText($file, 'text', 'pants');
243
    }
244
}
245