summaryrefslogtreecommitdiffstats
path: root/tests/Controller/View/TwigTest.php
blob: e46757e7ee5ce6f12f58466cc7572f7bfe1873d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php

namespace Jasny\Controller\View;

use Jasny\Controller\View;
use Jasny\Controller\Session\Flash;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Jasny\Controller\TestHelper;

/**
 * @covers Jasny\Controller\View\Twig
 */
class TwigTest extends \PHPUnit_Framework_TestCase
{
    use TestHelper;
    
    protected function getControllerClass()
    {
        return View\Twig::class;
    }

    /**
     * Test creating twig environment
     */
    public function testCreateTwigEnvironment()
    {
        $controller = $this->getController([]);
        $twig = $controller->createTwigEnvironment();
        
        $this->assertInstanceOf(\Twig_Environment::class, $twig);
        $this->assertInstanceOf(\Twig_Loader_Filesystem::class, $twig->getLoader());
        $this->assertEquals([getcwd()], $twig->getLoader()->getPaths());
    }
    
    /**
     * Test intializing twig environment
     */
    public function testInitTwig()
    {
        $uri = $this->createMock(UriInterface::class);
        
        $request = $this->createMock(ServerRequestInterface::class);
        $request->expects($this->once())->method('getUri')->willReturn($uri);
        
        $twig = $this->createMock(\Twig_Environment::class);
        $twig->expects($this->once())->method('addGlobal')->with('current_url', $this->identicalTo($uri));

        $controller = $this->getController(['createTwigEnvironment']);
        $controller->expects($this->any())->method('getRequest')->willReturn($request);
        $controller->expects($this->once())->method('createTwigEnvironment')->willReturn($twig);
        
        $controller->getTwig();
    }

    /**
     * Test Jasny Twig extensions when intializing twig environment
     */
    public function testInitTwigWithJasnyExtensions()
    {
        $request = $this->createMock(ServerRequestInterface::class);
        
        $twig = $this->createMock(\Twig_Environment::class);
        $twig->expects($this->exactly(4))->method('addExtension')->withConsecutive(
            [$this->isInstanceOf('Jasny\Twig\DateExtension')],
            [$this->isInstanceOf('Jasny\Twig\PcreExtension')],
            [$this->isInstanceOf('Jasny\Twig\TextExtension')],
            [$this->isInstanceOf('Jasny\Twig\ArrayExtension')]
        );
        
        $controller = $this->getController(['createTwigEnvironment']);
        $controller->expects($this->any())->method('getRequest')->willReturn($request);
        $controller->expects($this->once())->method('createTwigEnvironment')->willReturn($twig);
        
        $controller->getTwig();
    }

    /**
     * Test session flash when intializing twig environment
     */
    public function testInitTwigWithSessionFlash()
    {
        $request = $this->createMock(ServerRequestInterface::class);
        $flash = $this->createMock(Flash::class);
        
        $twig = $this->createMock(\Twig_Environment::class);
        $twig->expects($this->any())->method('addGlobal')->withConsecutive([], ['flash', $flash]);
        
        $controller = $this->getController(['createTwigEnvironment', 'flash']);
        $controller->expects($this->any())->method('getRequest')->willReturn($request);
        $controller->expects($this->once())->method('flash')->willReturn($flash);
        $controller->expects($this->once())->method('createTwigEnvironment')->willReturn($twig);
        
        $controller->getTwig();
    }

    /**
     * Provide data for testing 'setViewVariable' method
     *
     * @return array
     */
    public function setViewVariableProvider()
    {
        return [
            ['foo', null],
            ['foo', 'bar'],
            ['foo', ['bar', 'zoo']],
            ['foo', (object)['a' => 'bar', 'b' => 'zoo']],
        ];  
    }
    
    /**
     * Test 'setViewVariable' method
     *
     * @dataProvider setViewVariableProvider
     */
    public function testSetViewVariable($name, $value)
    {
        $twig = $this->createMock(\Twig_Environment::class);
        
        $controller = $this->getController(['getTwig']);
        $controller->method('getTwig')->willReturn($twig);
        
        $twig->expects($this->once())->method('addGlobal')->with($name, $value);            

        $result = $controller->setViewVariable($name, $value);

        $this->assertSame($controller, $result);
    }

    
    /**
     * Provide data for testing 'setViewFunction' method when creating functions
     *
     * @return array
     */
    public function setViewFunctionProvider()
    {
        return [
            ['test_name', function() {}],
            ['str_rot13'],
            ['obfuscate', 'str_rot13']
        ];
    }

