Passed
Push — master ( ddd97a...25aa58 )
by Harry
06:46
created

RaygunHandlerTest::testHandleThrowable()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace Graze\Monolog\Handler;
3
4
use Mockery as m;
5
use Monolog\TestCase;
6
7
class RaygunHandlerTest extends TestCase
8
{
9
    public function setUp()
10
    {
11
        if (!class_exists('Raygun4php\RaygunClient')) {
12
            $this->markTestSkipped('mindscape/raygun4php not installed');
13
        }
14
15
        $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...
16
    }
17
18
    public function testConstruct()
19
    {
20
        $this->assertInstanceOf('Graze\\Monolog\\Handler\\RaygunHandler', new RaygunHandler($this->client));
21
    }
22
23
    public function testInterface()
24
    {
25
        $this->assertInstanceOf('Monolog\\Handler\\HandlerInterface', new RaygunHandler($this->client));
26
    }
27
28
    public function testGetFormatter()
29
    {
30
        $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

30
        $handler = new RaygunHandler($this->client, /** @scrutinizer ignore-type */ 'foo');
Loading history...
31
        $this->assertInstanceOf('Monolog\\Formatter\\NormalizerFormatter', $handler->getFormatter());
32
    }
33
34
    public function testHandleError()
35
    {
36
        $record = $this->getRecord(300,
37
            'foo', array(
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
38
                'file' => 'bar',
39
                'line' => 1,
40
            )
41
        );
42
        $record['context']['tags'] = array('foo');
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
43
        $record['context']['timestamp'] = 1234567890;
44
        $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...
45
        $formatted = array_merge($record,
46
            array(
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
47
                'tags' => array('foo', 'bar'),
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
48
                'timestamp' => 1234567890,
49
                'custom_data' => array('bar' => 'baz')
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
50
            )
51
        );
52
53
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
54
        $handler = new RaygunHandler($this->client);
55
        $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

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

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

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

95
             ->/** @scrutinizer ignore-call */ 
96
               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...
96
             ->once()
97
             ->with($record)
98
             ->andReturn($formatted);
99
        $this->client
100
             ->shouldReceive('SendException')
101
             ->once()
102
             ->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...
103
104
        $handler->handle($record);
105
    }
106
107
    public function testIsHandling()
108
    {
109
        $handler = new RaygunHandler($this->client);
110
111
        $exception = new \Exception('foo');
112
        $handlingRecord1 = $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...
113
114
        $this->assertTrue($handler->isHandling($handlingRecord1));
115
116
        $context = array(
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
117
            'file' => $exception->getFile(),
118
            'line' => $exception->getLine(),
119
        );
120
        $handlingRecord2 = $this->getRecord(300, 'bar', $context);
121
        $this->assertTrue($handler->isHandling($handlingRecord2));
122
123
0 ignored issues
show
introduced by
Multiple blank lines detected
Loading history...
124
        $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...
125
        $this->assertFalse($handler->isHandling($nonHandlingRecord));
126
        $this->assertFalse($handler->handle($nonHandlingRecord));
127
    }
128
129
    /**
130
     * @requires PHP 7
131
     */
132
    public function testHandleThrowable()
133
    {
134
        $exception = new \TypeError('foo');
135
        $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...
136
        $record['context']['tags'] = array('foo');
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
137
        $formatted = array_merge($record,
138
            array(
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
139
                'tags' => array('foo'),
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
140
                'custom_data' => array(),
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
141
                'timestamp' => null,
142
            )
143
        );
144
145
        $formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
146
        $handler = new RaygunHandler($this->client);
147
        $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

147
        $handler->setFormatter(/** @scrutinizer ignore-type */ $formatter);
Loading history...
148
149
        $formatter
150
            ->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

150
            ->/** @scrutinizer ignore-call */ 
151
              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...
151
            ->once()
152
            ->with($record)
153
            ->andReturn($formatted);
154
        $this->client
155
            ->shouldReceive('SendException')
156
            ->once()
157
            ->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...
158
159
        $handler->handle($record);
160
    }
161
}
162