Completed
Push — master ( d39756...dddd84 )
by Chad
01:13
created

NewRelicLoggerTest::logError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SubjectivePHPTest\Psr\Log;
4
5
use SubjectivePHP\Psr\Log\NewRelicLogger;
6
use Psr\Log\LogLevel;
7
8
/**
9
 * @coversDefaultClass \SubjectivePHP\Psr\Log\NewRelicLogger
10
 * @covers ::__construct
11
 * @covers ::<private>
12
 */
13
final class NewRelicLoggerTest extends \PHPUnit\Framework\TestCase
14
{
15
    /**
16
     * @test
17
     * @covers ::log
18
     *
19
     * @return void
20
     */
21
    public function logWithOnlyMessage()
22
    {
23
        $newRelicAgentMock = $this->getNewRelicAgentMock(LogLevel::CRITICAL, 'an error message', ['foo' => 'bar']);
24
        $logger = new NewRelicLogger($newRelicAgentMock);
25
        $logger->log(LogLevel::CRITICAL, 'an error message', ['foo' => 'bar']);
26
    }
27
28
    /**
29
     * @test
30
     * @covers ::log
31
     *
32
     * @return void
33
     */
34
    public function logWithException()
35
    {
36
        $exception = new \RuntimeException();
37
        $newRelicAgentMock = $this->getNewRelicAgentMock(LogLevel::EMERGENCY, 'an alert message', [], $exception);
38
        $logger = new NewRelicLogger($newRelicAgentMock);
39
        $logger->log(LogLevel::EMERGENCY, 'an alert message', ['exception' => $exception]);
40
    }
41
42
    /**
43
     * @test
44
     * @covers ::log
45
     *
46
     * @return void
47
     */
48
    public function logIgnoredLevel()
49
    {
50
        $newRelicAgentMock = $this->getMockBuilder('\\SobanVuex\\NewRelic\\Agent')->getMock();
51
        $newRelicAgentMock->expects($this->exactly(0))->method('addCustomParameter');
52
        $newRelicAgentMock->expects($this->exactly(0))->method('noticeError');
53
        $logger = new NewRelicLogger($newRelicAgentMock);
0 ignored issues
show
Documentation introduced by
$newRelicAgentMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SobanVuex\NewRelic\Agent>.

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...
54
        $logger->log(LogLevel::DEBUG, 'a debug message');
55
    }
56
57
    /**
58
     * @test
59
     * @covers ::log
60
     *
61
     * @return void
62
     */
63
    public function logWithNonScalarContext()
64
    {
65
        $newRelicAgentMock = $this->getNewRelicAgentMock(
66
            LogLevel::CRITICAL,
67
            'an error message',
68
            [
69
                'foo' => 'bar',
70
                'extra' => var_export(new \StdClass(), true),
71
            ]
72
        );
73
        $logger = new NewRelicLogger($newRelicAgentMock);
74
        $logger->log(LogLevel::CRITICAL, 'an error message', ['foo' => 'bar', 'extra' => new \StdClass()]);
75
    }
76
77
    /**
78
     * @test
79
     * @covers ::log
80
     *
81
     * @return void
82
     */
83
    public function logError()
84
    {
85
        $error = new \Error('an error message', E_ERROR);
0 ignored issues
show
Unused Code introduced by
The call to Error::__construct() has too many arguments starting with 'an error message'.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
86
        $exception = new \ErrorException(
87
            $error->getMessage(),
88
            0,
89
            $error->getCode(),
90
            $error->getFile(),
91
            $error->getLine()
92
        );
93
        $newRelicAgentMock = $this->getNewRelicAgentMock(LogLevel::EMERGENCY, 'an error message', [], $exception);
94
        $logger = new NewRelicLogger($newRelicAgentMock);
95
        $logger->log(LogLevel::EMERGENCY, 'an error message', ['exception' => $error]);
96
    }
97
98
    private function getNewRelicAgentMock(
99
        string $level,
100
        string $message,
101
        array $parameters,
102
        \Exception $exception = null
103
    ) {
104
        $newRelicAgentMock = $this->getMockBuilder('\\SobanVuex\\NewRelic\\Agent')->setMethods(
105
            ['addCustomParameter', 'noticeError']
106
        )->getMock();
107
        $consecutiveKeyValues = [['level', $level]];
108
        foreach ($parameters as $key => $value) {
109
            $consecutiveKeyValues[] = [$key, $value];
110
        }
111
112
        $newRelicAgentMock->expects($this->exactly(count($consecutiveKeyValues)))->method(
113
            'addCustomParameter'
114
        )->withConsecutive(...$consecutiveKeyValues);
115
        $newRelicAgentMock->expects($this->once())->method('noticeError')->with($message, $exception);
116
        return $newRelicAgentMock;
117
    }
118
}
119