Passed
Push — master ( 61f3a0...6b4c98 )
by Alexander
01:39
created

LoggerDispatchingTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 135
rs 10
c 0
b 0
f 0
wmc 7
1
<?php
2
3
namespace Yiisoft\Log {
4
5
    use Yiisoft\Log\Tests\LoggerDispatchingTest;
6
7
    function microtime($getAsFloat)
8
    {
9
        if (LoggerDispatchingTest::$microtimeIsMocked) {
10
            return LoggerDispatchingTest::microtime(func_get_args());
11
        }
12
13
        return \microtime($getAsFloat);
14
    }
15
}
16
17
namespace Yiisoft\Log\Tests {
18
19
    use Psr\Log\LogLevel;
20
    use Yiisoft\Log\Logger;
21
    use Yiisoft\Log\Target;
22
23
    /**
24
     * @group log
25
     * @method static int|float microtime($getAsFloat)
26
     */
27
    class LoggerDispatchingTest extends TestCase
28
    {
29
        /**
30
         * @var Logger
31
         */
32
        protected $logger;
33
34
        /**
35
         * @var bool
36
         */
37
        public static $microtimeIsMocked = false;
38
39
        /**
40
         * Array of static functions
41
         *
42
         * @var array
43
         */
44
        public static $functions = [];
45
46
        protected function setUp(): void
47
        {
48
            static::$microtimeIsMocked = false;
49
            $this->logger = new Logger();
50
        }
51
52
        /**
53
         * @covers \Yiisoft\Log\Logger::dispatch()
54
         */
55
        public function testDispatchWithDisabledTarget(): void
56
        {
57
            /** @var Target $target */
58
            $target = $this->getMockBuilder(Target::class)
59
                ->onlyMethods(['collect'])
60
                ->getMockForAbstractClass();
61
62
            $target->expects($this->never())->method($this->anything());
63
            $target->setEnabled(false);
64
65
            $logger = new Logger(['fakeTarget' => $target]);
66
            $this->setInaccessibleProperty($logger, 'messages', [
67
                [LogLevel::INFO, 'test', []]
68
            ]);
69
            $logger->flush(true);
70
        }
71
72
        /**
73
         * @covers \Yiisoft\Log\Logger::dispatch()
74
         */
75
        public function testDispatchWithSuccessTargetCollect(): void
76
        {
77
            $target = $this->getMockBuilder(Target::class)
78
                ->onlyMethods(['collect'])
79
                ->getMockForAbstractClass();
80
81
            $target->expects($this->once())
82
                ->method('collect')
83
                ->with(
84
                    $this->equalTo([
85
                        [LogLevel::INFO, 'test', []]
86
                    ]),
87
                    $this->equalTo(true)
88
                );
89
90
            $logger = new Logger(['fakeTarget' => $target]);
91
92
            $this->setInaccessibleProperty($logger, 'messages', [
93
                [LogLevel::INFO, 'test', []]
94
            ]);
95
            $logger->flush(true);
96
        }
97
98
        /**
99
         * @covers \Yiisoft\Log\Logger::dispatch()
100
         */
101
        public function testDispatchWithFakeTarget2ThrowExceptionWhenCollect(): void
102
        {
103
            static::$microtimeIsMocked = true;
104
105
            $target1 = $this->getMockBuilder(Target::class)
106
                ->onlyMethods(['collect'])
107
                ->getMockForAbstractClass();
108
109
            $target2 = $this->getMockBuilder(Target::class)
110
                ->onlyMethods(['collect'])
111
                ->getMockForAbstractClass();
112
113
            $target1->expects($this->exactly(2))
114
                ->method('collect')
115
                ->withConsecutive(
116
                    [$this->equalTo([]), $this->equalTo(true)],
117
                    [
118
                        [[
119
                            'Unable to send log via ' . get_class($target1) . ': Exception: some error',
120
                            LogLevel::WARNING,
121
                            'Yiisoft\Log\Logger::dispatch',
122
                            'time data',
123
                            [],
124
                        ]],
125
                        true,
126
                    ]
127
                );
128
129
            $target2->expects($this->once())
130
                ->method('collect')
131
                ->with(
132
                    $this->equalTo([]),
133
                    $this->equalTo(true)
134
                )->will($this->throwException(new \Exception('some error')));
135
136
            $logger = new Logger([
137
                'fakeTarget1' => $target1,
138
                'fakeTarget2' => $target2,
139
            ]);
140
141
            static::$functions['microtime'] = function ($arguments) {
142
                $this->assertEquals([true], $arguments);
143
                return 'time data';
144
            };
145
146
            $this->setInaccessibleProperty($logger, 'messages', []);
147
            $logger->flush(true);
148
        }
149
150
        /**
151
         * @param $name
152
         * @param $arguments
153
         * @return mixed
154
         */
155
        public static function __callStatic($name, $arguments)
156
        {
157
            if (isset(static::$functions[$name]) && is_callable(static::$functions[$name])) {
158
                $arguments = $arguments[0] ?? $arguments;
159
                return forward_static_call(static::$functions[$name], $arguments);
160
            }
161
            static::fail("Function '$name' has not implemented yet!");
162
        }
163
    }
164
}
165