Test Failed
Push — master ( 6eef39...b1d0ab )
by Jean-Bernard
02:04
created

ollerTest::configureJustHelloRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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