1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbacaphiliacTest\PsrLog4Php; |
4
|
|
|
|
5
|
|
|
use Abacaphiliac\PsrLog4Php\LoggerWrapper; |
6
|
|
|
use Psr\Log\LogLevel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \Abacaphiliac\PsrLog4Php\LoggerWrapper |
10
|
|
|
*/ |
11
|
|
|
class LoggerWrapperTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|\Logger */ |
14
|
|
|
private $logger; |
15
|
|
|
/** @var LoggerWrapper */ |
16
|
|
|
private $sut; |
17
|
|
|
|
18
|
|
|
protected function setUp() |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
$this->logger = $this->getMockBuilder('\Logger')->disableOriginalConstructor()->getMock(); |
23
|
|
|
|
24
|
|
|
$this->sut = new LoggerWrapper($this->logger); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array[] |
29
|
|
|
*/ |
30
|
|
|
public function dataLog() |
31
|
|
|
{ |
32
|
|
|
return array( |
33
|
|
|
'emergency' => array('emergency', \LoggerLevel::getLevelFatal()), |
34
|
|
|
'alert' => array('alert', \LoggerLevel::getLevelFatal()), |
35
|
|
|
'critical' => array('critical', \LoggerLevel::getLevelFatal()), |
36
|
|
|
'error' => array('error', \LoggerLevel::getLevelError()), |
37
|
|
|
'warning' => array('warning', \LoggerLevel::getLevelWarn()), |
38
|
|
|
'notice' => array('notice', \LoggerLevel::getLevelWarn()), |
39
|
|
|
'info' => array('info', \LoggerLevel::getLevelInfo()), |
40
|
|
|
'debug' => array('debug', \LoggerLevel::getLevelDebug()), |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @dataProvider dataLog |
46
|
|
|
* @param string $method |
47
|
|
|
* @param string $level |
48
|
|
|
*/ |
49
|
|
|
public function testLog($method, $level) |
50
|
|
|
{ |
51
|
|
|
$message = 'FooBar'; |
52
|
|
|
$context = array('Foo' => 'Bar'); |
53
|
|
|
|
54
|
|
|
$this->logger->expects(self::once())->method('log') |
|
|
|
|
55
|
|
|
->with($level, self::isType('string')); |
56
|
|
|
|
57
|
|
|
$this->sut->{$method}($message, $context); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testPsrLogLevelIsMapped() |
61
|
|
|
{ |
62
|
|
|
$this->logger->expects(self::once())->method('log') |
|
|
|
|
63
|
|
|
->with(\LoggerLevel::getLevelFatal(), self::isType('string')); |
64
|
|
|
|
65
|
|
|
$this->sut->log(LogLevel::EMERGENCY, 'FooBar', array('Foo' => 'Bar')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testInvalidLogLevelIsMappedToDefault() |
69
|
|
|
{ |
70
|
|
|
$this->logger->expects(self::once())->method('log') |
|
|
|
|
71
|
|
|
->with(\LoggerLevel::getLevelError(), self::isType('string')); |
72
|
|
|
|
73
|
|
|
$this->sut->log('InvalidLevel', 'FooBar', array('Foo' => 'Bar')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testLogExceptionViaRenderer() |
77
|
|
|
{ |
78
|
|
|
\Logger::configure(array( |
79
|
|
|
'appenders' => array( |
80
|
|
|
'echo' => array( |
81
|
|
|
'class' => '\LoggerAppenderEcho', |
82
|
|
|
), |
83
|
|
|
), |
84
|
|
|
'rootLogger' => array( |
85
|
|
|
'level' => 'ERROR', |
86
|
|
|
'appenders' => array( |
87
|
|
|
'echo', |
88
|
|
|
), |
89
|
|
|
), |
90
|
|
|
'renderers' => array( |
91
|
|
|
'Exception' => array( |
92
|
|
|
'renderedClass' => 'AbacaphiliacTest\PsrLog4Php\FooBar', |
93
|
|
|
'renderingClass' => 'AbacaphiliacTest\PsrLog4Php\FooBarRenderer', |
94
|
|
|
), |
95
|
|
|
), |
96
|
|
|
), new \LoggerConfiguratorDefault()); |
97
|
|
|
|
98
|
|
|
$logger = \Logger::getLogger(__METHOD__); |
99
|
|
|
|
100
|
|
|
$sut = new LoggerWrapper($logger); |
101
|
|
|
|
102
|
|
|
ob_start(); |
103
|
|
|
$sut->error(new FooBar('FizzBuzz')); |
|
|
|
|
104
|
|
|
$actual = ob_get_clean(); |
105
|
|
|
|
106
|
|
|
self::assertEquals("ERROR - FizzBuzz\n", $actual); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: