Test Failed
Push — master ( c12d25...bf3bf2 )
by Jean-Bernard
01:55
created

configureRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
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; // 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\ArgumentResolver\DefaultValueResolver;
22
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver;
23
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver;
24
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionValueResolver;
25
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver;
26
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;
27
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
28
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
29
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface;
30
use Symfony\Component\HttpKernel\EventListener\ResponseListener;
31
use Symfony\Component\HttpKernel\EventListener\RouterListener;
32
use Symfony\Component\HttpKernel\HttpKernel;
33
use Symfony\Component\Routing\Matcher\UrlMatcher; // != Symfony\Bundle\FrameworkBundle\Routing\Router
34
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
35
use Symfony\Component\Routing\RequestContext;
36
use Symfony\Component\Routing\RouteCollectionBuilder;
37
use Symfony\Component\Templating\EngineInterface;
38
use Symfony\Component\Templating\TemplateNameParser;
39
use Symfony\Component\Templating\TemplateNameParserInterface;
40
use SymfonyUtil\Controller\EngineAsArgumentController;
41
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...
42
43
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...
44
{
45
    public function testCanBeCreated()
46
    {
47
        $this->assertInstanceOf(
48
            // ...::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...
49
            'Symfony\Component\HttpKernel\Kernel',
50
            new AppKernel('dev', true)
51
        );
52
    }
53
54
    public function testKernelInterface()
55
    {
56
        $this->assertInstanceOf(
57
            // ...::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...
58
            'Symfony\Component\HttpKernel\KernelInterface',
59
            new AppKernel('dev', true)
60
        );
61
    }
62
63
    public function testFrameworkReturnsResponse()
64
    {
65
        $this->assertInstanceOf(
66
            // Response::class, // 5.4 < php
67
            'Symfony\Component\HttpFoundation\Response',
68
            (new AppKernel('dev', true))->handle(Request::create('/', 'GET'))
69
        );
70
    }
71
72
    public function testControllerResponse()
73
    { // From: https://symfony.com/doc/current/create_framework/unit_testing.html
74
        // TODO: Try with a real matcher
75
        // TODO: Use real controller to be tested!
76
        $matcher = $this->createMock(UrlMatcherInterface::class);
77
        // use getMock() on PHPUnit 5.3 or below
78
        // $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...
79
80
        $matcher
81
            ->expects($this->once())
82
            ->method('match')
83
            ->will($this->returnValue([
84
                '_route' => 'foo',
85
                'name' => 'Fabien',
86
                '_controller' => function ($name) {
87
                    return new Response('Hello '.$name);
88
                },
89
            ]))
90
        ;
91
        $matcher
92
            ->expects($this->once())
93
            ->method('getContext')
94
            ->will($this->returnValue($this->createMock(RequestContext::class)))
95
        ;
96
97
        $requestStack = new RequestStack();
98
        $dispatcher = new EventDispatcher();
99
        $dispatcher->addSubscriber(new RouterListener(
100
            $matcher,
101
            $requestStack
102
        )); // Returns nothing.
103
        $dispatcher->addSubscriber(new ResponseListener('UTF-8'));
104
        $response = (new HttpKernel(
105
            $dispatcher,
106
            new ContainerControllerResolver($this->container()),
107
            // 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...
108
            $requestStack,
109
            new ArgumentResolver(
110
                new ArgumentMetadataFactory(),
111
                [
112
                    new RequestAttributeValueResolver(),
113
                    new RequestValueResolver(),
114
                    new SessionValueResolver(),
115
                    new ServiceValueResolver($this->container()),
116
                    new DefaultValueResolver(),
117
                    new VariadicValueResolver(),
118
                ]
119
            )
120
        // ))->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...
121
        ))->handle(new Request());
122
123
        $this->assertSame(200, $response->getStatusCode());
124
        $this->assertContains('Hello Fabien', $response->getContent());
125
    }
126
127
    public function testContainerCanBeCreated()
128
    {
129
        $this->assertInstanceOf(
130
            // ...::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...
131
            'Symfony\Component\DependencyInjection\ContainerBuilder',
132
            $this->container()
133
        );
134
    }
135
136
    public function testContainerInterface()
137
    {
138
        $this->assertInstanceOf(
139
            // ...::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...
140
            'psr\Container\ContainerInterface',
141
            $this->container()
142
        );
143
    }
144
145
    public function testComponentReturnsResponse() // Not yet a test!
146
    {
147
        // TODO: Use real controller to be tested!
148
        $requestStack = new RequestStack();
149
        $dispatcher = new EventDispatcher();
150
        $dispatcher->addSubscriber(new RouterListener(
151
            new UrlMatcher(
152
                $this->loadJustHelloRoutes(),
153
                new RequestContext()
154
            ),
155
            $requestStack
156
        ));
157
        $dispatcher->addSubscriber(new ResponseListener('UTF-8'));
158
159
        $this->assertInstanceOf(
160
            // Response::class, // 5.4 < php
161
            'Symfony\Component\HttpFoundation\Response',
162
            (new HttpKernel(
163
                $dispatcher,
164
                // new ContainerControllerResolver($this->container()),
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
165
                new ControllerResolver(),
166
                $requestStack,
167
                new ArgumentResolver(
168
                new ArgumentMetadataFactory(),
169
                [
170
                    new RequestAttributeValueResolver(),
171
                    new RequestValueResolver(),
172
                    new SessionValueResolver(),
173
                    new ServiceValueResolver($this->container()),
174
                    new DefaultValueResolver(),
175
                    new VariadicValueResolver(),
176
                ]
177
                )
178
            ))->handle(Request::create('/', 'GET'))
179
        );
180
    }
181
182
    private function configureRoutes(RouteCollectionBuilder $routes)
183
    { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait
184
        $routes->add('/', EngineAsArgumentController::class, 'index'); // .'::__invoke'
185
        //^ It should be tested if the actually used controller resolver can resolve this!
186
        //^ Returns Symfony/Component/Routing/Route .
187
    }
188
189
    private function loadRoutes(LoaderInterface $loader = null)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
190
    { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait
191
        $routes = new RouteCollectionBuilder($loader);
192
        $this->configureRoutes($routes);
193
194
        return $routes->build();
195
    }
196
197
    private function configureJustHelloRoutes(RouteCollectionBuilder $routes)
198
    { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait
199
        $routes->add(
200
            '/',
201
            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...
202
                return new Response('Hello');
203
            },
204
            'index'
205
        ); // .'::__invoke'
206
        //^ It should be tested if the actually used controller resolver can resolve this!
207
        //^ Returns Symfony/Component/Routing/Route .
208
    }
