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

ControllerListenerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\EventListener;
4
5
use Loevgaard\DandomainAltapayBundle\EventListener\ControllerListener;
6
use Loevgaard\DandomainAltapayBundle\Http\TransactionLogger;
7
use Loevgaard\DandomainAltapayBundle\Tests\EventListener\Fixture\ControllerWithAnnotation;
8
use PHPUnit\Framework\TestCase;
9
use Doctrine\Common\Annotations\AnnotationReader;
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());
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($res === false);
59
    }
60
61
    public function testControllerNotArray()
62
    {
63
        $event = $this->getFilterControllerEvent(function() {}, $this->request);
64
        $res = $this->listener->onKernelController($event);
65
        $this->assertTrue($res === false);
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
80
        $controller = new ControllerWithAnnotation();
81
        $event = $this->getFilterControllerEvent([$controller, 'foobarAction'], $this->request);
82
        $this->listener->onKernelController($event);
83
84
        $this->assertSame(1, $calls);
85
    }
86
87
    protected function getFilterControllerEvent($controller, Request $request, int $requestType = HttpKernelInterface::MASTER_REQUEST)
88
    {
89
        $mockKernel = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Kernel', array('', ''));
90
91
        return new FilterControllerEvent($mockKernel, $controller, $request, $requestType);
92
    }
93
}
94