Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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 |