Completed
Push — master ( 2f49e8...1d6c1f )
by Harry
05:44
created

RaygunHandlerTest::isHandleData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Graze\Monolog\Handler;
3
4
use Mockery as m;
5
use Monolog\Logger;
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', array(
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
39
                'file' => 'bar',
40
                'line' => 1,
41
            )
42
        );
43
        $record['context']['tags'] = array('foo');
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
44
        $record['context']['timestamp'] = 1234567890;
45
        $record['extra'] = array('bar' => 'baz', 'tags' => array('bar'));
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
46
        $formatted = array_merge($record,
47
            array(
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
48
                'tags' => array('foo', 'bar'),
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
49
                'timestamp' => 1234567890,
50
                'custom_data' => array('bar' => 'baz')
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
51
            )
52
        );
53
54
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
55
        $handler = new RaygunHandler($this->client);
56
        $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

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

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

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

96
             ->/** @scrutinizer ignore-call */ 
97
               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...
97
             ->once()
98
             ->with($record)
99
             ->andReturn($formatted);
100
        $this->client
101
             ->shouldReceive('SendException')
102
             ->once()
103
             ->with($exception, array('foo', 'bar'), array('bar' => 'baz'), 1234567890);
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
104
105
        $handler->handle($record);
106
    }
107
108
    /**
109
     * @dataProvider isHandleData
110
     *
111
     * @param array $record
112
     * @param bool $expected
113
     */
114
    public function testIsHandling(array $record, $expected)
115
    {
116
        $handler = new RaygunHandler($this->client);
117
118
        $this->assertEquals($expected, $handler->isHandling($record));
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    public function isHandleData()
125
    {
126
        return [
127
            [$this->getRecord(300, 'foo', ['exception' => new \Exception('foo')]), true],
128
            [$this->getRecord(300, 'bar', ['file' => '/a/path/to/a/file', 'line' => 123]), true],
129
            [$this->getRecord(300, 'baz', ['file' => '/a/path/to/a/file']), false],
130
            [$this->getRecord(300, 'faz', ['line' => 456]), false],
131
            // no context entry
132
            [
133
                [
134
                    'message'    => 'foobar',
135
                    'level'      => 300,
136
                    'level_name' => Logger::getLevelName(300),
137
                    'channel'    => 'test',
138
                    'datetime'   => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true))),
139
                    'extra'      => [],
140
                ],
141
                false,
142
            ],
143
        ];
144
    }
145
146
    public function testHandleCallsIsHandlingFirst()
147
    {
148
        $handler = new RaygunHandler($this->client);
149
        $nonHandlingRecord = $this->getRecord(300, 'baz', array());
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
150
        $this->assertFalse($handler->isHandling($nonHandlingRecord));
151
        $this->assertFalse($handler->handle($nonHandlingRecord));
152
    }
153
154
    /**
155
     * @requires PHP 7
156
     */
157
    public function testHandleThrowable()
158
    {
159
        $exception = new \TypeError('foo');
160
        $record = $this->getRecord(300, 'foo', array('exception' => $exception));
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
161
        $record['context']['tags'] = array('foo');
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
162
        $formatted = array_merge($record,
163
            array(
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
164
                'tags' => array('foo'),
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
165
                'custom_data' => array(),
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
166
                'timestamp' => null,
167
            )
168
        );
169
170
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
171
        $handler = new RaygunHandler($this->client);
172
        $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

172
        $handler->setFormatter(/** @scrutinizer ignore-type */ $formatter);
Loading history...
173
174
        $formatter
175
            ->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

175
            ->/** @scrutinizer ignore-call */ 
176
              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...
176
            ->once()
177
            ->with($record)
178
            ->andReturn($formatted);
179
        $this->client
180
            ->shouldReceive('SendException')
181
            ->once()
182
            ->with($exception, array('foo'), array(), null);
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
183
184
        $handler->handle($record);
185
    }
186
}
187