Test Failed
Push — master ( 6fea7d...d47aa1 )
by Jean-Bernard
02:28
created

testComponentReturnsResponse()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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