Test Failed
Push — master ( 60d264...01a9d6 )
by Jean-Bernard
05:44
created

testControllerResponse()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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