This repository has been archived by the owner on Jul 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 725
/
TwigServiceProviderTest.php
110 lines (93 loc) · 3.68 KB
/
TwigServiceProviderTest.php
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
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Provider;
use Silex\Application;
use Silex\Provider\CsrfServiceProvider;
use Silex\Provider\FormServiceProvider;
use Silex\Provider\TwigServiceProvider;
use Silex\Provider\AssetServiceProvider;
use Symfony\Component\HttpFoundation\Request;
/**
* TwigProvider test cases.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase
{
public function testRegisterAndRender()
{
$app = new Application();
$app->register(new TwigServiceProvider(), array(
'twig.templates' => array('hello' => 'Hello {{ name }}!'),
));
$app->get('/hello/{name}', function ($name) use ($app) {
return $app['twig']->render('hello', array('name' => $name));
});
$request = Request::create('/hello/john');
$response = $app->handle($request);
$this->assertEquals('Hello john!', $response->getContent());
}
public function testLoaderPriority()
{
$app = new Application();
$app->register(new TwigServiceProvider(), array(
'twig.templates' => array('foo' => 'foo'),
));
$loader = $this->getMock('\Twig_LoaderInterface');
$loader->expects($this->never())->method('getSource');
$app['twig.loader.filesystem'] = function ($app) use ($loader) {
return $loader;
};
$this->assertEquals('foo', $app['twig.loader']->getSource('foo'));
}
public function testHttpFoundationIntegration()
{
$app = new Application();
$app['request_stack']->push(Request::create('/dir1/dir2/file'));
$app->register(new TwigServiceProvider(), array(
'twig.templates' => array(
'absolute' => '{{ absolute_url("foo.css") }}',
'relative' => '{{ relative_path("/dir1/foo.css") }}',
),
));
$this->assertEquals('http://localhost/dir1/dir2/foo.css', $app['twig']->render('absolute'));
$this->assertEquals('../foo.css', $app['twig']->render('relative'));
}
public function testAssetIntegration()
{
$app = new Application();
$app->register(new TwigServiceProvider(), array(
'twig.templates' => array('hello' => '{{ asset("/foo.css") }}'),
));
$app->register(new AssetServiceProvider(), array(
'assets.version' => 1,
));
$this->assertEquals('/foo.css?1', $app['twig']->render('hello'));
}
public function testGlobalVariable()
{
$app = new Application();
$app['request_stack']->push(Request::create('/?name=Fabien'));
$app->register(new TwigServiceProvider(), array(
'twig.templates' => array('hello' => '{{ global.request.get("name") }}'),
));
$this->assertEquals('Fabien', $app['twig']->render('hello'));
}
public function testFormFactory()
{
$app = new Application();
$app->register(new FormServiceProvider());
$app->register(new CsrfServiceProvider());
$app->register(new TwigServiceProvider());
$this->assertInstanceOf('Twig_Environment', $app['twig'], 'Service twig is created successful.');
$this->assertInstanceOf('Symfony\Bridge\Twig\Form\TwigRendererEngine', $app['twig.form.engine'], 'Service twig.form.engine is created successful.');
$this->assertInstanceOf('Symfony\Bridge\Twig\Form\TwigRenderer', $app['twig.form.renderer'], 'Service twig.form.renderer is created successful.');
}
}