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