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\Tail; |
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 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
|
|
|
|
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.