Completed
Push — master ( 90c851...aff568 )
by Petre
02:13
created

AirbrakeExceptionListenerTest::testGivenThatTheListenerIsEnabledThenAirbrakeWillBeNotifiedOfConsoleExceptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
5
namespace SM\AirbrakeBundle\Tests\Listener;
6
7
use SM\AirbrakeBundle\Listener\AirbrakeExceptionListener;
8
9
/**
10
 * Test the functionality of the exception listener.
11
 *
12
 * @package SM\AirbrakeBundle\Tests\Listener
13
 * @author  Petre Pătrașc <[email protected]>
14
 */
15
class AirbrakeExceptionListenerTest extends \PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * @var \Exception
19
     */
20
    protected $exception;
21
22
    /**
23
     * @inheritDoc
24
     */
25
    protected function setUp()
26
    {
27
        $this->exception = new \Exception;
28
    }
29
30
    public function testGivenThatTheListenerIsDisabledThenAirbrakeWillNotBeNotifiedOfKernelExceptions()
31
    {
32
        $airbrakeService = $this->getAirbrakeService();
33
        $airbrakeService->expects($this->never())->method('notify');
34
35
        $listener = new AirbrakeExceptionListener(false, $airbrakeService);
36
        $listener->onKernelException($this->getKernelExceptionEvent());
37
    }
38
39
    public function testGivenThatTheListenerIsDisabledThenAirbrakeWillNotBeNotifiedOfConsoleExceptions()
40
    {
41
        $airbrakeService = $this->getAirbrakeService();
42
        $airbrakeService->expects($this->never())->method('notify');
43
44
        $listener = new AirbrakeExceptionListener(false, $airbrakeService);
45
        $listener->onConsoleException($this->getConsoleExceptionEvent());
46
    }
47
48
    public function testGivenThatTheListenerIsEnabledThenAirbrakeWillBeNotifiedOfKernelExceptions()
49
    {
50
        $airbrakeService = $this->getAirbrakeService();
51
        $airbrakeService->expects($this->once())->method('notify');
52
53
        $listener = new AirbrakeExceptionListener(true, $airbrakeService);
54
        $listener->onKernelException($this->getKernelExceptionEvent());
55
    }
56
57
    public function testGivenThatTheListenerIsEnabledThenAirbrakeWillBeNotifiedOfConsoleExceptions()
58
    {
59
        $airbrakeService = $this->getAirbrakeService();
60
        $airbrakeService->expects($this->once())->method('notify');
61
62
        $listener = new AirbrakeExceptionListener(true, $airbrakeService);
63
        $listener->onConsoleException($this->getConsoleExceptionEvent());
64
    }
65
66
    /**
67
     * @return \PHPUnit_Framework_MockObject_MockObject
68
     */
69
    protected function getAirbrakeService():\PHPUnit_Framework_MockObject_MockObject
70
    {
71
        $airbrakeService = $this->getMockBuilder('SM\AirbrakeBundle\Service\AirbrakeService')
72
            ->disableOriginalConstructor()
73
            ->setMethods(['notify'])
74
            ->getMock();
75
76
        return $airbrakeService;
77
    }
78
79
    /**
80
     * Generate a kernel exception event signature.
81
     *
82
     * @return \PHPUnit_Framework_MockObject_MockObject
83
     */
84
    protected function getKernelExceptionEvent(): \PHPUnit_Framework_MockObject_MockObject
85
    {
86
        $kernelException = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent')
87
            ->disableOriginalConstructor()
88
            ->setMethods(['getException'])
89
            ->getMock();
90
        $kernelException->expects($this->any())->method('getException')->willReturn($this->exception);
91
92
        return $kernelException;
93
    }
94
95
    /**
96
     * Generate a console exception event signature.
97
     *
98
     * @return \PHPUnit_Framework_MockObject_MockObject
99
     */
100
    protected function getConsoleExceptionEvent(): \PHPUnit_Framework_MockObject_MockObject
101
    {
102
        $consoleException = $this->getMockBuilder('Symfony\Component\Console\Event\ConsoleExceptionEvent')
103
            ->disableOriginalConstructor()
104
            ->setMethods(['getException'])
105
            ->getMock();
106
        $consoleException->expects($this->any())->method('getException')->willReturn($this->exception);
107
108
        return $consoleException;
109
    }
110
}
111