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

HeadTest::testBasicReadingTheFirstNLines()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 27
Ratio 100 %
Metric Value
dl 27
loc 27
rs 8.8571
cc 1
eloc 15
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\Head;
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 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...
29
{
30
    /**
31
     * @var Head
32
     */
33
    protected $head;
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->head = new Head();
44
        $this->head->setProcessFactory($this->processFactory);
45
    }
46
47
    public function testInstanceOf()
48
    {
49
        static::assertInstanceOf(FileModifierInterface::class, $this->head);
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->head->canModify($localFile));
58
        static::assertFalse(
59
            $this->head->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->head->canModify($randomThing));
66
    }
67
68
    public function testBasicReadingTheFirstNLines()
69
    {
70
        $file = $this->createFile('first_five_lines');
71
72
        $newFile = $this->head->head($file, 5);
73
74
        static::assertEquals(
75
            [
76
                "Line 1",
77
                "Line 2",
78
                "Line 3",
79
                "Line 4",
80
                "Line 5",
81
            ],
82
            $newFile->getContents()
83
        );
84
85
        $newFile = $this->head->head($file, 2);
86
87
        static::assertEquals(
88
            [
89
                "Line 1",
90
                "Line 2",
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 testOutputLinesUpToN()
122
    {
123
        $file = $this->createFile('from.second.line.onwards');
124
125
        $newFile = $this->head->head($file, '-2');
126
127
        static::assertEquals(
128
            [
129
                "Line 1",
130
                "Line 2",
131
                "Line 3",
132
                "Line 4",
133
                "Line 5",
134
                "Line 6",
135
                "Line 7",
136
                "Line 8",
137
            ],
138
            $newFile->getContents()
139
        );
140
    }
141
142
    public function testAddingAPostfixToTheEndOfTheFile()
143
    {
144
        $file = $this->createFile('postfix_test.test');
145
146
        $newFile = $this->head->head($file, 4, ['postfix' => 'pfixtest']);
147
148
        static::assertNotNull($newFile);
149
        static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename());
150
    }
151
152
    public function testCallingWithBlankPostfixWillReplaceInLine()
153
    {
154
        $file = $this->createFile('inline_tail.test');
155
156
        $newFile = $this->head->head($file, 2, ['postfix' => '']);
157
158
        static::assertNotNull($newFile);
159
        static::assertEquals($file->getFilename(), $newFile->getFilename());
160
    }
161
162
    public function testSettingKeepOldFileToFalseWillDeleteTheOldFile()
163
    {
164
        $file = $this->createFile('inline_replace.test');
165
166
        $newFile = $this->head->head($file, 5, ['keepOldFile' => false]);
167
168
        static::assertTrue($newFile->exists());
169
        static::assertFalse($file->exists());
170
    }
171
172
    public function testCallingModifyDoesTail()
173
    {
174
        $file = $this->createFile('simple_tail.test');
175
176
        $newFile = $this->head->modify($file, ['lines' => 4]);
177
178
        static::assertEquals(
179
            [
180
                "Line 1",
181
                "Line 2",
182
                "Line 3",
183
                "Line 4",
184
            ],
185
            $newFile->getContents()
186
        );
187
    }
188
189
    public function testCallingModifyWillPassThroughOptions()
190
    {
191
        $file = $this->createFile('option_pass_through.test');
192
193
        $newFile = $this->head->modify(
194
            $file,
195
            [
196
                'lines'       => 2,
197
                'postfix'     => 'pass',
198
                'keepOldFile' => false,
199
            ]
200
        );
201
202
        static::assertTrue($newFile->exists());
203
        static::assertFalse($file->exists());
204
        static::assertNotNull($newFile);
205
        static::assertEquals('option_pass_through-pass.test', $newFile->getFilename());
206
    }
207
208
    public function testCallingModifyWithoutLinesWillThrowAnException()
209
    {
210
        $file = $this->createFile('option_pass_through.test');
211
212
        $this->expectException(InvalidArgumentException::class);
213
214
        $this->head->modify($file);
215
    }
216
217
    public function testCallingModifyWithANonLocalFileWillThrowAnException()
218
    {
219
        $file = m::mock(FileNodeInterface::class);
220
        $file->shouldReceive('__toString')
221
             ->andReturn('some/file/here');
222
223
        $this->expectException(InvalidArgumentException::class);
224
225
        $this->head->modify($file, ['lines' => 1]);
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_tail.test');
236
        $file->put('nothing interesting here');
237
238
        $this->expectException(ProcessFailedException::class);
239
240
        $this->head->head($file, 3);
241
    }
242
}
243