ControllerListenerTest::testAnnotation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\EventListener;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Loevgaard\DandomainAltapayBundle\EventListener\ControllerListener;
7
use Loevgaard\DandomainAltapayBundle\Http\TransactionLogger;
8
use Loevgaard\DandomainAltapayBundle\Tests\EventListener\Fixture\ControllerWithAnnotation;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
12
use Symfony\Component\HttpKernel\HttpKernelInterface;
13
14
class ControllerListenerTest extends TestCase
15
{
16
    /**
17
     * @var Request
18
     */
19
    protected $request;
20
21
    /**
22
     * @var ControllerListener
23
     */
24
    protected $listener;
25
26
    /**
27
     * @var TransactionLogger|\PHPUnit_Framework_MockObject_MockObject
28
     */
29
    protected $transactionLogger;
30
31 View Code Duplication
    public function setUp()
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...
32
    {
33
        $this->transactionLogger = $this->getMockBuilder(TransactionLogger::class)
34
            ->disableOriginalConstructor()
35
            ->getMock()
36
        ;
37
38
        $this->listener = new ControllerListener($this->transactionLogger, new AnnotationReader());
0 ignored issues
show
Documentation introduced by
$this->transactionLogger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Loevgaard\Dandoma...Http\TransactionLogger>.

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...
39
40
        $this->request = new Request();
41
42
        // trigger the autoloading of the @LogHttpTransaction annotation
43
        class_exists('Loevgaard\DandomainAltapayBundle\Annotation\LogHttpTransaction');
44
    }
45
46
    public function tearDown()
47
    {
48
        $this->request = null;
49
        $this->listener = null;
50
        $this->transactionLogger = null;
51
    }
52
53
    public function testNotMasterRequest()
54
    {
55
        $controller = new ControllerWithAnnotation();
56
        $event = $this->getFilterControllerEvent([$controller, 'foobarAction'], $this->request, HttpKernelInterface::SUB_REQUEST);
57
        $res = $this->listener->onKernelController($event);
58
        $this->assertTrue(false === $res);
59
    }
60
61
    public function testControllerNotArray()
62
    {
63
        $event = $this->getFilterControllerEvent(function () {}, $this->request);
64
        $res = $this->listener->onKernelController($event);
65
        $this->assertTrue(false === $res);
66
    }
67
68
    public function testAnnotation()
69
    {
70
        $calls = 0;
71
72
        $this->transactionLogger
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Loevgaard\DandomainAltap...\Http\TransactionLogger.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
73
            ->expects($this->any())
74
            ->method('setRequest')
75
            ->willReturnCallback(function () use (&$calls) {
76
                ++$calls;
77
            });
78
79
        $controller = new ControllerWithAnnotation();
80
        $event = $this->getFilterControllerEvent([$controller, 'foobarAction'], $this->request);
81
        $this->listener->onKernelController($event);
82
83
        $this->assertSame(1, $calls);
84
    }
85
86
    protected function getFilterControllerEvent($controller, Request $request, int $requestType = HttpKernelInterface::MASTER_REQUEST)
87
    {
88
        $mockKernel = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Kernel', ['', '']);
89
90
        return new FilterControllerEvent($mockKernel, $controller, $request, $requestType);
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...
91
    }
92
}
93