Passed
Pull Request — master (#31)
by
unknown
13:33
created

WhoopsHandlerTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Graze\Monolog\Handler;
4
5
use Mockery as m;
6
use Monolog\Test\TestCase;
7
8
class WhoopsHandlerTest extends TestCase
9
{
10
    public function setUp(): void
11
    {
12
        if (!interface_exists('Whoops\Handler\HandlerInterface', true)) {
13
            $this->markTestSkipped('filp/whoops not installed');
14
        }
15
16
        $this->handlerWhoops = m::mock('Whoops\Handler\HandlerInterface');
0 ignored issues
show
Bug Best Practice introduced by
The property handlerWhoops 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 tearDown(): void
20
    {
21
        parent::tearDown();
22
        m::close();
23
    }
24
25
    public function testConstruct()
26
    {
27
        $this->assertInstanceOf('Graze\\Monolog\\Handler\\WhoopsHandler', new WhoopsHandler($this->handlerWhoops));
28
    }
29
30
    public function testInterface()
31
    {
32
        $this->assertInstanceOf('Monolog\\Handler\\HandlerInterface', new WhoopsHandler($this->handlerWhoops));
33
    }
34
35
    public function testHandleError()
36
    {
37
        $record = $this->getRecord(300, 'test', ['file' => 'bar', 'line' => 1]);
38
        $handlerMonolog = new WhoopsHandler($this->handlerWhoops);
39
40
        $this->handlerWhoops
41
            ->shouldReceive('setInspector')
42
            ->once()
43
            ->with(m::type('Whoops\Exception\Inspector'));
44
45
        $this->handlerWhoops
46
            ->shouldReceive('setRun')
47
            ->once()
48
            ->with(m::type('Whoops\Run'));
49
50
        $this->handlerWhoops
51
            ->shouldReceive('setException')
52
            ->once()
53
            ->with(m::type('Whoops\Exception\ErrorException'));
54
55
        $this->handlerWhoops
56
            ->shouldReceive('handle')
57
            ->once();
58
59
        $this->assertFalse($handlerMonolog->handle($record));
60
    }
61
62
    public function testHandleException()
63
    {
64
        $exception = new \Whoops\Exception\ErrorException('foo');
65
        $record = $this->getRecord(300, 'foo', ['exception' => $exception]);
66
        $handlerMonolog = new WhoopsHandler($this->handlerWhoops);
67
68
        $this->handlerWhoops
69
            ->shouldReceive('setInspector')
70
            ->once()
71
            ->with(m::type('Whoops\Exception\Inspector'));
72
73
        $this->handlerWhoops
74
            ->shouldReceive('setRun')
75
            ->once()
76
            ->with(m::type('Whoops\Run'));
77
78
        $this->handlerWhoops
79
            ->shouldReceive('setException')
80
            ->once()
81
            ->with($exception);
82
83
        $this->handlerWhoops
84
            ->shouldReceive('handle')
85
            ->once();
86
87
        $this->assertFalse($handlerMonolog->handle($record));
88
    }
89
90
    /**
91
     * @requires PHP 7
92
     */
93
    public function testHandleThrowable()
94
    {
95
        $exception = new \TypeError('foo');
96
        $record = $this->getRecord(300, 'foo', ['exception' => $exception]);
97
        $handlerMonolog = new WhoopsHandler($this->handlerWhoops);
98
99
        $this->handlerWhoops
100
            ->shouldReceive('setInspector')
101
            ->once()
102
            ->with(m::type('Whoops\Exception\Inspector'));
103
104
        $this->handlerWhoops
105
            ->shouldReceive('setRun')
106
            ->once()
107
            ->with(m::type('Whoops\Run'));
108
109
        $this->handlerWhoops
110
            ->shouldReceive('setException')
111
            ->once()
112
            ->with($exception);
113
114
        $this->handlerWhoops
115
            ->shouldReceive('handle')
116
            ->once();
117
118
        $this->assertFalse($handlerMonolog->handle($record));
119
    }
120
121
    /**
122
     * @dataProvider nonTriggeringData
123
     *
124
     * @param array $context
125
     */
126
    public function testContextWithNotEnoughInformationDoesNotTrigger(array $context)
127
    {
128
        $record = $this->getRecord(300, 'test', $context);
129
        $handlerMonolog = new WhoopsHandler($this->handlerWhoops);
130
        $this->assertFalse($handlerMonolog->handle($record));
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    public function nonTriggeringData()
137
    {
138
        return [
139
            [['file' => 'file']],
140
            [['line' => 1]],
141
            [[]],
142
            [['other' => 5]],
143
        ];
144
    }
145
}
146