|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Test\DiTesto; |
|
4
|
|
|
|
|
5
|
|
|
use LazyEight\DiTesto\Exceptions\IOException; |
|
6
|
|
|
use LazyEight\DiTesto\TextFile; |
|
7
|
|
|
use LazyEight\DiTesto\TextFileReader; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
class TextFileReaderTest 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
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::__construct |
|
29
|
|
|
* @uses \LazyEight\DiTesto\TextFileReader |
|
30
|
|
|
* @return \LazyEight\DiTesto\TextFileReader |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testCanBeCreated() |
|
33
|
|
|
{ |
|
34
|
|
|
$filename = $this->file; |
|
35
|
|
|
$instance = new TextFileReader($filename); |
|
36
|
|
|
$this->assertInstanceOf(TextFileReader::class, $instance); |
|
37
|
|
|
|
|
38
|
|
|
return $instance; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::readFile |
|
43
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::valid |
|
44
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::validatePath |
|
45
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::validateReadable |
|
46
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::validateType |
|
47
|
|
|
* @uses \LazyEight\DiTesto\TextFileReader |
|
48
|
|
|
* @depends testCanBeCreated |
|
49
|
|
|
* @uses \LazyEight\DiTesto\TextFileReader |
|
50
|
|
|
* @param \LazyEight\DiTesto\TextFileLoader |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testCanBeLoaded(TextFileReader $loader) |
|
53
|
|
|
{ |
|
54
|
|
|
$file = $loader->readFile(); |
|
|
|
|
|
|
55
|
|
|
$this->assertInstanceOf(TextFile::class, $loader->readFile()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::readFile |
|
60
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::valid |
|
61
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::validatePath |
|
62
|
|
|
* @expectedException \LazyEight\DiTesto\Exceptions\IOException |
|
63
|
|
|
* @uses \LazyEight\DiTesto\TextFileReader |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testCantBeLoaded() |
|
66
|
|
|
{ |
|
67
|
|
|
$loader = new TextFileReader(''); |
|
68
|
|
|
$this->expectException(IOException::class); |
|
69
|
|
|
$loader->readFile(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::readFile |
|
74
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::valid |
|
75
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::validatePath |
|
76
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::validateType |
|
77
|
|
|
* @expectedException \LazyEight\DiTesto\Exceptions\IOException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testInvalidType() |
|
80
|
|
|
{ |
|
81
|
|
|
$instance = new TextFileReader($this->imageFile); |
|
82
|
|
|
$this->expectException(IOException::class); |
|
83
|
|
|
$instance->readFile(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::readFile |
|
88
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::valid |
|
89
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::validatePath |
|
90
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::validateReadable |
|
91
|
|
|
* @expectedException \LazyEight\DiTesto\Exceptions\IOException |
|
92
|
|
|
*/ |
|
93
|
|
|
public function testCantRead() |
|
94
|
|
|
{ |
|
95
|
|
|
chmod($this->notReadable, 0000); |
|
96
|
|
|
|
|
97
|
|
|
$instance = new TextFileReader($this->notReadable); |
|
98
|
|
|
$this->expectException(IOException::class); |
|
99
|
|
|
$instance->readFile(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::readFile |
|
104
|
|
|
* @covers \LazyEight\DiTesto\TextFileReader::getPath |
|
105
|
|
|
* @uses \LazyEight\DiTesto\TextFileReader |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testCanGetPath() |
|
108
|
|
|
{ |
|
109
|
|
|
$loader = new TextFileReader($this->file); |
|
110
|
|
|
$this->assertEquals($this->file, $loader->getPath()); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.