Completed
Push — master ( 003c1a...870291 )
by Tomáš
06:24
created

CompleteTest::testGetControllerWithTrait()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Symplify\ControllerAutowire\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use PHPUnit_Framework_Assert;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\HttpKernelInterface;
12
use Symplify\ControllerAutowire\Controller\ControllerTrait;
13
use Symplify\ControllerAutowire\HttpKernel\Controller\ControllerResolver;
14
use Symplify\ControllerAutowire\Tests\CompleteTestSource\Controller\ControllerWithParameter;
15
use Symplify\ControllerAutowire\Tests\CompleteTestSource\DoNotScan\SomeRegisteredController;
16
use Symplify\ControllerAutowire\Tests\CompleteTestSource\Scan\ContainerAwareController;
17
use Symplify\ControllerAutowire\Tests\CompleteTestSource\Scan\TraitAwareController;
18
use Symplify\ControllerAutowire\Tests\HttpKernel\Controller\ControllerFinderSource\SomeController;
19
use Symplify\ControllerAutowire\Tests\HttpKernel\Controller\ControllerFinderSource\SomeService;
20
21
final class CompleteTest extends TestCase
22
{
23
    /**
24
     * @var ControllerResolver
25
     */
26
    private $controllerResolver;
27
28
    protected function setUp()
29
    {
30
        $kernel = new AppKernel('test_env', true);
0 ignored issues
show
Unused Code introduced by
The call to AppKernel::__construct() has too many arguments starting with 'test_env'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
31
        $kernel->boot();
32
33
        $this->controllerResolver = $kernel->getContainer()
34
            ->get('symplify.controller_resolver');
35
    }
36
37
    public function testMissingControllerParameter()
38
    {
39
        $request = new Request();
40
        $this->assertFalse($this->controllerResolver->getController($request));
41
    }
42
43
    public function testGetAutowiredController()
44
    {
45
        $request = new Request();
46
        $request->attributes->set('_controller', SomeController::class . '::someAction');
47
48
        /** @var SomeController $controller */
49
        $controller = $this->controllerResolver->getController($request)[0];
50
51
        $this->assertInstanceOf(SomeController::class, $controller);
52
        $this->assertInstanceOf(SomeService::class, $controller->getSomeService());
53
    }
54
55
    public function testGetContainerAwareController()
56
    {
57
        $request = new Request();
58
        $request->attributes->set('_controller', ContainerAwareController::class . '::someAction');
59
60
        /** @var ContainerAwareController $controller */
61
        $controller = $this->controllerResolver->getController($request)[0];
62
63
        $this->assertInstanceOf(ContainerAwareController::class, $controller);
64
        $this->assertInstanceOf(ContainerInterface::class, $controller->getContainer());
65
    }
66
67 View Code Duplication
    public function testGetAutowiredControllerWithParameter()
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...
68
    {
69
        $request = new Request();
70
        $request->attributes->set('_controller', 'some.controller.with_parameter:someAction');
71
72
        /** @var ControllerWithParameter $controller */
73
        $controller = $this->controllerResolver->getController($request)[0];
74
75
        $this->assertInstanceOf(ControllerWithParameter::class, $controller);
76
        $this->assertSame(__DIR__, $controller->getKernelRootDir());
77
    }
78
79
    public function testGetControllerWithTrait()
80
    {
81
        $request = new Request();
82
        $request->attributes->set(
83
            '_controller',
84
            'symplify.controllerautowire.tests.completetestsource.scan.traitawarecontroller:someAction'
85
        );
86
87
        /** @var TraitAwareController|ControllerTrait $controller */
88
        $controller = $this->controllerResolver->getController($request)[0];
89
90
        $this->assertInstanceOf(TraitAwareController::class, $controller);
91
92
        $httpKernel = PHPUnit_Framework_Assert::getObjectAttribute($controller, 'httpKernel');
0 ignored issues
show
Bug introduced by
It seems like $controller can also be of type object<Symplify\Controll...roller\ControllerTrait>; however, PHPUnit_Framework_Assert::getObjectAttribute() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
93
        $this->assertInstanceOf(HttpKernelInterface::class, $httpKernel);
94
    }
95
96
    /**
97
     * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
98
     */
99
    public function testGetControllerServiceMissing()
100
    {
101
        $request = new Request();
102
        $request->attributes->set('_controller', 'some.missing.controller.service:someAction');
103
104
        $this->controllerResolver->getController($request);
105
    }
106
107 View Code Duplication
    public function testGetControllerServiceRegisteredInConfig()
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...
108
    {
109
        $request = new Request();
110
        $request->attributes->set('_controller', 'some.controller.service:someAction');
111
112
        $controller = $this->controllerResolver->getController($request)[0];
113
        $this->assertInstanceOf(SomeRegisteredController::class, $controller);
114
    }
115
}
116