Completed
Push — master ( 16f3da...34d795 )
by Jean-Bernard
07:52
created

testKernelInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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