1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TextFile\Tests\Reader; |
4
|
|
|
|
5
|
|
|
use TextFile\Reader\SimpleReader; |
6
|
|
|
use TextFile\Tests\TextFileTestCase; |
7
|
|
|
use TextFile\Walker\SimpleWalker; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class SimpleReaderTest |
11
|
|
|
* |
12
|
|
|
* @package TextFile\Tests\Reader |
13
|
|
|
*/ |
14
|
|
|
class SimpleReaderTest extends TextFileTestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @covers TextFile\Reader\SimpleReader::getLinesRange |
18
|
|
|
*/ |
19
|
|
|
public function testGetLinesRange() |
20
|
|
|
{ |
21
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
22
|
|
|
|
23
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
24
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
25
|
|
|
$range = $reader->getLinesRange($file, 0, 2); |
26
|
|
|
|
27
|
|
|
$this->assertInstanceOf('\LimitIterator', $range); |
28
|
|
|
|
29
|
|
|
$this->assertCount(2, $range); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @covers TextFile\Reader\SimpleReader::getNextLineContent |
34
|
|
|
*/ |
35
|
|
|
public function testGetNextLineContentFromStart() |
36
|
|
|
{ |
37
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
38
|
|
|
|
39
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
40
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
41
|
|
|
|
42
|
|
|
$this->assertEquals('second', $reader->getNextLineContent($file)); |
43
|
|
|
|
44
|
|
|
// Test if pointer is correctly reset |
45
|
|
|
|
46
|
|
|
$this->assertEquals('second', $reader->getNextLineContent($file)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @covers TextFile\Reader\SimpleReader::getNextLineContent |
51
|
|
|
*/ |
52
|
|
|
public function testGetNextLineContent() |
53
|
|
|
{ |
54
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
55
|
|
|
|
56
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
57
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
58
|
|
|
|
59
|
|
|
$file->seek(2); |
60
|
|
|
|
61
|
|
|
$this->assertEquals('fourth', $reader->getNextLineContent($file)); |
62
|
|
|
|
63
|
|
|
// Test if pointer is correctly reset |
64
|
|
|
|
65
|
|
|
$this->assertEquals('fourth', $reader->getNextLineContent($file)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @covers TextFile\Reader\SimpleReader::getNextLineContent |
70
|
|
|
* |
71
|
|
|
* @expectedException \TextFile\Exception\OutOfBoundsException |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function testGetNextLineContentOutOfBounds() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
76
|
|
|
|
77
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
78
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
79
|
|
|
|
80
|
|
|
$file->seek(5); |
81
|
|
|
|
82
|
|
|
$reader->getNextLineContent($file); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @covers TextFile\Reader\SimpleReader::getPreviousLineContent |
87
|
|
|
*/ |
88
|
|
|
public function testGetPreviousLineContent() |
89
|
|
|
{ |
90
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
91
|
|
|
|
92
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
93
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
94
|
|
|
|
95
|
|
|
$file->seek(2); |
96
|
|
|
|
97
|
|
|
$this->assertEquals('second', $reader->getPreviousLineContent($file)); |
98
|
|
|
|
99
|
|
|
// Test if pointer is correctly reset |
100
|
|
|
|
101
|
|
|
$this->assertEquals('second', $reader->getPreviousLineContent($file)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @covers TextFile\Reader\SimpleReader::getPreviousLineContent |
106
|
|
|
* |
107
|
|
|
* @expectedException \TextFile\Exception\OutOfBoundsException |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function testGetPreviousLineContentOutOfBounds() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
112
|
|
|
|
113
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
114
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
115
|
|
|
|
116
|
|
|
$file->seek(0); |
117
|
|
|
|
118
|
|
|
$reader->getPreviousLineContent($file); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @covers TextFile\Reader\SimpleReader::getCurrentLineContent |
123
|
|
|
*/ |
124
|
|
|
public function testGetCurrentLineContent() |
125
|
|
|
{ |
126
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
127
|
|
|
|
128
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
129
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
130
|
|
|
|
131
|
|
|
$file->seek(2); |
132
|
|
|
|
133
|
|
|
$this->assertEquals('third', $reader->getCurrentLineContent($file)); |
134
|
|
|
|
135
|
|
|
// Test if pointer is correctly reset |
136
|
|
|
|
137
|
|
|
$this->assertEquals('third', $reader->getCurrentLineContent($file)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @covers TextFile\Reader\SimpleReader::getLineContent |
142
|
|
|
*/ |
143
|
|
|
public function testGetLineContent() |
144
|
|
|
{ |
145
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
146
|
|
|
|
147
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
148
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
149
|
|
|
|
150
|
|
|
$this->assertEquals('third', $reader->getLineContent($file, 2)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @covers TextFile\Reader\SimpleReader::getLineContent |
155
|
|
|
* |
156
|
|
|
* @expectedException \TextFile\Exception\OutOfBoundsException |
157
|
|
|
*/ |
158
|
|
|
public function testGetLineContentOutOfBoundsTooHigh() |
159
|
|
|
{ |
160
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
161
|
|
|
|
162
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
163
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
164
|
|
|
|
165
|
|
|
$this->assertEquals('third', $reader->getLineContent($file, 9)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @covers TextFile\Reader\SimpleReader::getLineContent |
170
|
|
|
* |
171
|
|
|
* @expectedException \TextFile\Exception\OutOfBoundsException |
172
|
|
|
*/ |
173
|
|
|
public function testGetLineContentOutOfBoundsTooLow() |
174
|
|
|
{ |
175
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
176
|
|
|
|
177
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
178
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
179
|
|
|
|
180
|
|
|
$this->assertEquals('third', $reader->getLineContent($file, -1)); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @covers TextFile\Reader\SimpleReader::getNextCharacterContent |
185
|
|
|
*/ |
186
|
|
|
public function testGetNextCharacterContent() |
187
|
|
|
{ |
188
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
189
|
|
|
|
190
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
191
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
192
|
|
|
|
193
|
|
|
$file->fseek(2); |
194
|
|
|
|
195
|
|
|
$this->assertEquals('r', $reader->getNextCharacterContent($file)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @covers TextFile\Reader\SimpleReader::getPreviousCharacterContent |
200
|
|
|
*/ |
201
|
|
|
public function testGetPreviousCharacterContent() |
202
|
|
|
{ |
203
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
204
|
|
|
|
205
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
206
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
207
|
|
|
|
208
|
|
|
$file->fseek(2); |
209
|
|
|
|
210
|
|
|
$this->assertEquals('i', $reader->getPreviousCharacterContent($file)); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @covers TextFile\Reader\SimpleReader::getCharacterContent |
215
|
|
|
*/ |
216
|
|
|
public function testGetCharacterContent() |
217
|
|
|
{ |
218
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
219
|
|
|
|
220
|
|
|
$filePath = $this->createTestFileFromFixtures('complex_multiline_file.txt'); |
221
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
222
|
|
|
|
223
|
|
|
$this->assertEquals('r', $reader->getCharacterContent($file, 2)); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @covers TextFile\Reader\SimpleReader::getPreviousCharacterContent |
228
|
|
|
*/ |
229
|
|
|
public function testCleanLineContent() |
230
|
|
|
{ |
231
|
|
|
$reader = new SimpleReader(new SimpleWalker()); |
232
|
|
|
|
233
|
|
|
$this->assertEmpty($reader->cleanLineContent(PHP_EOL)); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
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.