Test Failed
Push — master ( afce87...1b1ba3 )
by Jean-Bernard
02:31
created

testContainerInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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