Completed
Push — master ( 1d6c1f...26ec6b )
by Harry
06:29
created

RaygunHandlerTest::testHandleEmptyDoesNothing()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 24
rs 8.9713
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(300,
38
            'foo',
39
            [
40
                'file' => 'bar',
41
                'line' => 1,
42
            ]
43
        );
44
        $record['context']['tags'] = ['foo'];
45
        $record['context']['timestamp'] = 1234567890;
46
        $record['extra'] = ['bar' => 'baz', 'tags' => ['bar']];
47
        $formatted = array_merge($record,
48
            [
49
                'tags'        => ['foo', 'bar'],
50
                'timestamp'   => 1234567890,
51
                'custom_data' => ['bar' => 'baz'],
52
            ]
53
        );
54
55
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
56
        $handler = new RaygunHandler($this->client);
57
        $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

57
        $handler->setFormatter(/** @scrutinizer ignore-type */ $formatter);
Loading history...
58
59
        $formatter
60
            ->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

60
            ->/** @scrutinizer ignore-call */ 
61
              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...
61
            ->once()
62
            ->with($record)
63
            ->andReturn($formatted);
64
        $this->client
65
            ->shouldReceive('SendError')
66
            ->once()
67
            ->with(0, 'foo', 'bar', 1, ['foo', 'bar'], ['bar' => 'baz'], 1234567890);
68
69
        $handler->handle($record);
70
    }
71
72
    public function testHandleException()
73
    {
74
        $exception = new \Exception('foo');
75
        $record = $this->getRecord(300, 'foo', ['exception' => $exception]);
76
        $record['extra'] = ['bar' => 'baz', 'tags' => ['foo', 'bar']];
77
        $record['extra']['timestamp'] = 1234567890;
78
        $formatted = array_merge($record,
79
            [
80
                'tags'        => ['foo', 'bar'],
81
                'timestamp'   => 1234567890,
82
                'custom_data' => ['bar' => 'baz'],
83
            ]
84
        );
85
        $formatted['context']['exception'] = [
86
            'class'   => get_class($exception),
87
            'message' => $exception->getMessage(),
88
            'code'    => $exception->getCode(),
89
            'file'    => $exception->getFile() . ':' . $exception->getLine(),
90
        ];
91
92
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
93
        $handler = new RaygunHandler($this->client);
94
        $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

94
        $handler->setFormatter(/** @scrutinizer ignore-type */ $formatter);
Loading history...
95
96
        $formatter
97
            ->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

97
            ->/** @scrutinizer ignore-call */ 
98
              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...
98
            ->once()
99
            ->with($record)
100
            ->andReturn($formatted);
101
        $this->client
102
            ->shouldReceive('SendException')
103
            ->once()
104
            ->with($exception, ['foo', 'bar'], ['bar' => 'baz'], 1234567890);
105
106
        $handler->handle($record);
107
    }
108
109
    public function testHandleEmptyDoesNothing()
110
    {
111
        $record = $this->getRecord(300, 'bar');
112
        $record['extra'] = ['bar' => 'baz', 'tags' => ['foo', 'bar']];
113
        $record['extra']['timestamp'] = 1234567890;
114
        $formatted = array_merge($record,
115
            [
116
                'tags'        => ['foo', 'bar'],
117
                'timestamp'   => 1234567890,
118
                'custom_data' => ['bar' => 'baz'],
119
            ]
120
        );
121
122
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
123
        $handler = new RaygunHandler($this->client);
124
        $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

124
        $handler->setFormatter(/** @scrutinizer ignore-type */ $formatter);
Loading history...
125
126
        $formatter
127
            ->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

127
            ->/** @scrutinizer ignore-call */ 
128
              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...
128
            ->once()
129
            ->with($record)
130
            ->andReturn($formatted);
131
132
        $handler->handle($record);
133
    }
134
135
    /**
136
     * @requires PHP 7
137
     */
138
    public function testHandleThrowable()
139
    {
140
        $exception = new \TypeError('foo');
141
        $record = $this->getRecord(300, 'foo', ['exception' => $exception]);
142
        $record['context']['tags'] = ['foo'];
143
        $formatted = array_merge($record,
144
            [
145
                'tags'        => ['foo'],
146
                'custom_data' => [],
147
                'timestamp'   => null,
148
            ]
149
        );
150
151
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
152
        $handler = new RaygunHandler($this->client);
153
        $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

153
        $handler->setFormatter(/** @scrutinizer ignore-type */ $formatter);
Loading history...
154
155
        $formatter
156
            ->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

156
            ->/** @scrutinizer ignore-call */ 
157
              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...
157
            ->once()
158
            ->with($record)
159
            ->andReturn($formatted);
160
        $this->client
161
            ->shouldReceive('SendException')
162
            ->once()
163
            ->with($exception, ['foo'], [], null);
164
165
        $handler->handle($record);
166
    }
167
}
168