Completed
Push — master ( fd7370...afa46a )
by Joachim
04:42
created

ExceptionListenerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 26.97 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 24
loc 89
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A tearDown() 0 5 1
A testNotBundleException() 0 6 1
A testNormalException() 0 8 1
A testPaymentExceptionWithReferrer() 12 12 1
A testPaymentExceptionWithCallbackServerUrl() 12 12 1
A getGetResponseForExceptionEvent() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\EventListener;
4
5
use Loevgaard\DandomainAltapayBundle\EventListener\ExceptionListener;
6
use Loevgaard\DandomainAltapayBundle\Exception\Exception;
7
use Loevgaard\DandomainAltapayBundle\Exception\PaymentException;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
11
use Symfony\Component\HttpKernel\HttpKernelInterface;
12
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
13
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
14
15
class ExceptionListenerTest extends TestCase
16
{
17
    /**
18
     * @var EngineInterface|\PHPUnit_Framework_MockObject_MockObject
19
     */
20
    protected $engine;
21
22
    /**
23
     * @var ExceptionListener
24
     */
25
    protected $listener;
26
27
    /**
28
     * @var Request
29
     */
30
    protected $request;
31
32
    public function setUp()
33
    {
34
        $this->engine = $this->getMockForAbstractClass(EngineInterface::class);
35
36
        // the engines render method will always return 'response' concatenated with the redirect variable
37
        $this->engine
38
            ->expects($this->any())
39
            ->method('render')
40
            ->willReturnCallback(function($template, $parameters) {
41
                return 'response'.$parameters['redirect'];
42
            });
43
        ;
44
45
        $this->listener = new ExceptionListener($this->engine);
46
        $this->request = new Request();
47
    }
48
49
    public function tearDown()
50
    {
51
        $this->engine = null;
52
        $this->listener = null;
53
    }
54
55
    public function testNotBundleException()
56
    {
57
        $event = $this->getGetResponseForExceptionEvent($this->request, new \Exception());
58
        $res = $this->listener->onKernelException($event);
59
        $this->assertTrue($res === false);
60
    }
61
62
    public function testNormalException()
63
    {
64
        $exception = $this->getMockForAbstractClass(Exception::class);
65
        $event = $this->getGetResponseForExceptionEvent($this->request, $exception);
66
        $this->listener->onKernelException($event);
67
68
        $this->assertSame('response', $event->getResponse()->getContent());
69
    }
70
71 View Code Duplication
    public function testPaymentExceptionWithReferrer()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $payment = $this->getMockForAbstractClass(Payment::class);
74
        $payment->setReferrer('referrer');
75
76
        $exception = PaymentException::create('exception', $this->request, $payment);
77
78
        $event = $this->getGetResponseForExceptionEvent($this->request, $exception);
79
        $this->listener->onKernelException($event);
80
81
        $this->assertSame('responsereferrer', $event->getResponse()->getContent());
82
    }
83
84 View Code Duplication
    public function testPaymentExceptionWithCallbackServerUrl()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        $payment = $this->getMockForAbstractClass(Payment::class);
87
        $payment->setCallBackServerUrl('example.com');
88
89
        $exception = PaymentException::create('exception', $this->request, $payment);
90
91
        $event = $this->getGetResponseForExceptionEvent($this->request, $exception);
92
        $this->listener->onKernelException($event);
93
94
        $this->assertSame('responsehttp://example.com', $event->getResponse()->getContent());
95
    }
96
97
    protected function getGetResponseForExceptionEvent(Request $request, \Exception $e)
98
    {
99
        $mockKernel = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Kernel', array('', ''));
100
101
        return new GetResponseForExceptionEvent($mockKernel, $request, HttpKernelInterface::MASTER_REQUEST, $e);
102
    }
103
}
104