Completed
Pull Request — master (#1)
by Christophe
04:03
created

FrontRenderTest::testExceptionOnEmptyPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Chris\Bundle\FrontRenderBundle\Tests\Render;
4
5
use Chris\Bundle\FrontRenderBundle\Listener\TwigListener;
6
use Chris\Bundle\FrontRenderBundle\Render\FrontRender;
7
use InvalidArgumentException;
8
use Symfony\Bundle\TwigBundle\TwigEngine;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\EventDispatcher\EventDispatcher;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Templating\TemplateNameParser;
13
use Twig_Error_Syntax;
14
15
class FrontRenderTest extends \PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * @var string TEMPLATE_PATH
19
     */
20
    const TEMPLATE_PATH = 'Tests/Template';
21
22
    /**
23
     * @var string TEMPLATE
24
     */
25
    const TEMPLATE = 'index.html.twig';
26
27
    /**
28
     * @var string FAILED_TEMPLATE
29
     */
30
    const FAILED_TEMPLATE = 'failed.html.twig';
31
32
    /**
33
     * @var string FAILED_PATH
34
     */
35
    const FAILED_PATH = 'failedPath.html.twig';
36
37
    /**
38
     * @var EventDispatcher
39
     */
40
    protected $eventDispatcher;
41
42
    /**
43
     * @var \Twig_Loader_Filesystem
44
     */
45
    protected $twigLoader;
46
47
    /**
48
     * @var \Twig_Environment
49
     */
50
    protected $twigEnvironment;
51
52
    /**
53
     * @var TwigListener
54
     */
55
    protected $twigListener;
56
57
    /**
58
     * @var TemplateNameParser
59
     */
60
    protected $templateNameParser;
61
62
    /**
63
     * @var FileLocator
64
     */
65
    protected $fileLocator;
66
67
    /**
68
     * @var TwigEngine
69
     */
70
    protected $engine;
71
72
    /**
73
     * @var FrontRender
74
     */
75
    protected $frontRender;
76
77
    /**
78
     * Set up the front render test
79
     */
80
    public function setUp()
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
81
    {
82
        parent::setUp();
83
84
        $this->eventDispatcher = \Phake::mock(EventDispatcher::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Phake::mock(\Symfony\Co...EventDispatcher::class) of type object<Phake_IMock> is incompatible with the declared type object<Symfony\Component...atcher\EventDispatcher> of property $eventDispatcher.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
85
        $this->twigLoader      = \Phake::partialMock(\Twig_Loader_Filesystem::class, [$_SERVER['KERNEL_DIR'] . self::TEMPLATE_PATH]);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Phake::partialMock(\Twi.... self::TEMPLATE_PATH)) of type object<Phake_IMock> is incompatible with the declared type object<Twig_Loader_Filesystem> of property $twigLoader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
86
        $this->twigEnvironment = \Phake::partialMock(\Twig_Environment::class, $this->twigLoader);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Phake::partialMock(\Twi...ass, $this->twigLoader) of type object<Phake_IMock> is incompatible with the declared type object<Twig_Environment> of property $twigEnvironment.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
        $this->twigListener    = \Phake::partialMock(TwigListener::class, $this->twigEnvironment);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Phake::partialMock(\Chr...$this->twigEnvironment) of type object<Phake_IMock> is incompatible with the declared type object<Chris\Bundle\Fron...\Listener\TwigListener> of property $twigListener.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
88
89
        $this->templateNameParser = \Phake::mock(TemplateNameParser::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Phake::mock(\Symfony\Co...plateNameParser::class) of type object<Phake_IMock> is incompatible with the declared type object<Symfony\Component...ing\TemplateNameParser> of property $templateNameParser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
90
        $this->fileLocator        = \Phake::mock(FileLocator::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Phake::mock(\Symfony\Co...fig\FileLocator::class) of type object<Phake_IMock> is incompatible with the declared type object<Symfony\Component\Config\FileLocator> of property $fileLocator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
91
        $this->engine             = \Phake::partialMock(TwigEngine::class, $this->twigEnvironment, $this->templateNameParser, $this->fileLocator);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Phake::partialMock(\Sym...er, $this->fileLocator) of type object<Phake_IMock> is incompatible with the declared type object<Symfony\Bundle\TwigBundle\TwigEngine> of property $engine.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
92
93
        $lexer = $this->twigListener->getLexer();
0 ignored issues
show
Bug introduced by
The method getLexer() does not seem to exist on object<Phake_IMock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
        $this->twigEnvironment->setLexer($lexer);
0 ignored issues
show
Bug introduced by
The method setLexer() does not seem to exist on object<Phake_IMock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
    }
96
97
    /**
98
     * Test render of the template with parameter
99
     */
100 View Code Duplication
    public function testTemplateRenderWithParameter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, self::TEMPLATE);
103
104
        $this->frontRender->setParameters(
105
            [
106
                'applicationName' => 'Test Render',
107
            ]
108
        );
109
110
        $this->assertContains(
111
            'Welcome on Test Render',
112
            $this->frontRender->render()
113
        );
114
    }
115
116
    /**
117
     * Test render of the template without parameter
118
     */
119 View Code Duplication
    public function testTemplateRenderWithoutParameter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        $this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, self::TEMPLATE);
122
123
        $this->assertNotContains(
124
            'Test Render',
125
            $this->frontRender->render()
126
        );
127
    }
128
129
    /**
130
     * Test on the update of lexer
131
     *
132
     * @dataProvider lexerTags
133
     *
134
     * @param string $oldTags
135
     * @param string $newTags
136
     */
137
    public function testSetLexer($oldTags, $newTags)
138
    {
139
        $this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, self::TEMPLATE);
140
141
        $this->frontRender->setParameters(
142
            [
143
                'applicationName' => 'Test Render',
144
            ]
145
        );
146
147
        $render = $this->frontRender->render();
148
149
        $this->assertContains(
150
            $oldTags,
151
            $render
152
        );
153
154
        $this->assertNotContains(
155
            $newTags,
156
            $render
157
        );
158
    }
159
160
    /**
161
     * Test on the failed template
162
     *
163
     * @expectedException Twig_Error_Syntax
164
     */
165
    public function testExceptionOnSyntax()
166
    {
167
        $this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, self::FAILED_TEMPLATE);
168
169
        $this->frontRender->render();
170
    }
171
172
    /**
173
     * Test when the template doesn't exist
174
     *
175
     * @expectedException InvalidArgumentException
176
     */
177
    public function testExceptionOnFailedPath()
178
    {
179
        $this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, self::FAILED_PATH);
180
181
        $this->frontRender->render();
182
    }
183
184
    /**
185
     * Test when the template path is empty
186
     *
187
     * @expectedException Chris\Bundle\FrontRenderBundle\Exception\FrontRenderException
188
     */
189
    public function testExceptionOnEmptyPath()
190
    {
191
        $this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, '');
192
193
        $this->frontRender->render();
194
    }
195
196
    /**
197
     * Provide the lexer tags
198
     *
199
     * @return array
200
     */
201
    public function lexerTags()
202
    {
203
        return [
204
            ['{#', '{*'],
205
            ['{%', '{@'],
206
            ['{{', '{$']
207
        ];
208
209
    }
210
211
212
}
213