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::exists |
48
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
49
|
|
|
*/ |
50
|
|
|
public function testExists() |
51
|
|
|
{ |
52
|
|
|
$this->assertTrue((new TextFile($this->file))->exists()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @covers \LazyEight\DiTesto\TextFile::exists |
57
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
58
|
|
|
*/ |
59
|
|
|
public function testNotExists() |
60
|
|
|
{ |
61
|
|
|
$this->assertFalse((new TextFile('abc.txt'))->exists()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @covers \LazyEight\DiTesto\TextFile::isReadable |
66
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
67
|
|
|
* @depends testCanBeCreated |
68
|
|
|
*/ |
69
|
|
|
public function testIsReadable(TextFile $textFile) |
70
|
|
|
{ |
71
|
|
|
$this->assertTrue($textFile->isReadable()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @covers \LazyEight\DiTesto\TextFile::isReadable |
76
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
77
|
|
|
*/ |
78
|
|
|
public function testNotReadable() |
79
|
|
|
{ |
80
|
|
|
chmod($this->notReadable, 0000); |
81
|
|
|
$this->assertFalse((new TextFile($this->notReadable))->isReadable()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @covers \LazyEight\DiTesto\TextFile::isWritable |
86
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
87
|
|
|
*/ |
88
|
|
|
public function testIsWritable() |
89
|
|
|
{ |
90
|
|
|
$instance = new TextFile($this->file); |
91
|
|
|
$this->assertFileIsWritable($this->file); |
92
|
|
|
$this->assertEquals(true, $instance->isWritable()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @covers \LazyEight\DiTesto\TextFile::isWritable |
97
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
98
|
|
|
*/ |
99
|
|
|
public function testNotWritable() |
100
|
|
|
{ |
101
|
|
|
$instance = new TextFile($this->notReadable); |
102
|
|
|
$this->assertFileNotIsWritable($this->notReadable); |
103
|
|
|
$this->assertEquals(false, $instance->isWritable()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getSize |
108
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
109
|
|
|
* @depends testCanBeCreated |
110
|
|
|
*/ |
111
|
|
|
public function testGetSize(TextFile $textFile) |
112
|
|
|
{ |
113
|
|
|
$this->assertEquals(121, $textFile->getSize()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getSize |
118
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
119
|
|
|
* @depends testCanBeCreated |
120
|
|
|
*/ |
121
|
|
|
public function testGetSizeWrongSize(TextFile $textFile) |
122
|
|
|
{ |
123
|
|
|
$this->assertNotEquals(0, $textFile->getSize()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getSize |
128
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
129
|
|
|
*/ |
130
|
|
|
public function testGetSizeNotReadable() |
131
|
|
|
{ |
132
|
|
|
$textFile = new TextFile($this->notReadable); |
133
|
|
|
$this->assertEquals(121, $textFile->getSize()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getType |
138
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
139
|
|
|
* @depends testCanBeCreated |
140
|
|
|
*/ |
141
|
|
|
public function testGetType(TextFile $textFile) |
142
|
|
|
{ |
143
|
|
|
$this->assertEquals('text/plain', $textFile->getType()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getType |
148
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
149
|
|
|
*/ |
150
|
|
|
public function testGetWrongType() |
151
|
|
|
{ |
152
|
|
|
$textFile = new TextFile($this->imageFile); |
153
|
|
|
$this->assertNotEquals('text/plain', $textFile->getType()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getFilename |
158
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
159
|
|
|
* @depends testCanBeCreated |
160
|
|
|
*/ |
161
|
|
|
public function testGetFilename(TextFile $textFile) |
162
|
|
|
{ |
163
|
|
|
$this->assertEquals(basename($this->file), $textFile->getFilename()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getFilename |
168
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
169
|
|
|
* @depends testCanBeCreated |
170
|
|
|
*/ |
171
|
|
|
public function testGetWrongFilename(TextFile $textFile) |
172
|
|
|
{ |
173
|
|
|
$this->assertNotEquals('', $textFile->getFilename()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getPath |
178
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
179
|
|
|
* @depends testCanBeCreated |
180
|
|
|
*/ |
181
|
|
|
public function testGetPath(TextFile $textFile) |
182
|
|
|
{ |
183
|
|
|
$this->assertEquals(dirname($this->file), $textFile->getPath()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getPath |
188
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
189
|
|
|
* @depends testCanBeCreated |
190
|
|
|
*/ |
191
|
|
|
public function testGetWrongPath(TextFile $textFile) |
192
|
|
|
{ |
193
|
|
|
$this->assertNotEquals('', $textFile->getPath()); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getPathName |
198
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
199
|
|
|
* @depends testCanBeCreated |
200
|
|
|
*/ |
201
|
|
|
public function testGetPathname(TextFile $textFile) |
202
|
|
|
{ |
203
|
|
|
$this->assertEquals($this->file, $textFile->getPathName()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getPathName |
208
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
209
|
|
|
* @depends testCanBeCreated |
210
|
|
|
*/ |
211
|
|
|
public function testGetWrongPathname(TextFile $textFile) |
212
|
|
|
{ |
213
|
|
|
$this->assertNotEquals('', $textFile->getPathName()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @covers \LazyEight\DiTesto\TextFile::read |
218
|
|
|
* @covers \LazyEight\DiTesto\TextFile::__toString |
219
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
220
|
|
|
* @depends testCanBeCreated |
221
|
|
|
* @return \LazyEight\DiTesto\TextFile |
222
|
|
|
*/ |
223
|
|
|
public function testRead(TextFile $textFile) |
224
|
|
|
{ |
225
|
|
|
$textFile->read(); |
226
|
|
|
$this->assertEquals(file_get_contents($this->file), $textFile->__toString()); |
227
|
|
|
|
228
|
|
|
return $textFile; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @covers \LazyEight\DiTesto\TextFile::getIterator |
233
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
234
|
|
|
* @depends testRead |
235
|
|
|
*/ |
236
|
|
|
public function testGetIterator(TextFile $textFile) |
237
|
|
|
{ |
238
|
|
|
$this->assertInstanceOf(\ArrayIterator::class, $textFile->getIterator()); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @covers \LazyEight\DiTesto\TextFile::offsetExists |
243
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
244
|
|
|
* @depends testRead |
245
|
|
|
*/ |
246
|
|
|
public function testOffsetExists(TextFile $textFile) |
247
|
|
|
{ |
248
|
|
|
$this->assertTrue($textFile->offsetExists(0)); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @covers \LazyEight\DiTesto\TextFile::offsetExists |
253
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
254
|
|
|
* @depends testRead |
255
|
|
|
*/ |
256
|
|
|
public function testOffsetNotExists(TextFile $textFile) |
257
|
|
|
{ |
258
|
|
|
$this->assertFalse($textFile->offsetExists(count($textFile) + 1)); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @covers \LazyEight\DiTesto\TextFile::count |
263
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
264
|
|
|
* @depends testRead |
265
|
|
|
*/ |
266
|
|
|
public function testCount(TextFile $textFile) |
267
|
|
|
{ |
268
|
|
|
$lines = explode(PHP_EOL, file_get_contents($this->file)); |
269
|
|
|
$this->assertEquals(count($lines), $textFile->count()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @covers \LazyEight\DiTesto\TextFile::offsetGet |
274
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
275
|
|
|
* @depends testRead |
276
|
|
|
*/ |
277
|
|
|
public function testOffsetGet(TextFile $textFile) |
278
|
|
|
{ |
279
|
|
|
$lines = explode(PHP_EOL, file_get_contents($this->file)); |
280
|
|
|
$this->assertEquals($lines[0], $textFile->offsetGet(0)); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @covers \LazyEight\DiTesto\TextFile::offsetSet |
285
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
286
|
|
|
* @depends testRead |
287
|
|
|
*/ |
288
|
|
|
public function testOffsetSet(TextFile $textFile) |
289
|
|
|
{ |
290
|
|
|
$newLine = new Line('this is a new line'); |
291
|
|
|
$textFile->offsetSet(0, $newLine); |
292
|
|
|
$this->assertEquals($newLine, $textFile->offsetGet(0)); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @covers \LazyEight\DiTesto\TextFile::write |
297
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
298
|
|
|
* @depends testRead |
299
|
|
|
*/ |
300
|
|
|
public function testWrite(TextFile $textFile) |
301
|
|
|
{ |
302
|
|
|
$newFile = new TextFile($this->newFilename); |
303
|
|
|
$newFile[] = clone $textFile[0]; |
304
|
|
|
$newFile->write(); |
305
|
|
|
|
306
|
|
|
$this->assertTrue($newFile->exists()); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @covers \LazyEight\DiTesto\TextFile::offsetUnset |
311
|
|
|
* @uses \LazyEight\DiTesto\TextFile |
312
|
|
|
* @depends testRead |
313
|
|
|
*/ |
314
|
|
|
public function testOffsetUnset(TextFile $textFile) |
315
|
|
|
{ |
316
|
|
|
$textFile->offsetUnset(0); |
317
|
|
|
$this->assertEquals(3, $textFile->count()); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @inheritDoc |
322
|
|
|
*/ |
323
|
|
|
public static function tearDownAfterClass() |
324
|
|
|
{ |
325
|
|
|
unlink('./tests/files/new_file.txt'); |
326
|
|
|
|
327
|
|
|
parent::tearDownAfterClass(); // TODO: Change the autogenerated stub |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @inheritDoc |
332
|
|
|
*/ |
333
|
|
|
protected function tearDown() |
334
|
|
|
{ |
335
|
|
|
chmod($this->notReadable, 0555); |
336
|
|
|
|
337
|
|
|
parent::tearDown(); // TODO: Change the autogenerated stub |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|