LoggerTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 33
c 1
b 0
f 0
dl 0
loc 75
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dataLog() 0 11 1
A dataInterpolate() 0 5 1
A dataConstruct() 0 5 1
A testConstruct() 0 7 2
A testInterpolate() 0 6 1
A testLog() 0 17 1
1
<?php
2
namespace RazonYang\Yii2\Psr\Log\Tests\Unit;
3
4
use yii\log\Logger as YiiLogger;
5
use Codeception\Test\Unit;
6
use Psr\Log\LogLevel;
7
use RazonYang\Yii2\Psr\Log\Logger;
8
use Yii;
9
10
class LoggerTest extends Unit
11
{
12
    /**
13
     * @dataProvider dataLog
14
     */
15
    public function testLog(
16
        string $level,
17
        string $message,
18
        array $context,
19
        int $expectedLevel,
20
        string $expectedMessage,
21
        string $expectedCategory
22
    ): void {
23
        $yiiLogger = Yii::createObject('yii\log\Logger');
24
        Yii::setLogger($yiiLogger);
25
26
        $logger = new Logger();
27
        $logger->log($level, $message, $context);
28
        $message = Yii::getLogger()->messages[0];
29
        $this->assertSame($expectedMessage, $message[0]);
30
        $this->assertSame($expectedLevel, $message[1]);
31
        $this->assertSame($expectedCategory, $message[2]);
32
    }
33
34
    public function dataLog(): array
35
    {
36
        return [
37
            [LogLevel::EMERGENCY, 'foo', [], YiiLogger::LEVEL_ERROR, 'foo', 'application'],
38
            [LogLevel::ALERT, 'bar', ['__CATEGORY__' => __METHOD__], YiiLogger::LEVEL_ERROR, 'bar', __METHOD__],
39
            [LogLevel::CRITICAL, 'foo', [], YiiLogger::LEVEL_ERROR, 'foo', 'application'],
40
            [LogLevel::ERROR, 'foo', [], YiiLogger::LEVEL_ERROR, 'foo', 'application'],
41
            [LogLevel::WARNING, 'foo', [], YiiLogger::LEVEL_WARNING, 'foo', 'application'],
42
            [LogLevel::NOTICE, 'foo', [], YiiLogger::LEVEL_INFO, 'foo', 'application'],
43
            [LogLevel::INFO, 'foo', [], YiiLogger::LEVEL_INFO, 'foo', 'application'],
44
            [LogLevel::DEBUG, 'hi {name}', ['name' => 'foo'], YiiLogger::LEVEL_TRACE, 'hi foo', 'application'],
45
        ];
46
    }
47
48
    /**
49
     * @dataProvider dataConstruct
50
     */
51
    public function testConstruct(int $defaultLevel, string $categoryParam, array $levelMap): void
52
    {
53
        $logger = new Logger($categoryParam, $levelMap, $defaultLevel);
54
        foreach (['categoryParam', 'defaultLevel', 'levelMap'] as $name) {
55
            $property = new \ReflectionProperty(Logger::class, $name);
56
            $property->setAccessible(true);
57
            $this->assertSame($$name, $property->getValue($logger));
58
        }
59
    }
60
61
    public function dataConstruct(): array
62
    {
63
        return [
64
            [YiiLogger::LEVEL_ERROR, 'foo', []],
65
            [YiiLogger::LEVEL_WARNING, 'bar', [LogLevel::CRITICAL => YiiLogger::LEVEL_ERROR]],
66
        ];
67
    }
68
69
    /**
70
     * @dataProvider dataInterpolate
71
     */
72
    public function testInterpolate(string $message, array $context, string $expected): void
73
    {
74
        $logger = new Logger();
75
        $method = new \ReflectionMethod(Logger::class, 'interpolate');
76
        $method->setAccessible(true);
77
        $this->assertSame($expected, $method->invoke($logger, $message, $context));
78
    }
79
80
    public function dataInterpolate(): array
81
    {
82
        return [
83
            ['hello world', [], 'hello world'],
84
            ['hello {name}', ['name' => 'foo'], 'hello foo'],
85
        ];
86
    }
87
}
88