    /**
     * Test 'setViewFunction' method for adding functions
     * @dataProvider setViewFunctionProvider
     * 
     * @param string $name 
     * @param callable $callable
     */
    public function testSetViewFunctionFunction($name, $callable = null)
    {
        $twig = $this->createMock(\Twig_Environment::class);
        
        $controller = $this->getController(['getTwig']);
        $controller->method('getTwig')->willReturn($twig);
        
        $fn = $callable ?: $name;
        
        $twig->expects($this->once())->method('addFunction')
            ->with($this->callback(function($function) use ($name, $fn) {
                $this->assertInstanceOf(\Twig_SimpleFunction::class, $function);
                $this->assertEquals($name, $function->getName());
                $this->assertSame($fn, $function->getCallable());
                return true;
            }));
            
        $twig->expects($this->never())->method('addFilter');

        $controller->setViewFunction($name, $callable, 'function');
    }

    /**
     * Test 'setViewFunction' method for adding filters
     * @dataProvider setViewFunctionProvider
     * 
     * @param string $name 
     * @param callable $callable
     */
    public function testSetViewFunctionFilter($name, $callable = null)
    {
        $twig = $this->createMock(\Twig_Environment::class);
        
        $controller = $this->getController(['getTwig']);
        $controller->method('getTwig')->willReturn($twig);
        
        $fn = $callable ?: $name;
        
        $twig->expects($this->once())->method('addFilter')
            ->with($this->callback(function($function) use ($name, $fn) {
                $this->assertInstanceOf(\Twig_SimpleFilter::class, $function);
                $this->assertEquals($name, $function->getName());
                $this->assertSame($fn, $function->getCallable());
                return true;
            }));
        
        $twig->expects($this->never())->method('addFunction');

        $controller->setViewFunction($name, $callable, 'filter');
    }
    
    
    public function invalidAsProvider()
    {
        return [
            ['foo', "'foo'"],
            [10, 'a integer'],
            [['filter'], 'a array']
        ];
    }
    
    /**
     * @dataProvider invalidAsProvider
     * 
     * @param mixed  $as
     * @param string $not
     */
    public function testSetViewFunctionInvalid($as, $not)
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage("You should create either a 'function' or 'filter', not $not");
        
        $twig = $this->createMock(\Twig_Environment::class);
        
        $controller = $this->getController(['getTwig']);
        $controller->method('getTwig')->willReturn($twig);
        
        $controller->setViewFunction('abc', null, $as);
    }
    
    
    public function assertViewVariableNameProvider()
    {
        return [
            ['setViewVariable'],
            ['setViewFunction', 'function'],
            ['setViewFunction', 'filter']
        ];
    }
    
    /**
     * @dataProvider assertViewVariableNameProvider
     * 
     * @expectedException InvalidArgumentException
     * @expectedExceptionMessage Expected name to be a string, not a stdClass object
     */
    public function testAssertViewVariableNameNonString($fn, $as = null)
    {
        $twig = $this->createMock(\Twig_Environment::class);
        
        $controller = $this->getController(['getTwig']);
        $controller->method('getTwig')->willReturn($twig);
        
        $controller->$fn(new \stdClass(), null, $as);
    }
    
    /**
     * @dataProvider assertViewVariableNameProvider
     * 
     * @expectedException InvalidArgumentException
     * @expectedExceptionMessage Invalid name 'hello world'
     */
    public function testAssertViewVariableNameInvalid($fn, $as = null)
    {
        $twig = $this->createMock(\Twig_Environment::class);
        
        $controller = $this->getController(['getTwig']);
        $controller->method('getTwig')->willReturn($twig);
        
        $controller->$fn('hello world', null, $as);
    }
    
    
    public function viewProvider()
    {
        return [
            ['foo', 'foo.html.twig'],
            ['foo.html.twig', 'foo.html.twig'],
            ['foo.html', 'foo.html']
        ];
    }
    
    /**
     * @dataProvider viewProvider
     * 
     * @param string $name
     * @param string $filename
     */
    public function testView($name, $filename)
    {
        $context = ['foo' => 1, 'bar' => 2, 'zoo' => ['monkey', 'lion']];
        
        $template = $this->createMock(\Twig_TemplateInterface::class);
        $template->expects($this->once())->method('render')->with($context)->willReturn('hello world');
        
        $twig = $this->createMock(\Twig_Environment::class);
        $twig->expects($this->once())->method('loadTemplate')->with($filename)->willReturn($template);
        $twig->expects($this->once())->method('getCharset')->willReturn('test-charset');
        
        $controller = $this->getController(['getTwig', 'output']);
        $controller->expects($this->atLeastOnce())->method('getTwig')->willReturn($twig);
        $controller->expects($this->once())->method('output')->with('hello world', 'text/html; charset=test-charset');
        
        $controller->view($name, $context);
    }
}