RaygunHandlerTest::testHandleThrowable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 29
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace Graze\Monolog\Handler;
4
5
use Mockery as m;
6
use Monolog\TestCase;
7
8
class RaygunHandlerTest extends TestCase
9
{
10
    public function setUp()
11
    {
12
        if (!class_exists('Raygun4php\RaygunClient')) {
13
            $this->markTestSkipped('mindscape/raygun4php not installed');
14
        }
15
16
        $this->client = m::mock('Raygun4php\RaygunClient');
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
    }
18
19
    public function testConstruct()
20
    {
21
        $this->assertInstanceOf('Graze\\Monolog\\Handler\\RaygunHandler', new RaygunHandler($this->client));
22
    }
23
24
    public function testInterface()
25
    {
26
        $this->assertInstanceOf('Monolog\\Handler\\HandlerInterface', new RaygunHandler($this->client));
27
    }
28
29
    public function testGetFormatter()
30
    {
31
        $handler = new RaygunHandler($this->client, 'foo');
0 ignored issues
show
Bug introduced by
'foo' of type string is incompatible with the type integer expected by parameter $level of Graze\Monolog\Handler\RaygunHandler::__construct(). ( Ignorable by Annotation )

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

31
        $handler = new RaygunHandler($this->client, /** @scrutinizer ignore-type */ 'foo');
Loading history...
32
        $this->assertInstanceOf('Monolog\\Formatter\\NormalizerFormatter', $handler->getFormatter());
33
    }
34
35
    public function testHandleError()
36
    {
37
        $record = $this->getRecord(
38
            300,
39
            'foo',
40
            [
41
                'file' => 'bar',
42
                'line' => 1,
43
            ]
44
        );
45
        $record['context']['tags'] = ['foo'];
46
        $record['context']['timestamp'] = 1234567890;
47
        $record['extra'] = ['bar' => 'baz', 'tags' => ['bar']];
48
        $formatted = array_merge(
49
            $record,
50
            [
51
                'tags' => ['foo', 'bar'],
52
                'timestamp' => 1234567890,
53
                'custom_data' => ['bar' => 'baz']
54
            ]
55
        );
56
57
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
58
        $handler = new RaygunHandler($this->client);
59
        $handler->setFormatter($formatter);
0 ignored issues
show
Bug introduced by
$formatter of type Mockery\MockInterface is incompatible with the type Monolog\Formatter\FormatterInterface expected by parameter $formatter of Monolog\Handler\AbstractHandler::setFormatter(). ( Ignorable by Annotation )

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

59
        $handler->setFormatter(/** @scrutinizer ignore-type */ $formatter);
Loading history...
60
61
        $formatter
62
            ->shouldReceive('format')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'format'. ( Ignorable by Annotation )

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

62
            ->/** @scrutinizer ignore-call */ 
63
              shouldReceive('format')

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...
63
            ->once()
64
            ->with($record)
65
            ->andReturn($formatted);
66
        $this->client
67
            ->shouldReceive('SendError')
68
            ->once()
69
            ->with(0, 'foo', 'bar', 1, ['foo', 'bar'], ['bar' => 'baz'], 1234567890);
70
71
        $handler->handle($record);
72
    }
73
74
    public function testHandleException()
75
    {
76
        $exception = new \Exception('foo');
77
        $record = $this->getRecord(300, 'foo', ['exception' => $exception]);
78
        $record['extra'] = ['bar' => 'baz', 'tags' => ['foo', 'bar']];
79
        $record['extra']['timestamp'] = 1234567890;
80
        $formatted = array_merge(
81
            $record,
82
            [
83
                'tags' => ['foo', 'bar'],
84
                'timestamp' => 1234567890,
85
                'custom_data' => ['bar' => 'baz']
86
            ]
87
        );
88
        $formatted['context']['exception'] = [
89
            'class'   => get_class($exception),
90
            'message' => $exception->getMessage(),
91
            'code'    => $exception->getCode(),
92
            'file'    => $exception->getFile() . ':' . $exception->getLine(),
93
        ];
94
95
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
96
        $handler = new RaygunHandler($this->client);
97
        $handler->setFormatter($formatter);
0 ignored issues
show
Bug introduced by
$formatter of type Mockery\MockInterface is incompatible with the type Monolog\Formatter\FormatterInterface expected by parameter $formatter of Monolog\Handler\AbstractHandler::setFormatter(). ( Ignorable by Annotation )

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

97
        $handler->setFormatter(/** @scrutinizer ignore-type */ $formatter);
Loading history...
98
99
        $formatter
100
            ->shouldReceive('format')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'format'. ( Ignorable by Annotation )

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

100
            ->/** @scrutinizer ignore-call */ 
101
              shouldReceive('format')

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...
101
            ->once()
102
            ->with($record)
103
            ->andReturn($formatted);
104
        $this->client
105
            ->shouldReceive('SendException')
106
            ->once()
107
            ->with($exception, ['foo', 'bar'], ['bar' => 'baz'], 1234567890);
108
109
        $handler->handle($record);
110
    }
111
112
    public function testHandleEmptyDoesNothing()
113
    {
114
        $record = $this->getRecord(300, 'bar');
115
        $record['extra'] = ['bar' => 'baz', 'tags' => ['foo', 'bar']];
116
        $record['extra']['timestamp'] = 1234567890;
117
        $formatted = array_merge(
118
            $record,
119
            [
120
                'tags'        => ['foo', 'bar'],
121
                'timestamp'   => 1234567890,
122
                'custom_data' => ['bar' => 'baz'],
123
            ]
124
        );
125
126
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
127
        $handler = new RaygunHandler($this->client);
128
        $handler->setFormatter($formatter);
0 ignored issues
show
Bug introduced by
$formatter of type Mockery\MockInterface is incompatible with the type Monolog\Formatter\FormatterInterface expected by parameter $formatter of Monolog\Handler\AbstractHandler::setFormatter(). ( Ignorable by Annotation )

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

128
        $handler->setFormatter(/** @scrutinizer ignore-type */ $formatter);
Loading history...
129
130
        $formatter
131
            ->shouldReceive('format')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'format'. ( Ignorable by Annotation )

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

131
            ->/** @scrutinizer ignore-call */ 
132
              shouldReceive('format')

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...
132
            ->once()
133
            ->with($record)
134
            ->andReturn($formatted);
135
136
        $handler->handle($record);
137
    }
138
139
    /**
140
     * @requires PHP 7
141
     */
142
    public function testHandleThrowable()
143
    {
144
        $exception = new \TypeError('foo');
145
        $record = $this->getRecord(300, 'foo', ['exception' => $exception]);
146
        $record['context']['tags'] = ['foo'];
147
        $formatted = array_merge(
148
            $record,
149
            [
150
                'tags'        => ['foo'],
151
                'custom_data' => [],
152
                'timestamp'   => null,
153
            ]
154
        );
155
156
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
157
        $handler = new RaygunHandler($this->client);
158
        $handler->setFormatter($formatter);
0 ignored issues
show
Bug introduced by
$formatter of type Mockery\MockInterface is incompatible with the type Monolog\Formatter\FormatterInterface expected by parameter $formatter of Monolog\Handler\AbstractHandler::setFormatter(). ( Ignorable by Annotation )

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

158
        $handler->setFormatter(/** @scrutinizer ignore-type */ $formatter);
Loading history...
159
160
        $formatter
161
            ->shouldReceive('format')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'format'. ( Ignorable by Annotation )

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

161
            ->/** @scrutinizer ignore-call */ 
162
              shouldReceive('format')

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...
162
            ->once()
163
            ->with($record)
164
            ->andReturn($formatted);
165
        $this->client
166
            ->shouldReceive('SendException')
167
            ->once()
168
            ->with($exception, ['foo'], [], null);
169
170
        $handler->handle($record);
171
    }
172
}
173