1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TextFile\Tests\Walker; |
4
|
|
|
|
5
|
|
|
use TextFile\Tests\TextFileTestCase; |
6
|
|
|
use TextFile\Walker\SimpleWalker; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SimpleWalkerTest |
10
|
|
|
* |
11
|
|
|
* @package TextFile\Tests\Walker |
12
|
|
|
*/ |
13
|
|
|
class SimpleWalkerTest extends TextFileTestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @covers TextFile\Walker\SimpleWalker::goToLine |
17
|
|
|
*/ |
18
|
|
|
public function testGoToLine() |
19
|
|
|
{ |
20
|
|
|
$filePath = $this->createTestFileFromFixtures('simple_multiline_file.txt'); |
21
|
|
|
$lineNumber = 21; |
22
|
|
|
|
23
|
|
|
for ($i = 0; $i <= $lineNumber; $i++) { |
24
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
25
|
|
|
|
26
|
|
|
$walker = new SimpleWalker(); |
27
|
|
|
|
28
|
|
|
$walker->goToLine($file, $i); |
29
|
|
|
|
30
|
|
|
$this->assertEquals($i, $file->key()); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @covers TextFile\Walker\SimpleWalker::goToLine |
36
|
|
|
* @expectedException \TextFile\Exception\OutOfBoundsException |
37
|
|
|
*/ |
38
|
|
|
public function testGoToLineTooHigh() |
39
|
|
|
{ |
40
|
|
|
$filePath = $this->createTestFileFromFixtures('simple_multiline_file.txt'); |
41
|
|
|
|
42
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
43
|
|
|
|
44
|
|
|
$walker = new SimpleWalker(); |
45
|
|
|
|
46
|
|
|
$walker->goToLine($file, 40); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @covers TextFile\Walker\SimpleWalker::goToLine |
51
|
|
|
* @expectedException \TextFile\Exception\OutOfBoundsException |
52
|
|
|
*/ |
53
|
|
|
public function testGoToLineTooLow() |
54
|
|
|
{ |
55
|
|
|
$filePath = $this->createTestFileFromFixtures('simple_multiline_file.txt'); |
56
|
|
|
|
57
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
58
|
|
|
|
59
|
|
|
$walker = new SimpleWalker(); |
60
|
|
|
|
61
|
|
|
$walker->goToLine($file, -1); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @covers TextFile\Walker\SimpleWalker::countLines |
66
|
|
|
*/ |
67
|
|
|
public function testCountLines() |
68
|
|
|
{ |
69
|
|
|
$filePath = $this->createTestFileFromFixtures('simple_multiline_file.txt'); |
70
|
|
|
|
71
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
72
|
|
|
|
73
|
|
|
$walker = new SimpleWalker(); |
74
|
|
|
|
75
|
|
|
$this->assertEquals(21, $walker->countLines($file)); |
76
|
|
|
|
77
|
|
|
$file->seek(21); |
78
|
|
|
$file->fwrite('test' . PHP_EOL); |
79
|
|
|
|
80
|
|
|
// Evaluate if correctly refreshed after modification |
81
|
|
|
$this->assertEquals(22, $walker->countLines($file)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @covers TextFile\Walker\SimpleWalker::goBeforeCharacter |
86
|
|
|
*/ |
87
|
|
|
public function testGoBeforeCharacter() |
88
|
|
|
{ |
89
|
|
|
$filePath = $this->createTestFileFromFixtures('simple_multiline_file.txt'); |
90
|
|
|
|
91
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
92
|
|
|
|
93
|
|
|
$walker = new SimpleWalker(); |
94
|
|
|
|
95
|
|
|
$walker->goBeforeCharacter($file, 0); |
96
|
|
|
|
97
|
|
|
$this->assertEquals(0, $file->fgetc()); |
98
|
|
|
|
99
|
|
|
$walker->goBeforeCharacter($file, 1); |
100
|
|
|
|
101
|
|
|
$this->assertEquals(PHP_EOL, $file->fgetc()); |
102
|
|
|
|
103
|
|
|
$walker->goBeforeCharacter($file, 2); |
104
|
|
|
|
105
|
|
|
$this->assertEquals(1, $file->fgetc()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @covers TextFile\Walker\SimpleWalker::goBeforeCharacter |
110
|
|
|
* @expectedException \TextFile\Exception\OutOfBoundsException |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
public function testGoBeforeCharacterTooLow() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$filePath = $this->createTestFileFromFixtures('simple_multiline_file.txt'); |
115
|
|
|
|
116
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
117
|
|
|
|
118
|
|
|
$walker = new SimpleWalker(); |
119
|
|
|
|
120
|
|
|
$walker->goBeforeCharacter($file, -1); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @covers TextFile\Walker\SimpleWalker::goBeforeCharacter |
125
|
|
|
* @expectedException \TextFile\Exception\OutOfBoundsException |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function testGoBeforeCharacterTooHigh() |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$filePath = $this->createTestFileFromFixtures('simple_multiline_file.txt'); |
130
|
|
|
|
131
|
|
|
$file = new \SplFileObject($filePath, 'r+'); |
132
|
|
|
|
133
|
|
|
$walker = new SimpleWalker(); |
134
|
|
|
|
135
|
|
|
$walker->goBeforeCharacter($file, $file->getSize() + 1); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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.