Completed
Pull Request — master (#10)
by Victor
02:13
created

TextFileReaderTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 2
dl 0
loc 103
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanBeCreated() 0 8 1
A testCanBeLoaded() 0 5 1
A testCantBeLoaded() 0 6 1
A testInvalidType() 0 6 1
A testCantRead() 0 8 1
A testCanGetPath() 0 5 1
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();
0 ignored issues
show
Unused Code introduced by
$file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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