FileTest::testExceptionDuringCreateLogDirectory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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(): void
44
    {
45
        $this->logPath = __DIR__ . '/../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(): void
53
    {
54
        $this->assertFileDoesNotExist($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(): void
65
    {
66
        $storage = new File($this->fileConfig);
67
68
        $this->assertFileDoesNotExist($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
    public function testExceptionDuringCreateLogDirectory(): void
86
    {
87
        $this->expectExceptionMessage("Unable to create log directory: /none/exists");
88
        $this->expectException(\SimpleLog\LogException::class);
89
90
        $storage = new File(['log_path' => '/none/exists']);
91
92
        $storage->store($this->testMessage[0], $this->testLog);
93
    }
94
95
    public function testExceptionDuringSaveLogFile(): void
96
    {
97
        $this->expectException(\SimpleLog\LogException::class);
98
        \chmod(__DIR__ . '/../no_permission/notice.log', 0555);
99
        (new File(['log_path' => __DIR__ . '/../no_permission']))
100
            ->store($this->testMessage[0], $this->testLog);
101
    }
102
103
    /**
104
     * actions launched after test was finished
105
     */
106
    protected function tearDown(): void
107
    {
108
        if (\file_exists($this->fullTestFilePath)) {
109
            \unlink($this->fullTestFilePath);
110
        }
111
    }
112
}
113