ExceptionListenerTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 15 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\Entity\Payment;
6
use Loevgaard\DandomainAltapayBundle\EventListener\ExceptionListener;
7
use Loevgaard\DandomainAltapayBundle\Exception\Exception;
8
use Loevgaard\DandomainAltapayBundle\Exception\PaymentException;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
13
use Symfony\Component\HttpKernel\HttpKernelInterface;
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
        $this->listener = new ExceptionListener($this->engine);
0 ignored issues
show
Documentation introduced by
$this->engine is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Bundle\Fr...lating\EngineInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
        $this->request = new Request();
46
    }
47
48
    public function tearDown()
49
    {
50
        $this->engine = null;
51
        $this->listener = null;
52
    }
53
54
    public function testNotBundleException()
55
    {
56
        $event = $this->getGetResponseForExceptionEvent($this->request, new \Exception());
57
        $res = $this->listener->onKernelException($event);
58
        $this->assertTrue(false === $res);
59
    }
60
61
    public function testNormalException()
62
    {
63
        $exception = $this->getMockForAbstractClass(Exception::class);
64
        $event = $this->getGetResponseForExceptionEvent($this->request, $exception);
0 ignored issues
show
Documentation introduced by
$exception is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Exception>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
        $this->listener->onKernelException($event);
66
67
        $this->assertSame('response', $event->getResponse()->getContent());
68
    }
69
70 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...
71
    {
72
        $payment = $this->getMockForAbstractClass(Payment::class);
73
        $payment->setReferrer('referrer');
74
75
        $exception = PaymentException::create('exception', $this->request, $payment);
0 ignored issues
show
Documentation introduced by
$payment is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Loevgaard\Dandoma...yBundle\Entity\Payment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
77
        $event = $this->getGetResponseForExceptionEvent($this->request, $exception);
78
        $this->listener->onKernelException($event);
79
80
        $this->assertSame('responsereferrer', $event->getResponse()->getContent());
81
    }
82
83 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...
84
    {
85
        $payment = $this->getMockForAbstractClass(Payment::class);
86
        $payment->setCallBackServerUrl('example.com');
87
88
        $exception = PaymentException::create('exception', $this->request, $payment);
0 ignored issues
show
Documentation introduced by
$payment is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Loevgaard\Dandoma...yBundle\Entity\Payment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
90
        $event = $this->getGetResponseForExceptionEvent($this->request, $exception);
91
        $this->listener->onKernelException($event);
92
93
        $this->assertSame('responsehttp://example.com', $event->getResponse()->getContent());
94
    }
95
96
    protected function getGetResponseForExceptionEvent(Request $request, \Exception $e)
97
    {
98
        $mockKernel = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Kernel', ['', '']);
99
100
        return new GetResponseForExceptionEvent($mockKernel, $request, HttpKernelInterface::MASTER_REQUEST, $e);
0 ignored issues
show
Documentation introduced by
$mockKernel is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...el\HttpKernelInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
    }
102
}
103