FileWriterTest::testWriteNewFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Test\DiTesto;
4
5
use LazyEight\DiTesto\FileSystem\Exceptions\FileSystemException;
6
use LazyEight\DiTesto\FileSystem\FileSystemHandler;
7
use LazyEight\DiTesto\TextFile;
8
use LazyEight\DiTesto\FileWriter;
9
use PHPUnit\Framework\TestCase;
10
11
class FileWriterTest 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\FileWriter::__construct
35
     * @covers \LazyEight\DiTesto\FileWriter::writeFile
36
     * @covers \LazyEight\DiTesto\FileSystem\FileSystemHandler::getPath
37
     * @covers \LazyEight\DiTesto\FileSystem\FileSystemHandler::writable
38
     * @covers \LazyEight\DiTesto\FileSystem\FileSystemHandler::write
39
     * @uses \LazyEight\DiTesto\FileWriter
40
     */
41
    public function testWriteNewFile()
42
    {
43
        $newFilename = $this->path . '/' . $this->newFilename;
44
        $textFile = new TextFile($newFilename, $this->testLine);
45
        $handler = new FileSystemHandler($newFilename);
46
47
        (new FileWriter($textFile, $handler))->writeFile();
48
        $this->assertTrue($handler->getPath()->exists());
49
    }
50
51
    /**
52
     * @covers \LazyEight\DiTesto\FileWriter::writeFile
53
     * @covers \LazyEight\DiTesto\FileSystem\FileSystemHandler::writable
54
     * @covers \LazyEight\DiTesto\FileSystem\FileSystemHandler::write
55
     * @uses \LazyEight\DiTesto\FileWriter
56
     * @expectException IOException
57
     */
58
    public function testCantWriteFile()
59
    {
60
        $path = $this->path . '/' . 'newPath';
61
        mkdir($path);
62
        chmod($path, 0555);
63
64
        $newFilename = $path . '/' . $this->newFilename;
65
        $textFile = new TextFile($newFilename, $this->testLine);
66
        $handler = new FileSystemHandler($newFilename);
67
68
        $this->expectException(FileSystemException::class);
69
        (new FileWriter($textFile, $handler))->writeFile();
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public static function tearDownAfterClass()
76
    {
77
        $file = './tests/files/newFilename.txt';
78
        $newFilePath = './tests/files/newPath';
79
80
        if (file_exists($file)) {
81
            unlink($file);
82
        }
83
84
        if (file_exists($newFilePath)) {
85
            rmdir($newFilePath);
86
        }
87
88
        parent::tearDownAfterClass(); // TODO: Change the autogenerated stub
89
    }
90
}
91