209
210
    private function loadJustHelloRoutes(LoaderInterface $loader = null)
211
    { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait
212
        $routes = new RouteCollectionBuilder($loader);
213
        $this->configureJustHelloRoutes($routes);
214
215
        return $routes->build();
216
    }
217
218
    private function container()
219
    {
220
        $c = new ContainerBuilder();
221
        // https://symfony.com/doc/current/service_container.html
222
223
        $c->autowire(TemplateNameParser::class)
224
            ->setAutoconfigured(true)
225
            ->setPublic(false);
226
        $c->setAlias(TemplateNameParserInterface::class, TemplateNameParser::class);
227
228
        $c->autowire(Twig_Loader_Array::class, Twig_Loader_Array::class)
229
            ->setArgument('$templates', ['index.html.twig' => 'Hello Component!'])
230
            ->setAutoconfigured(true)
231
            ->setPublic(false);
232
        $c->setAlias(Twig_LoaderInterface::class, Twig_Loader_Array::class);
233
234
        $c->autowire(Twig_Environment::class, Twig_Environment::class)
235
            ->setAutoconfigured(true)
236
            ->setPublic(false);
237
        $c->setAlias(Twig\Environment::class, Twig_Environment::class);
238
239
        $c->autowire(TwigEngine::class)
240
            ->setAutoconfigured(true)
241
            ->setPublic(false);
242
        $c->setAlias(EngineInterface::class, TwigEngine::class);
243
244
        // Unit Testing
245
        // $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...
246
        //     ->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...
247
248
        //Controllers
249
        $c->autowire(EngineAsArgumentController::class)
250
            ->setAutoconfigured(true)
251
            ->addTag('controller.service_arguments')
252
            ->setPublic(false);
253
254
        return $c;
255
    }
256
}
257
258
// http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html
259
// http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html
260