Test Failed
Push — master ( e47890...96dce3 )
by Jim
03:33
created

LogManagerTest::testAgencyMethods()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 28
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/18/2018
6
 * Time: 3:23 PM
7
 */
8
9
namespace TimSDK\Foundation\Log;
10
11
use Monolog\Logger;
12
use TimSDK\Tests\TestCase;
13
use TimSDK\Foundation\Config;
14
use Monolog\Handler\ErrorLogHandler;
15
use TimSDK\Container\ServiceContainer;
16
17
class LogManagerTest extends TestCase
18
{
19
    public function testStack()
20
    {
21
        $app = new ServiceContainer([], [
22
            'config' => new Config([
23
                'log' => [
24
                    'default'  => 'errorlog',
25
                    'channels' => [
26
                        'stack'    => [
27
                            'driver'   => 'stack',
28
                            'channels' => ['errorlog', 'single'],
29
                        ],
30
                        'errorlog' => [
31
                            'driver' => 'errorlog',
32
                            'type'   => ErrorLogHandler::OPERATING_SYSTEM,
33
                            'level'  => 'debug',
34
                        ],
35
                        'single'   => [
36
                            'driver' => 'single',
37
                            'path'   => __DIR__ . '/logs/tim-sdk.log',
38
                            'level'  => 'debug',
39
                        ],
40
                    ],
41
                ]
42
            ])
43
        ]);
44
45
        $log = new LogManager($app);
46
        $this->assertInstanceOf(ErrorLogHandler::class, $log->stack(['errorlog', 'single'])->getHandlers()[0]);
47
        $this->assertInstanceOf(ErrorLogHandler::class, $log->channel('stack')->getHandlers()[0]);
0 ignored issues
show
Bug introduced by
The method getHandlers() does not exist on Psr\Log\LoggerInterface. It seems like you code against a sub-type of Psr\Log\LoggerInterface such as TimSDK\Foundation\Log\LogManager or Monolog\Logger. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        $this->assertInstanceOf(ErrorLogHandler::class, $log->channel('stack')->/** @scrutinizer ignore-call */ getHandlers()[0]);
Loading history...
48
        $this->assertInstanceOf(ErrorLogHandler::class, $log->driver('stack')->getHandlers()[0]);
49
    }
50
51
    public function testResolveUndefinedDriver()
52
    {
53
        $app = new ServiceContainer([]);
54
        $log = \Mockery::mock(LogManager::class.'[createEmergencyLogger]', [$app])->shouldAllowMockingProtectedMethods();
55
56
        $emergencyLogger = \Mockery::mock(Logger::class);
57
        $log->shouldReceive('createEmergencyLogger')->andReturn($emergencyLogger)->once();
58
        $emergencyLogger->shouldReceive('emergency')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'emergency'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $emergencyLogger->/** @scrutinizer ignore-call */ 
59
                          shouldReceive('emergency')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
59
            ->with('Unable to create configured logger. Using emergency logger.', \Mockery::on(function ($data) {
60
                $this->assertArrayHasKey('exception', $data);
61
                $this->assertInstanceOf(\InvalidArgumentException::class, $data['exception']);
62
                $this->assertSame('Log [bad-name] is not defined.', $data['exception']->getMessage());
63
64
                return true;
65
            }));
66
        $log->driver('bad-name');
67
    }
68
69
    public function testResolveCustomCreator()
70
    {
71
        $app = new ServiceContainer([], [
72
            'config' => new Config([
73
                'log' => [
74
                    'channels' => [
75
                        'custom' => [
76
                            'driver' => 'mylog',
77
                            'key' => 'value',
78
                            'level' => 'debug',
79
                        ],
80
                    ],
81
                ],
82
            ]),
83
        ]);
84
85
        $log = new LogManager($app);
86
        $log->extend('mylog', function () {
87
            return 'mylog';
88
        });
89
90
        $this->assertSame('mylog', $log->driver('custom'));
91
    }
92
93
    public function testUnsupportedDriver()
94
    {
95
        $app = new ServiceContainer([], [
96
            'config' => new Config([
97
                'log' => [
98
                    'channels' => [
99
                        'custom' => [
100
                            'driver' => 'abcde',
101
                            'key' => 'value',
102
                            'level' => 'debug',
103
                        ],
104
                    ],
105
                ],
106
            ]),
107
        ]);
108
109
        $log = \Mockery::mock(LogManager::class.'[createEmergencyLogger]', [$app])->shouldAllowMockingProtectedMethods();
110
        $emergencyLogger = \Mockery::mock(Logger::class);
111
        $log->shouldReceive('createEmergencyLogger')->andReturn($emergencyLogger)->once();
112
        $emergencyLogger->shouldReceive('emergency')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'emergency'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
        $emergencyLogger->/** @scrutinizer ignore-call */ 
113
                          shouldReceive('emergency')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
113
            ->with('Unable to create configured logger. Using emergency logger.', \Mockery::on(function ($data) {
114
                $this->assertArrayHasKey('exception', $data);
115
                $this->assertInstanceOf(\InvalidArgumentException::class, $data['exception']);
116
                $this->assertSame('Driver [abcde] is not supported.', $data['exception']->getMessage());
117
118
                return true;
119
            }))->once();
120
        $log->driver('custom');
121
    }
122
123
    public function testLogAgencyMethod()
124
    {
125
        $app = new ServiceContainer([], [
126
            'config' => new Config([
127
                'log' => [
128
                    'default' => 'single',
129
                    'channels' => [
130
                        'single' => [
131
                            'driver' => 'single',
132
                        ],
133
                    ],
134
                ],
135
            ]),
136
        ]);
137
        $log = \Mockery::mock(LogManager::class.'[createSingleDriver]', [$app])->shouldAllowMockingProtectedMethods();
138
139
        $logger = \Mockery::mock(Logger::class);
140
        $log->shouldReceive('createSingleDriver')->andReturn($logger)->once();
141
        $logger->shouldReceive('log')->with('debug', 'log message', [])->once();
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'log'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        $logger->/** @scrutinizer ignore-call */ 
142
                 shouldReceive('log')->with('debug', 'log message', [])->once();

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
142
143
        $log->log('debug', 'log message');
144
    }
145
146
    public function testAgencyMethods()
147
    {
148
        $app = new ServiceContainer([], [
149
            'config' => new Config([
150
                'log' => [
151
                    'default' => 'single',
152
                    'channels' => [
153
                        'single' => [
154
                            'driver' => 'single',
155
                        ],
156
                    ],
157
                ],
158
            ]),
159
        ]);
160
        $log = \Mockery::mock(LogManager::class.'[createSingleDriver]', [$app])->shouldAllowMockingProtectedMethods();
161
162
        $logger = \Mockery::mock(Logger::class);
163
164
        $log->shouldReceive('createSingleDriver')->andReturn($logger)->once();
165
        $logger->shouldReceive('log')->withAnyArgs()->once();
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'log'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
        $logger->/** @scrutinizer ignore-call */ 
166
                 shouldReceive('log')->withAnyArgs()->once();

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
166
        $logger->shouldReceive('emergency')->with('emergency message', [])->once();
167
        $logger->shouldReceive('alert')->with('alert message', [])->once();
168
        $logger->shouldReceive('critical')->with('critical message', [])->once();
169
        $logger->shouldReceive('error')->with('error message', [])->once();
170
        $logger->shouldReceive('warning')->with('warning message', [])->once();
171
        $logger->shouldReceive('notice')->with('notice message', [])->once();
172
        $logger->shouldReceive('info')->with('info message', [])->once();
173
        $logger->shouldReceive('debug')->with('debug message', [])->once();
174
175
        $log->log('debug', 'log message');
176
        $log->emergency('emergency message');
177
        $log->alert('alert message');
178
        $log->critical('critical message');
179
        $log->error('error message');
180
        $log->warning('warning message');
181
        $log->notice('notice message');
182
        $log->info('info message');
183
        $log->debug('debug message');
184
    }
185
186
    public function testSetDefaultDriver()
187
    {
188
        $app = new ServiceContainer([], [
189
            'config' => new Config([
190
                'log' => [
191
                    'channels' => [
192
                        'single' => [
193
                            'driver' => 'single',
194
                        ],
195
                    ],
196
                ],
197
            ]),
198
        ]);
199
        $log = \Mockery::mock(LogManager::class.'[createSingleDriver]', [$app])->shouldAllowMockingProtectedMethods();
200
201
        $logger = \Mockery::mock(Logger::class);
202
203
        $this->assertNull($log->getDefaultDriver());
204
205
        $log->setDefaultDriver('single');
206
207
        $log->shouldReceive('createSingleDriver')->andReturn($logger)->once();
208
        $logger->shouldReceive('log')->withAnyArgs()->once();
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'log'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

208
        $logger->/** @scrutinizer ignore-call */ 
209
                 shouldReceive('log')->withAnyArgs()->once();

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
209
        $logger->shouldReceive('debug')->with('debug message', [])->once();
210
211
        $log->debug('debug message');
212
213
        $this->assertSame('single', $log->getDefaultDriver());
214
    }
215
216
    public function testDriverCreators()
217
    {
218
        $app = new ServiceContainer([], [
219
            'config' => new Config([
220
                'log' => [
221
                    'channels' => [
222
                        'single' => [
223
                            'driver' => 'single',
224
                        ],
225
                    ],
226
                ],
227
            ]),
228
        ]);
229
        $log = \Mockery::mock(LogManager::class, [$app])
230
            ->shouldAllowMockingProtectedMethods()
231
            ->shouldDeferMissing();
232
233
        $this->assertInstanceOf(Logger::class, $log->createStackDriver(['channels' => ['single']]));
234
        $this->assertInstanceOf(Logger::class, $log->createSlackDriver(['url' => 'https://easywechat.com']));
235
        $this->assertInstanceOf(Logger::class, $log->createDailyDriver(['path' => '/path/to/file.log']));
236
        $this->assertInstanceOf(Logger::class, $log->createSyslogDriver([]));
237
        $this->assertInstanceOf(Logger::class, $log->createErrorlogDriver([]));
238
    }
239
240
    /**
241
     * @expectedException \InvalidArgumentException
242
     * @expectedExceptionMessage Invalid log level.
243
     */
244
    public function testInvalidLevel()
245
    {
246
        $app = new ServiceContainer([]);
247
        $log = \Mockery::mock(LogManager::class, [$app])
248
            ->shouldAllowMockingProtectedMethods()
249
            ->shouldDeferMissing();
250
251
        $log->level([
252
            'level' => 'undefined',
253
        ]);
254
    }
255
256
    public function testCall()
257
    {
258
        $app = new ServiceContainer([]);
259
        $log = new LogManager($app);
260
        $this->assertInternalType('array', $log->getHandlers());
0 ignored issues
show
Bug introduced by
The method getHandlers() does not exist on TimSDK\Foundation\Log\LogManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

260
        $this->assertInternalType('array', $log->/** @scrutinizer ignore-call */ getHandlers());
Loading history...
261
    }
262
}
263