Code Duplication    Length = 215-216 lines in 2 locations

tests/integration/Modify/HeadTest.php 1 location

@@ 30-244 (lines=215) @@
27
use Symfony\Component\Process\Exception\ProcessFailedException;
28
use Symfony\Component\Process\Process;
29
30
class HeadTest extends AbstractFileTestCase
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);
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')
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

tests/integration/Modify/TailTest.php 1 location

@@ 30-245 (lines=216) @@
27
use Symfony\Component\Process\Exception\ProcessFailedException;
28
use Symfony\Component\Process\Process;
29
30
class TailTest extends AbstractFileTestCase
31
{
32
    /**
33
     * @var Tail
34
     */
35
    protected $tail;
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->tail = new Tail();
46
        $this->tail->setBuilder($this->builder);
47
    }
48
49
    public function testInstanceOf()
50
    {
51
        static::assertInstanceOf(FileModifierInterface::class, $this->tail);
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->tail->canModify($localFile));
60
        static::assertFalse(
61
            $this->tail->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->tail->canModify($randomThing));
68
    }
69
70
    public function testBasicReadingTheLastNLines()
71
    {
72
        $file = $this->createFile('last_five_lines');
73
74
        $newFile = $this->tail->tail($file, 5);
75
76
        static::assertEquals(
77
            [
78
                "Line 6",
79
                "Line 7",
80
                "Line 8",
81
                "Line 9",
82
                "Line 10",
83
            ],
84
            $newFile->getContents()
85
        );
86
87
        $newFile = $this->tail->tail($file, 2);
88
89
        static::assertEquals(
90
            [
91
                "Line 9",
92
                "Line 10",
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 testOutputLinesStartingFromN()
124
    {
125
        $file = $this->createFile('from.second.line.onwards');
126
127
        $newFile = $this->tail->tail($file, '+2');
128
129
        static::assertEquals(
130
            [
131
                "Line 2",
132
                "Line 3",
133
                "Line 4",
134
                "Line 5",
135
                "Line 6",
136
                "Line 7",
137
                "Line 8",
138
                "Line 9",
139
                "Line 10",
140
            ],
141
            $newFile->getContents()
142
        );
143
    }
144
145
    public function testAddingAPostfixToTheEndOfTheFile()
146
    {
147
        $file = $this->createFile('postfix_test.test');
148
149
        $newFile = $this->tail->tail($file, 4, ['postfix' => 'pfixtest']);
150
151
        static::assertNotNull($newFile);
152
        static::assertEquals('postfix_test-pfixtest.test', $newFile->getFilename());
153
    }
154
155
    public function testCallingWithBlankPostfixWillReplaceInLine()
156
    {
157
        $file = $this->createFile('inline_tail.test');
158
159
        $newFile = $this->tail->tail($file, 2, ['postfix' => '']);
160
161
        static::assertNotNull($newFile);
162
        static::assertEquals($file->getFilename(), $newFile->getFilename());
163
    }
164
165
    public function testSettingKeepOldFileToFalseWillDeleteTheOldFile()
166
    {
167
        $file = $this->createFile('inline_replace.test');
168
169
        $newFile = $this->tail->tail($file, 5, ['keepOldFile' => false]);
170
171
        static::assertTrue($newFile->exists());
172
        static::assertFalse($file->exists());
173
    }
174
175
    public function testCallingModifyDoesTail()
176
    {
177
        $file = $this->createFile('simple_tail.test');
178
179
        $newFile = $this->tail->modify($file, ['lines' => 4]);
180
181
        static::assertEquals(
182
            [
183
                "Line 7",
184
                "Line 8",
185
                "Line 9",
186
                "Line 10",
187
            ],
188
            $newFile->getContents()
189
        );
190
    }
191
192
    public function testCallingModifyWillPassThroughOptions()
193
    {
194
        $file = $this->createFile('option_pass_through.test');
195
196
        $newFile = $this->tail->modify(
197
            $file,
198
            [
199
                'lines'       => 2,
200
                'postfix'     => 'pass',
201
                'keepOldFile' => false,
202
            ]
203
        );
204
205
        static::assertTrue($newFile->exists());
206
        static::assertFalse($file->exists());
207
        static::assertNotNull($newFile);
208
        static::assertEquals('option_pass_through-pass.test', $newFile->getFilename());
209
    }
210
211
    public function testCallingModifyWithoutLinesWillThrowAnException()
212
    {
213
        $file = $this->createFile('option_pass_through.test');
214
215
        $this->expectException(InvalidArgumentException::class);
216
217
        $this->tail->modify($file);
218
    }
219
220
    public function testCallingModifyWithANonLocalFileWillThrowAnException()
221
    {
222
        $file = m::mock(FileNodeInterface::class);
223
        $file->shouldReceive('__toString')
224
             ->andReturn('some/file/here');
225
226
        $this->expectException(InvalidArgumentException::class);
227
228
        $this->tail->modify($file, ['lines' => 1]);
229
    }
230
231
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding()
232
    {
233
        $process = m::mock(Process::class)->makePartial();
234
        $process->shouldReceive('isSuccessful')->andReturn(false);
235
        $this->builder->shouldReceive('build')
236
                      ->andReturn($process);
237
238
        $file = new LocalFile(static::$dir . 'failed_tail.test');
239
        $file->put('nothing interesting here');
240
241
        $this->expectException(ProcessFailedException::class);
242
243
        $this->tail->tail($file, 3);
244
    }
245
}
246