Test Failed
Push — master ( e6e23e...c3543a )
by Jean-Bernard
02:06
created

EngineAsArgumentInKernelControllerTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 15

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 15
dl 0
loc 170
rs 9.1666
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanBeCreated() 0 8 1
A testKernelInterface() 0 8 1
A testFrameworkReturnsResponse() 0 8 1
B testControllerResponse() 0 42 1
A testContainerCanBeCreated() 0 8 1
A testContainerInterface() 0 8 1
B ComponentReturnsResponse() 0 25 1
A configureRoutes() 0 6 1
A loadRoutes() 0 7 1
B container() 0 38 1
1
<?php
2
3
/*
4
 * This file is part of the Symfony-Util package.
5
 *
6
 * (c) Jean-Bernard Addor
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
use PHPUnit\Framework\TestCase;
13
use Symfony\Bridge\Twig\TwigEngine;
14
// use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver; // Do not know how to configure this.
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\EventDispatcher\EventDispatcher;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
21
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;
22
use Symfony\Component\HttpKernel\EventListener\ResponseListener;
23
use Symfony\Component\HttpKernel\EventListener\RouterListener;
24
use Symfony\Component\HttpKernel\HttpKernel;
25
use Symfony\Component\Routing\Matcher\UrlMatcher;
26
use Symfony\Component\Routing\RequestContext;
27
use Symfony\Component\Routing\RouteCollectionBuilder;
28
use Symfony\Component\Templating\EngineInterface;
29
use Symfony\Component\Templating\TemplateNameParser;
30
use Symfony\Component\Templating\TemplateNameParserInterface;
31
use SymfonyUtil\Controller\EngineAsArgumentController;
32
use Tests\Component\AppKernel;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AppKernel.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
33
34
/**
35
 * @covers \SymfonyUtil\Controller\EngineAsArgumentController
36
 */
