Completed
Push — master ( 05283a...49c7da )
by Victor
12s
created

TextFileWriterTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 76
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testWriteFile() 0 11 1
A testCantWriteFile() 0 13 1
A tearDownAfterClass() 0 15 3
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 LazyEight\DiTesto\TextFileWriter;
9
use PHPUnit\Framework\TestCase;
10
11
class TextFileWriterTest extends TestCase
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $file = './tests/files/urls.txt';
17
18
    /**
19
     * @var string
20
     */
21
    protected $path = './tests/files';
22
23
    /**
24
     * @var string
25
     */
26
    protected $newFilename = 'newFilename.txt';
27
28
    /**
29
     * @var string
30
     */
31
    protected $testLine = 'THIS IS A TEST LINE';
32
33
    /**
34
     * @covers \LazyEight\DiTesto\TextFileWriter::writeFile
35
     * @uses \LazyEight\DiTesto\TextFileWriter
36
     */
37
    public function testWriteFile()
38
    {
39
        $newFilename = $this->path . '/' . $this->newFilename;
40
        $textFile = new TextFile($newFilename);
41
        $textFile[] = new Line($this->testLine);
42
43
        $textFileWriter = new TextFileWriter();
44
        $textFileWriter->writeFile($textFile);
45
46
        $this->assertTrue($textFile->exists());
47
    }
48
49
    /**
50
     * @covers \LazyEight\DiTesto\TextFileWriter::writeFile
51
     * @uses \LazyEight\DiTesto\TextFileWriter
52
     * @expectException IOException
53
     */
54
    public function testCantWriteFile()
55
    {
56
        $path = $this->path . '/' . 'newPath';
57
        mkdir($path);
58
        chmod($path, 0555);
59
60
        $newFilename = $path . '/' . $this->newFilename;
61
        $textFile = new TextFile($newFilename);
62
        $textFile[] = new Line($this->testLine);
63
64
        $this->expectException(IOException::class);
65
        (new TextFileWriter())->writeFile($textFile);
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public static function tearDownAfterClass()
72
    {
73
        $file = './tests/files/newFilename.txt';
74
        $newFilePath = './tests/files/newPath';
75
76
        if (file_exists($file)) {
77
            unlink($file);
78
        }
79
80
        if (file_exists($newFilePath)) {
81
            rmdir($newFilePath);
82
        }
83
84
        parent::tearDownAfterClass(); // TODO: Change the autogenerated stub
85
    }
86
}
87