Passed
Branch master (8a6725)
by Michał
02:20
created

FileTest::tearDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleLog\Test;
4
5
use SimpleLog\Storage\File;
6
use PHPUnit\Framework\TestCase;
7
8
class FileTest extends TestCase
9
{
10
    /**
11
     * store generated log file path
12
     *
13
     * @var string
14
     */
15
    protected $logPath;
16
17
    /**
18
     * @var array
19
     */
20
    protected $fileConfig = [];
21
22
    /**
23
     * @var string
24
     */
25
    protected $testLog = 'notice';
26
27
    /**
28
     * @var string
29
     */
30
    protected $fullTestFilePath;
31
32
    /**
33
     * @var array
34
     */
35
    protected $testMessage = [
36
        'Some log message',
37
        'Some another log message',
38
    ];
39
40
    /**
41
     * actions launched before test starts
42
     */
43
    protected function setUp()
44
    {
45
        $this->logPath = dirname(__FILE__) . '/../log';
46
        $this->fileConfig = ['log_path' => $this->logPath];
47
        $this->fullTestFilePath = $this->logPath . '/' . $this->testLog . '.log';
48
49
        $this->tearDown();
50
    }
51
52
    public function testCreateLogFile()
53
    {
54
        $this->assertFileNotExists($this->fullTestFilePath);
55
56
        (new File($this->fileConfig))->store($this->testMessage[0], $this->testLog);
57
58
        $this->assertFileExists($this->fullTestFilePath);
59
60
        $content = file_get_contents($this->fullTestFilePath);
61
        $this->assertEquals($this->testMessage[0], $content);
62
    }
63
64
    public function testAddMessageForExistingLog()
65
    {
66
        $storage = new File($this->fileConfig);
67
68
        $this->assertFileNotExists($this->fullTestFilePath);
69
70
        $storage->store($this->testMessage[0], $this->testLog);
71
72
        $this->assertFileExists($this->fullTestFilePath);
73
74
        $content = file_get_contents($this->fullTestFilePath);
75
        $this->assertEquals($this->testMessage[0], $content);
76
77
        $storage->store($this->testMessage[1], $this->testLog);
78
79
        $this->assertFileExists($this->fullTestFilePath);
80
81
        $content = file_get_contents($this->fullTestFilePath);
82
        $this->assertEquals($this->testMessage[0] . $this->testMessage[1], $content);
83
    }
84
85
    /**
86
     * @expectedException \SimpleLog\LogException
87
     * @expectedExceptionMessage Unable to create log directory: /none/exists
88
     */
89
    public function testExceptionDuringCreateLogDirectory()
90
    {
91
        $storage = new File(['log_path' => '/none/exists']);
92
93
        $storage->store($this->testMessage[0], $this->testLog);
94
    }
95
96
    /**
97
     * @expectedException \SimpleLog\LogException
98
     */
99
    public function testExceptionDuringSaveLogFile()
100
    {
101
        chmod(dirname(__FILE__) . '/../no_permission/notice.log', 0555);
102
        (new File(['log_path' => dirname(__FILE__) . '/../no_permission']))
103
            ->store($this->testMessage[0], $this->testLog);
104
    }
105
106
    /**
107
     * actions launched after test was finished
108
     */
109
    protected function tearDown()
110
    {
111
        if (file_exists($this->fullTestFilePath)) {
112
            unlink($this->fullTestFilePath);
113
        }
114
    }
115
}
116