Test Failed
Push — master ( 4f320d...aac376 )
by Jean-Bernard
01:59
created

testComponentReturnsResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 61
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 9.5147
c 0
b 0
f 0
cc 2
eloc 44
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\EventDispatcher\EventDispatcher;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
20
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;
21
use Symfony\Component\HttpKernel\EventListener\ResponseListener;
22
use Symfony\Component\HttpKernel\EventListener\RouterListener;
23
use Symfony\Component\HttpKernel\HttpKernel;
24
use Symfony\Component\Routing\Matcher\UrlMatcher;
25
use Symfony\Component\Routing\RequestContext;
26
use Symfony\Component\Routing\RouteCollectionBuilder;
27
use SymfonyUtil\Controller\EngineAsArgumentController;
28
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...
29
30
/**
31
 * @covers \SymfonyUtil\Controller\EngineAsArgumentController
32
 */
33
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...
34
{
35
    public function testCanBeCreated()
36
    {
37
        $this->assertInstanceOf(
38
            // ...::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...
39
            'Symfony\Component\HttpKernel\Kernel',
40
            new AppKernel('dev', true)
41
        );
42
    }
43
44
    public function testKernelInterface()
45
    {
46
        $this->assertInstanceOf(
47
            // ...::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...
48
            'Symfony\Component\HttpKernel\KernelInterface',
49
            new AppKernel('dev', true)
50
        );
51
    }
52
53
    public function testFrameworkReturnsResponse()
54
    {
55
        $this->assertInstanceOf(
56
            // Response::class, // 5.4 < php
57
            'Symfony\Component\HttpFoundation\Response',
58
            (new AppKernel('dev', true))->handle(Request::create('/', 'GET'))
59
        );
60
    }
61
62
    public function testComponentReturnsResponse()
63
    {
64
        $requestStack = new RequestStack();
65
        $routes = new RouteCollectionBuilder(); // Because I know how to use it.
66
        $routes->add('/', EngineAsArgumentController::class, 'index'); // returns Symfony/Component/Routing/Route
67
        $dispatcher = new EventDispatcher();
68
        $dispatcher->addSubscriber(new RouterListener(
69
            new UrlMatcher(
70
                $routes->build(), 
71
                new RequestContext()
72
            ),
73
            $requestStack
74
        ));
75
        $dispatcher->addSubscriber(new ResponseListener('UTF-8'));
76
77
        $c = new ContainerBuilder();
78
        // https://symfony.com/doc/current/service_container.html
79
80
        $c->autowire(TemplateNameParser::class)
81
            ->setAutoconfigured(true)
82
            ->setPublic(false);
83
        $c->setAlias(TemplateNameParserInterface::class, TemplateNameParser::class);
84
85
        $c->autowire(Twig_Loader_Array::class, Twig_Loader_Array::class)
86
            ->setArgument('$templates', ['index.html.twig' => 'Hello Component!'])
87
            ->setAutoconfigured(true)
88
            ->setPublic(false);
89
        $c->setAlias(Twig_LoaderInterface::class, Twig_Loader_Array::class);
90
91
        $c->autowire(Twig_Environment::class, Twig_Environment::class)
92
            ->setAutoconfigured(true)
93
            ->setPublic(false);
94
        $c->setAlias(Twig\Environment::class, Twig_Environment::class);
95
96
        $c->autowire(TwigEngine::class)
97
            ->setAutoconfigured(true)
98
            ->setPublic(false);
99
        $c->setAlias(EngineInterface::class, TwigEngine::class);
100
101
        if (in_array($this->getEnvironment(), ['test'], true)) {
102
            $c->autowire('test.client', Client::class)
103
                ->setPublic(true); // Public needed!
104
        }
105
106
        //Controllers
107
        $c->autowire(EngineAsArgumentController::class)
108
            ->setAutoconfigured(true)
109
            ->addTag('controller.service_arguments')
110
            ->setPublic(false);
111
112
        $this->assertInstanceOf(
113
            // Response::class, // 5.4 < php
114
            'Symfony\Component\HttpFoundation\Response',
115
            (new HttpKernel(
116
                $dispatcher,
117
                new ContainerControllerResolver($c),
118
                $requestStack,
119
                new ArgumentResolver()
120
            ))->handle(Request::create('/', 'GET'))
121
        );
122
    }
123
}
124
125
// http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html
126
// http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html
127