1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Test\DiTesto; |
4
|
|
|
|
5
|
|
|
use LazyEight\DiTesto\Exceptions\IOException; |
6
|
|
|
use LazyEight\DiTesto\Line; |
7
|
|
|
use LazyEight\DiTesto\TextFile; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class TextFileTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $file = './tests/files/urls.txt'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $imageFile = './tests/files/images.jpg'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $notReadable = './tests/files/urls_not_readable.txt'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $newFilename = './tests/files/new_file.txt'; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @covers \LazyEight\DiTesto\TextFile::__construct |
35
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
36
|
|
|
* @return \LazyEight\DiTesto\TextFile |
37
|
|
|
*/ |
38
|
|
|
public function testCanBeCreated() |
39
|
|
|
{ |
40
|
|
|
$instance = new TextFile($this->file); |
41
|
|
|
$this->assertInstanceOf(TextFile::class, $instance); |
42
|
|
|
|
43
|
|
|
return $instance; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @covers \LazyEight\DiTesto\TextFile::__construct |
48
|
|
|
* @covers \LazyEight\DiTesto\TextFile::readLines |
49
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
50
|
|
|
* @return \LazyEight\DiTesto\TextFile |
51
|
|
|
*/ |
52
|
|
|
public function testCanBeCreatedWithContent() |
53
|
|
|
{ |
54
|
|
|
$instance = new TextFile($this->file, file_get_contents($this->file)); |
55
|
|
|
$this->assertInstanceOf(TextFile::class, $instance); |
56
|
|
|
$this->assertEquals(file_get_contents($this->file), $instance->getRawContent()); |
57
|
|
|
|
58
|
|
|
return $instance; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getRawContent |
63
|
|
|
* @uses \LazyEight\DiTesto\File |
64
|
|
|
* @depends testCanBeCreatedWithContent |
65
|
|
|
* @param TextFile $file |
66
|
|
|
*/ |
67
|
|
|
public function testGetRawContent(TextFile $file) |
68
|
|
|
{ |
69
|
|
|
$content = file_get_contents($this->file); |
70
|
|
|
$this->assertEquals($content, $file->getRawContent()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getRawContent |
75
|
|
|
* @uses \LazyEight\DiTesto\File |
76
|
|
|
* @depends testCanBeCreatedWithContent |
77
|
|
|
* @param TextFile $file |
78
|
|
|
*/ |
79
|
|
|
public function testCantGetRawContent(TextFile $file) |
80
|
|
|
{ |
81
|
|
|
$this->assertNotEquals('', $file->getRawContent()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @covers \LazyEight\DiTesto\TextFile::setRawContent |
86
|
|
|
* @covers \LazyEight\DiTesto\TextFile::readLines |
87
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
88
|
|
|
* @depends testCanBeCreated |
89
|
|
|
*/ |
90
|
|
|
public function testSetRawContent(TextFile $textFile) |
91
|
|
|
{ |
92
|
|
|
$textFile->setRawContent(file_get_contents($this->file)); |
93
|
|
|
$this->assertEquals(file_get_contents($this->file), $textFile->getRawContent()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @covers \LazyEight\DiTesto\TextFile::setRawContent |
98
|
|
|
* @covers \LazyEight\DiTesto\TextFile::readLines |
99
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
100
|
|
|
* @depends testCanBeCreated |
101
|
|
|
*/ |
102
|
|
|
public function testSetEmptyRawContent(TextFile $textFile) |
103
|
|
|
{ |
104
|
|
|
$textFile->setRawContent(''); |
105
|
|
|
$this->assertNotEquals(file_get_contents($this->file), $textFile->getRawContent()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getIterator |
110
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
111
|
|
|
* @depends testCanBeCreatedWithContent |
112
|
|
|
*/ |
113
|
|
|
public function testGetIterator(TextFile $textFile) |
114
|
|
|
{ |
115
|
|
|
$this->assertInstanceOf(\ArrayIterator::class, $textFile->getIterator()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @covers \LazyEight\DiTesto\TextFile::offsetExists |
120
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
121
|
|
|
* @depends testCanBeCreatedWithContent |
122
|
|
|
*/ |
123
|
|
|
public function testOffsetExists(TextFile $textFile) |
124
|
|
|
{ |
125
|
|
|
$this->assertTrue($textFile->offsetExists(0)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @covers \LazyEight\DiTesto\TextFile::offsetExists |
130
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
131
|
|
|
* @depends testCanBeCreatedWithContent |
132
|
|
|
*/ |
133
|
|
|
public function testOffsetNotExists(TextFile $textFile) |
134
|
|
|
{ |
135
|
|
|
$this->assertFalse($textFile->offsetExists(count($textFile) + 1)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @covers \LazyEight\DiTesto\TextFile::count |
140
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
141
|
|
|
* @depends testCanBeCreatedWithContent |
142
|
|
|
*/ |
143
|
|
|
public function testCount(TextFile $textFile) |
144
|
|
|
{ |
145
|
|
|
$lines = explode(PHP_EOL, file_get_contents($this->file)); |
146
|
|
|
$this->assertEquals(count($lines), $textFile->count()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @covers \LazyEight\DiTesto\TextFile::offsetGet |
151
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
152
|
|
|
* @depends testCanBeCreatedWithContent |
153
|
|
|
*/ |
154
|
|
|
public function testOffsetGet(TextFile $textFile) |
155
|
|
|
{ |
156
|
|
|
$lines = explode(PHP_EOL, file_get_contents($this->file)); |
157
|
|
|
$this->assertEquals($lines[0], $textFile->offsetGet(0)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @covers \LazyEight\DiTesto\TextFile::offsetSet |
162
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
163
|
|
|
* @depends testCanBeCreatedWithContent |
164
|
|
|
*/ |
165
|
|
|
public function testOffsetSet(TextFile $textFile) |
166
|
|
|
{ |
167
|
|
|
$newLine = new Line('this is a new line'); |
168
|
|
|
$textFile->offsetSet(0, $newLine); |
169
|
|
|
$this->assertEquals($newLine, $textFile->offsetGet(0)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @covers \LazyEight\DiTesto\TextFile::offsetUnset |
174
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
175
|
|
|
* @depends testCanBeCreatedWithContent |
176
|
|
|
*/ |
177
|
|
|
public function testOffsetUnset(TextFile $textFile) |
178
|
|
|
{ |
179
|
|
|
$textFile->offsetUnset(0); |
180
|
|
|
$this->assertEquals(3, $textFile->count()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @inheritDoc |
185
|
|
|
*/ |
186
|
|
|
protected function tearDown() |
187
|
|
|
{ |
188
|
|
|
chmod($this->notReadable, 0555); |
189
|
|
|
|
190
|
|
|
if (file_exists('./tests/files/new_file.txt')) { |
191
|
|
|
unlink('./tests/files/new_file.txt'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
parent::tearDown(); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|