Test Failed
Push — master ( cca1e4...191bab )
by Jean-Bernard
01:49
created

EngineAsArgumentInKernelControllerTest::testComponentReturnsResponse()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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

Syntax error, unexpected T_OBJECT_OPERATOR, expecting T_VARIABLE or '{' or '$'
Loading history...
257
258
        return $c;
259
    }
260
}
261
262
// http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html
263
// http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html
264