LogTest::testIncorrectLogObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Test;
4
5
use PHPUnit\Framework\TestCase;
6
use SimpleLog\Log as SimpleLog;
7
use BlueRegister\Log;
8
use Test\TestClass;
9
10
class LogTest extends TestCase
11
{
12
    /**
13
     * name of test event log file
14
     */
15
    const REGISTER_LOG_NAME = '/debug.log';
16
17
    /**
18
     * @var string
19
     */
20
    protected $logPath;
21
22
    /**
23
     * actions launched before test starts
24
     */
25
    protected function setUp(): void
26
    {
27
        $this->logPath = __DIR__ . '/log';
28
29
        $this->clearLog();
30
    }
31
32
    public function testIncorrectLogObject()
33
    {
34
        $this->expectExceptionMessage("Log should be instance of SimpleLog\LogInterface: Test\TestClass\SimpleClass");
35
        $this->expectException(\LogicException::class);
36
37
        new Log(TestClass\SimpleClass::class);
38
    }
39
40
    public function testIncorrectLogObjectFromNoneExistingClass()
41
    {
42
        $this->expectExceptionMessage("Class don't exists: SomeClass");
43
        $this->expectException(\LogicException::class);
44
45
        new Log('SomeClass');
46
    }
47
48
    public function testCreateLogInstanceFromIncorrectObject()
49
    {
50
        $this->expectExceptionMessage("Cannot create Log instance: Test\TestClass\SimpleClass");
51
        $this->expectException(\LogicException::class);
52
53
        new Log(new \Test\TestClass\SimpleClass);
54
    }
55
56
    public function testCreateLogObject()
57
    {
58
        $log1 = new Log(new SimpleLog);
59
        $log2 = new Log(SimpleLog::class);
60
61
        $this->assertInstanceOf(Log::class, $log1);
62
        $this->assertInstanceOf(Log::class, $log2);
63
    }
64
65
    public function testLogMessages()
66
    {
67
        $this->clearLog();
68
        $simpleLog = new SimpleLog;
69
        $simpleLog->setOption('log_path', $this->logPath)
70
            ->setOption('level', 'debug');
71
72
        $log = new Log($simpleLog);
73
        $log->makeLog('Test log message');
74
75
        $this->assertFileExists($this->logFile());
76
        $this->clearLog();
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    protected function logFile()
83
    {
84
        return $this->logPath . self::REGISTER_LOG_NAME;
85
    }
86
87
    protected function clearLog()
88
    {
89
        if (file_exists($this->logFile())) {
90
            unlink($this->logFile());
91
        }
92
    }
93
94
    /**
95
     * actions launched after test was finished
96
     */
97
    protected function tearDown(): void
98
    {
99
        $this->clearLog();
100
    }
101
}
102