Completed
Push — master ( 49c7da...61b9c9 )
by Victor
13s
created

FileWriterTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

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