LoggerWrapperTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 98
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A dataLog() 0 13 1
A testLog() 0 10 1
A testPsrLogLevelIsMapped() 0 7 1
A testInvalidLogLevelIsMappedToDefault() 0 7 1
A testLogExceptionViaRenderer() 0 32 1
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')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Logger.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Logger.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Logger.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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'));
0 ignored issues
show
Documentation introduced by
new \AbacaphiliacTest\Ps...4Php\FooBar('FizzBuzz') is of type object<AbacaphiliacTest\PsrLog4Php\FooBar>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
        $actual = ob_get_clean();
105
        
106
        self::assertEquals("ERROR - FizzBuzz\n", $actual);
107
    }
108
}
109