Completed
Push — master ( 6d3799...60d264 )
by Jean-Bernard
04:46
created

configureRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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;
15
//^ Do not know how to configure this.
16
// * In a constructor of a kernel derivative
17
// * Using the DI container and looking example config
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\EventDispatcher\EventDispatcher;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\RequestStack;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
24
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver;
25
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver;
26
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver;
27
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver;
28
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionValueResolver;
29
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver;
30
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;
31
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
32
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
33
use Symfony\Component\HttpKernel\DependencyInjection\ControllerArgumentValueResolverPass;
34
use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;
35
use Symfony\Component\HttpKernel\EventListener\ResponseListener;
36
use Symfony\Component\HttpKernel\EventListener\RouterListener;
37
use Symfony\Component\HttpKernel\HttpKernel;
38
use Symfony\Component\Routing\Matcher\UrlMatcher; // != Symfony\Bundle\FrameworkBundle\Routing\Router
39
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
40
use Symfony\Component\Routing\RequestContext;
41
use Symfony\Component\Routing\RouteCollectionBuilder;
42
use Symfony\Component\Templating\EngineInterface;
43
use Symfony\Component\Templating\TemplateNameParser;
44
use Symfony\Component\Templating\TemplateNameParserInterface;
45
use SymfonyUtil\Controller\EngineInConstructorController;
46
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...
47
48
// use Tests\Component\EngineAsArgumentFrameworkController;
49
50
final class EngineInConstructorInKernelControllerTest 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...
51
{
52
    public function testCanBeCreated()
53
    {
54
        $this->assertInstanceOf(
55
            // ...::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...
56
            'Symfony\Component\HttpKernel\Kernel',
57
            new AppKernel('dev', true)
58
        );
59
    }
60
61
    public function testKernelInterface()
62
    {
63
        $this->assertInstanceOf(
64
            // ...::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...
65
            'Symfony\Component\HttpKernel\KernelInterface',
66
            new AppKernel('dev', true)
67
        );
68
    }
69
70
    public function testFrameworkReturnsResponse()
71
    {
72
        $this->assertInstanceOf(
73
            // Response::class, // 5.4 < php
74
            'Symfony\Component\HttpFoundation\Response',
75
            (new AppKernel('dev', true))->handle(Request::create('/constructor', 'GET'))
76
        );
77
    }
78
79
    public function testControllerResponse()
80
    { // From: https://symfony.com/doc/current/create_framework/unit_testing.html
81
        // TODO: Try with a real matcher see next test...
82
        // TODO: Use real controller to be tested! OK
83
        $matcher = $this->createMock(UrlMatcherInterface::class); // What about another test with RequestMatcherInterface?
84
        // use getMock() on PHPUnit 5.3 or below
85
        // $matcher = $this->getMock(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...
86
87
        $matcher
88
            ->expects($this->once())
89
            ->method('match')
90
            ->will($this->returnValue([
91
                '_route' => 'foo',
92
                'name' => 'Fabien',
93
                // '_controller' => function ($name) {
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...
94
                //     return new Response('Hello '.$name);
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...
95
                // },
96
                '_controller' => EngineInConstructorController::class,
97
            ]))
98
        ;
99
        $matcher
100
            ->expects($this->once())
101
            ->method('getContext')
102
            ->will($this->returnValue($this->createMock(RequestContext::class)))
103
        ;
104
105
        $c = $this->container();
106
        $c->compile();
107
        $requestStack = new RequestStack();
108
        $dispatcher = new EventDispatcher();
109
        $dispatcher->addSubscriber(new RouterListener(
110
            $matcher,
111
            $requestStack
112
        )); // Returns nothing.
113
        $dispatcher->addSubscriber(new ResponseListener('UTF-8'));
114
        $response = (new HttpKernel(
115
            $dispatcher,
116
            new ContainerControllerResolver($c),
117
            // new ControllerResolver(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
118
            $requestStack,
119
            new ArgumentResolver(
120
                // new ArgumentMetadataFactory(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
121
                // [
122
                //     new RequestAttributeValueResolver(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
123
                //     new RequestValueResolver(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
124
                //     new SessionValueResolver(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
125
                //     new ServiceValueResolver($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...
126
                //     new DefaultValueResolver(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
127
                //     new VariadicValueResolver(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
128
                // ]
129
            )
130
        // ))->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...
131
        ))->handle(new Request());
132
133
        $this->assertSame(200, $response->getStatusCode());
134
        $this->assertContains('Hello Component!', $response->getContent());
135
    }
136
137
    public function testContainerCanBeCreated()
138
    {
139
        $this->assertInstanceOf(
140
            // ...::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...
141
            'Symfony\Component\DependencyInjection\ContainerBuilder',
142
            $this->container()
143
        );
144
    }
145
146
    public function testContainerInterface()
147
    {
148
        $this->assertInstanceOf(
149
            // ...::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...
150
            'psr\Container\ContainerInterface',
151
            $this->container()
152
        );
153
    }
154
155
    public function testComponentReturnsResponse()
156
    {
157
        $c = $this->container();
158
        $c->compile();
159
        $requestStack = new RequestStack();
160
        $dispatcher = new EventDispatcher();
161
        $dispatcher->addSubscriber(new RouterListener(
162
            new UrlMatcher(
163
                $this->loadRoutes(),
164
                new RequestContext()
165
            ),
166
            $requestStack
167
        ));
168
        $dispatcher->addSubscriber(new ResponseListener('UTF-8'));
169
170
        $this->assertInstanceOf(
171
            // Response::class, // 5.4 < php
172
            'Symfony\Component\HttpFoundation\Response',
173
            (new HttpKernel(
174
                $dispatcher,
175
                new ContainerControllerResolver($c),
176
                $requestStack,
177
                new ArgumentResolver()
178
            ))->handle(Request::create('/', 'GET'))
179
        );
180
    }
181
182
    private function configureRoutes(RouteCollectionBuilder $routes)
183
    { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait
184
        $routes->add('/', EngineInConstructorController::class, 'index');
185
    }
186
187
    private function loadRoutes(LoaderInterface $loader = null)
188
    { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait
189
        $routes = new RouteCollectionBuilder($loader);
190
        $this->configureRoutes($routes);
191
192
        return $routes->build();
193
    }
194
195
    private function configureJustHelloRoutes(RouteCollectionBuilder $routes)
196
    { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait
197
        $routes->add(
198
            '/',
199
            function () {
0 ignored issues
show
Documentation introduced by
function () { return...on\Response('Hello'); } is of type object<Closure>, but the function expects a string.

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...
200
                return new Response('Hello');
201
            },
202
            'index'
203
        );
204
        //^ It should be tested if the actually used controller resolver can resolve this!
205
        //^ Returns Symfony/Component/Routing/Route .
206
    }
207
208
    private function loadJustHelloRoutes(LoaderInterface $loader = null)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
209
    { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait
210
        $routes = new RouteCollectionBuilder($loader);
211
        $this->configureJustHelloRoutes($routes);
212
213
        return $routes->build();
214
    }
215
216
    private function container()
217
    {
218
        $c = new ContainerBuilder();
219
        // https://symfony.com/doc/current/service_container.html
220
221
        $c->autowire(TemplateNameParser::class)
222
            ->setAutoconfigured(true)
223
            ->setPublic(false);
224
        $c->setAlias(TemplateNameParserInterface::class, TemplateNameParser::class);
225
226
        $c->autowire(Twig_Loader_Array::class, Twig_Loader_Array::class)
227
            ->setArgument('$templates', ['index.html.twig' => 'Hello Component!'])
228
            ->setAutoconfigured(true)
229
            ->setPublic(false);
230
        $c->setAlias(Twig_LoaderInterface::class, Twig_Loader_Array::class);
231
232
        $c->autowire(Twig_Environment::class, Twig_Environment::class)
233
            ->setAutoconfigured(true)
234
            ->setPublic(false);
235
        $c->setAlias(Twig\Environment::class, Twig_Environment::class);
236
237
        $c->autowire(TwigEngine::class)
238
            ->setAutoconfigured(true)
239
            ->setPublic(false);
240
        $c->setAlias(EngineInterface::class, TwigEngine::class);
241
242
        // Unit Testing
243
        // $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...
244
        //     ->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...
245
246
        //Controllers
247
        $c->autowire(EngineInConstructorController::class)
248
            ->setAutoconfigured(true)
249
            ->addTag('controller.service_arguments')
250
            ->setPublic(true);
251
252
        return $c;
253
    }
254
}
255
256
// http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html
257
// http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html
258