Completed
Push — master ( acced1...f85cfe )
by Tomáš
07:16 queued 05:05
created

testGetAutowiredControllerWithParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Symplify\ControllerAutowire\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symplify\ControllerAutowire\HttpKernel\Controller\ControllerResolver;
9
use Symplify\ControllerAutowire\Tests\AliasingBundle\Controller\ControllerWithParameter;
10
use Symplify\ControllerAutowire\Tests\CompleteTestSource\DoNotScan\SomeRegisteredController;
11
use Symplify\ControllerAutowire\Tests\CompleteTestSource\Scan\ContainerAwareController;
12
use Symplify\ControllerAutowire\Tests\HttpKernel\Controller\ControllerFinderSource\SomeController;
13
use Symplify\ControllerAutowire\Tests\HttpKernel\Controller\ControllerFinderSource\SomeService;
14
15
final class CompleteTest extends TestCase
16
{
17
    /**
18
     * @var ControllerResolver
19
     */
20
    private $controllerResolver;
21
22
    protected function setUp()
23
    {
24
        $kernel = new AppKernel('test_env', true);
25
        $kernel->boot();
26
27
        $this->controllerResolver = $kernel->getContainer()
28
            ->get('default.controller_resolver');
29
    }
30
31
    public function testMissingControllerParameter()
32
    {
33
        $request = new Request();
34
        $this->assertFalse($this->controllerResolver->getController($request));
35
    }
36
37
    public function testGetAutowiredController()
38
    {
39
        $request = new Request();
40
        $request->attributes->set('_controller', SomeController::class.'::someAction');
41
42
        /** @var SomeController $controller */
43
        $controller = $this->controllerResolver->getController($request)[0];
44
45
        $this->assertInstanceOf(SomeController::class, $controller);
46
        $this->assertInstanceOf(SomeService::class, $controller->getSomeService());
47
    }
48
49
    public function testGetContainerAwareController()
50
    {
51
        $request = new Request();
52
        $request->attributes->set('_controller', ContainerAwareController::class.'::someAction');
53
54
        /** @var ContainerAwareController $controller */
55
        $controller = $this->controllerResolver->getController($request)[0];
56
57
        $this->assertInstanceOf(ContainerAwareController::class, $controller);
58
        $this->assertInstanceOf(ContainerInterface::class, $controller->getContainer());
59
    }
60
61
    public function testGetAutowiredControllerWithParameter()
62
    {
63
        $request = new Request();
64
        $request->attributes->set('_controller', 'some.controller.with_parameter:someAction');
65
66
        /** @var ControllerWithParameter $controller */
67
        $controller = $this->controllerResolver->getController($request)[0];
68
69
        $this->assertInstanceOf(ControllerWithParameter::class, $controller);
70
        $this->assertSame(__DIR__, $controller->getKernelRootDir());
71
    }
72
73
    /**
74
     * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
75
     */
76
    public function testGetControllerServiceMissing()
77
    {
78
        $request = new Request();
79
        $request->attributes->set('_controller', 'some.missing.controller.service:someAction');
80
81
        $this->controllerResolver->getController($request);
82
    }
83
84
    public function testGetControllerServiceRegisteredInConfig()
85
    {
86
        $request = new Request();
87
        $request->attributes->set('_controller', 'some.controller.service:someAction');
88
89
        $controller = $this->controllerResolver->getController($request)[0];
90
        $this->assertInstanceOf(SomeRegisteredController::class, $controller);
91
    }
92
}
93