HeadTest::createFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 5

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 19
loc 19
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
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\Head;
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 View Code Duplication
class HeadTest 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...
31
{
32
    /**
33
     * @var Head
34
     */
35
    protected $head;
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->head = new Head();
46
        $this->head->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->head);
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->head->canModify($localFile));
60
        static::assertFalse(
61
            $this->head->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->head->canModify($randomThing));
68
    }
69
70
    public function testBasicReadingTheFirstNLines()
71
    {
72
        $file = $this->createFile('first_five_lines');
73
74
        $newFile = $this->head->head($file, 5);
75
76
        static::assertEquals(
77
            [
78
                "Line 1",
79
                "Line 2",
80
                "Line 3",
81
                "Line 4",
82
                "Line 5",
83
            ],
84
            $newFile->getContents()
85
        );
86
87
        $newFile = $this->head->head($file, 2);
88
89
        static::assertEquals(
90
            [
91
                "Line 1",
92
                "Line 2",
93
            ],
94
            $newFile->getContents()
95
        );
96
    }
97
98
    /**
99
     * @param string $path
100
     *
101
     * @return LocalFile
102
     */
103
    private function createFile($path)
104
    {
105
        $file = new LocalFile(static::$dir . $path);
106
        $file->put(
107
            "Line 1
108
Line 2
109
Line 3
110
Line 4
111
Line 5
112
Line 6
113
Line 7
114
Line 8
115
Line 9
116
Line 10
117
"
118
        );
119
120
        return $file;
121
    }
122
123
    public function testOutputLinesUpToN()
124
    {
125
        $file = $this->createFile('from.second.line.onwards');
126
127
        $newFile = $this->head->head($file, '-2');
128
129
        static::assertEquals(
130
            [
131
                "Line 1",
132
                "Line 2",
133
                "Line 3",
134
                "Line 4",
135
                "Line 5",
136
                "Line 6",
137
                "Line 7",
138
                "Line 8",
139
            ],
140
            $newFile->getContents()
141
        );
142
    }
143
144
    public function testAddingAPostfixToTheEndOfTheFile()
145
    {
146
        $file = $this->createFile('postfix_test.test');
147
148
        $newFile = $this->head->head($file, 4, ['postfix' => 'pfixtest']);
149
150
        static::assertNotNull($newFile);
151
        static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename());
152
    }
153
154
    public function testCallingWithBlankPostfixWillReplaceInLine()
155
    {
156
        $file = $this->createFile('inline_tail.test');
157
158
        $newFile = $this->head->head($file, 2, ['postfix' => '']);
159
160
        static::assertNotNull($newFile);
161
        static::assertEquals($file->getFilename(), $newFile->getFilename());
162
    }
163
164
    public function testSettingKeepOldFileToFalseWillDeleteTheOldFile()
165
    {
166
        $file = $this->createFile('inline_replace.test');
167
168
        $newFile = $this->head->head($file, 5, ['keepOldFile' => false]);
169
170
        static::assertTrue($newFile->exists());
171
        static::assertFalse($file->exists());
172
    }
173
174
    public function testCallingModifyDoesTail()
175
    {
176
        $file = $this->createFile('simple_tail.test');
177
178
        $newFile = $this->head->modify($file, ['lines' => 4]);
179
180
        static::assertEquals(
181
            [
182
                "Line 1",
183
                "Line 2",
184
                "Line 3",
185
                "Line 4",
186
            ],
187
            $newFile->getContents()
188
        );
189
    }
190
191
    public function testCallingModifyWillPassThroughOptions()
192
    {
193
        $file = $this->createFile('option_pass_through.test');
194
195
        $newFile = $this->head->modify(
196
            $file,
197
            [
198
                'lines'       => 2,
199
                'postfix'     => 'pass',
200
                'keepOldFile' => false,
201
            ]
202
        );
203
204
        static::assertTrue($newFile->exists());
205
        static::assertFalse($file->exists());
206
        static::assertNotNull($newFile);
207
        static::assertEquals('option_pass_through-pass.test', $newFile->getFilename());
208
    }
209
210
    public function testCallingModifyWithoutLinesWillThrowAnException()
211
    {
212
        $file = $this->createFile('option_pass_through.test');
213
214
        $this->expectException(InvalidArgumentException::class);
215
216
        $this->head->modify($file);
217
    }
218
219
    public function testCallingModifyWithANonLocalFileWillThrowAnException()
220
    {
221
        $file = m::mock(FileNodeInterface::class);
222
        $file->shouldReceive('__toString')
223
             ->andReturn('some/file/here');
224
225
        $this->expectException(InvalidArgumentException::class);
226
227
        $this->head->modify($file, ['lines' => 1]);
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_tail.test');
238
        $file->put('nothing interesting here');
239
240
        $this->expectException(ProcessFailedException::class);
241
242
        $this->head->head($file, 3);
243
    }
244
}
245