37
final class EngineAsArgumentInKernelControllerTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
38
{
39
    public function testCanBeCreated()
40
    {
41
        $this->assertInstanceOf(
42
            // ...::class, // 5.4 < php
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
43
            'Symfony\Component\HttpKernel\Kernel',
44
            new AppKernel('dev', true)
45
        );
46
    }
47
48
    public function testKernelInterface()
49
    {
50
        $this->assertInstanceOf(
51
            // ...::class, // 5.4 < php
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
            'Symfony\Component\HttpKernel\KernelInterface',
53
            new AppKernel('dev', true)
54
        );
55
    }
56
57
    public function testFrameworkReturnsResponse()
58
    {
59
        $this->assertInstanceOf(
60
            // Response::class, // 5.4 < php
61
            'Symfony\Component\HttpFoundation\Response',
62
            (new AppKernel('dev', true))->handle(Request::create('/', 'GET'))
63
        );
64
    }
65
66
    public function testControllerResponse()
67
    {
68
        $matcher = $this->createMock(Routing\Matcher\UrlMatcherInterface::class);
69
        // use getMock() on PHPUnit 5.3 or below
70
        // $matcher = $this->getMock(Routing\Matcher\UrlMatcherInterface::class);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
72
        $matcher
73
            ->expects($this->once())
74
            ->method('match')
75
            ->will($this->returnValue(array(
76
                '_route' => 'foo',
77
                'name' => 'Fabien',
78
                '_controller' => function ($name) {
79
                    return new Response('Hello '.$name);
80
                }
81
            )))
82
        ;
83
        $matcher
84
            ->expects($this->once())
85
            ->method('getContext')
86
            ->will($this->returnValue($this->createMock(Routing\RequestContext::class)))
87
        ;
88
89
        $requestStack = new RequestStack();
90
        $dispatcher = new EventDispatcher();
91
        $dispatcher->addSubscriber(new RouterListener(
92
            $matcher,
93
            $requestStack
94
        ));
95
        $dispatcher->addSubscriber(new ResponseListener('UTF-8'));
96
        $response = (new HttpKernel(
97
            $dispatcher,
98
            // new ContainerControllerResolver($c),
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
            new ControllerResolver(),
100
            $requestStack,
101
            new ArgumentResolver() // OK
102
        // ))->handle(Request::create('/', 'GET'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
        ))->handle(new Request());
104
105
        $this->assertEquals(200, $response->getStatusCode());
106
        $this->assertContains('Hello Fabien', $response->getContent());
107
    }
108
109
    public function testContainerCanBeCreated()
110
    {
111
        $this->assertInstanceOf(
112
            // ...::class, // 5.4 < php
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
113
            'Symfony\Component\DependencyInjection\ContainerBuilder',
114
            $this->container()
115
        );
116
    }
117
118
    public function testContainerInterface()
119
    {
120
        $this->assertInstanceOf(
121
            // ...::class, // 5.4 < php
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
122
            'psr\Container\ContainerInterface',
123
            $this->container()
124
        );
125
    }
126
127
    public function ComponentReturnsResponse() // Not yet a test!
128
    {
129
        $requestStack = new RequestStack();
130
        $dispatcher = new EventDispatcher();
131
        $dispatcher->addSubscriber(new RouterListener(
132
            new UrlMatcher(
133
                $this->loadRoutes(),
134
                new RequestContext()
135
            ),
136
            $requestStack
137
        ));
138
        $dispatcher->addSubscriber(new ResponseListener('UTF-8'));
139
140
141
        $this->assertInstanceOf(
142
            // Response::class, // 5.4 < php
143
            'Symfony\Component\HttpFoundation\Response',
144
            (new HttpKernel(
145
                $dispatcher,
146
                new ContainerControllerResolver($c),
0 ignored issues
show
Bug introduced by
The variable $c does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
147
                $requestStack,
148
                new ArgumentResolver()
149
            ))->handle(Request::create('/', 'GET'))
150
        );
151
    }
152
153
    private function configureRoutes(RouteCollectionBuilder $routes)
154
    { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait
155
        $routes->add('/', EngineAsArgumentController::class, 'index'); // .'::__invoke'
156
        //^ It should be tested if the actually used controller resolver can resolve this!
157
        //^ Returns Symfony/Component/Routing/Route .
158
    }
159
160
    private function loadRoutes(LoaderInterface $loader = null)
161
    { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait
162
        $routes = new RouteCollectionBuilder($loader);
163
        $this->configureRoutes($routes);
164
165
        return $routes->build();
166
    }
167
168
    private function container()
169
    {
170
        $c = new ContainerBuilder();
171
        // https://symfony.com/doc/current/service_container.html
172
173
        $c->autowire(TemplateNameParser::class)
174
            ->setAutoconfigured(true)
175
            ->setPublic(false);
176
        $c->setAlias(TemplateNameParserInterface::class, TemplateNameParser::class);
177
178
        $c->autowire(Twig_Loader_Array::class, Twig_Loader_Array::class)
179
            ->setArgument('$templates', ['index.html.twig' => 'Hello Component!'])
180
            ->setAutoconfigured(true)
181
            ->setPublic(false);
182
        $c->setAlias(Twig_LoaderInterface::class, Twig_Loader_Array::class);
183
184
        $c->autowire(Twig_Environment::class, Twig_Environment::class)
185
            ->setAutoconfigured(true)
186
            ->setPublic(false);
187
        $c->setAlias(Twig\Environment::class, Twig_Environment::class);
188
189
        $c->autowire(TwigEngine::class)
190
            ->setAutoconfigured(true)
191
            ->setPublic(false);
192
        $c->setAlias(EngineInterface::class, TwigEngine::class);
193
194
        // Unit Testing
195
        // $c->autowire('test.client', Client::class)
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
196
        //     ->setPublic(true); // Public needed!
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
197
198
        //Controllers
199
        $c->autowire(EngineAsArgumentController::class)
200
            ->setAutoconfigured(true)
201
            ->addTag('controller.service_arguments')
202
            ->setPublic(false);
203
204
        return $c;
205
    }
206
}
207
208
// http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html
209
// http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html
210