-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
EngineTest.php
187 lines (157 loc) · 7.91 KB
/
EngineTest.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
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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Components\Templating;
require_once __DIR__.'/../../../../lib/SymfonyTests/Components/Templating/SimpleHelper.php';
use Symfony\Components\Templating\Engine;
use Symfony\Components\Templating\Loader\Loader;
use Symfony\Components\Templating\Renderer\Renderer;
use Symfony\Components\Templating\Renderer\PhpRenderer;
use Symfony\Components\Templating\Storage\Storage;
use Symfony\Components\Templating\Storage\StringStorage;
use Symfony\Components\Templating\Helper\SlotsHelper;
class EngineTest extends \PHPUnit_Framework_TestCase
{
static protected $loader, $renderer;
static public function setUpBeforeClass()
{
self::$loader = new ProjectTemplateLoader();
self::$renderer = new ProjectTemplateRenderer();
}
public function testConstructor()
{
$engine = new ProjectTemplateEngine(self::$loader);
$this->assertEquals(self::$loader, $engine->getLoader(), '__construct() takes a loader instance as its second first argument');
$this->assertEquals(array('php'), array_keys($engine->getRenderers()), '__construct() automatically registers a PHP renderer if none is given');
$engine = new ProjectTemplateEngine(self::$loader, array('foo' => self::$renderer));
$this->assertEquals(array('foo', 'php'), array_keys($engine->getRenderers()), '__construct() takes an array of renderers as its third argument');
$this->assertTrue(self::$renderer->getEngine() === $engine, '__construct() registers itself on all renderers');
$engine = new ProjectTemplateEngine(self::$loader, array('php' => self::$renderer));
$this->assertTrue($engine->getRenderers() === array('php' => self::$renderer), '__construct() can overridde the default PHP renderer');
}
public function testMagicGet()
{
$engine = new ProjectTemplateEngine(self::$loader);
$engine->set($helper = new \SimpleHelper('bar'), 'foo');
$this->assertEquals($helper, $engine->foo, '->__get() returns the value of a helper');
try {
$engine->bar;
$this->fail('->__get() throws an InvalidArgumentException if the helper is not defined');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->__get() throws an InvalidArgumentException if the helper is not defined');
$this->assertEquals('The helper "bar" is not defined.', $e->getMessage(), '->__get() throws an InvalidArgumentException if the helper is not defined');
}
}
public function testGetSetHas()
{
$engine = new ProjectTemplateEngine(self::$loader);
$foo = new \SimpleHelper('foo');
$engine->set($foo);
$this->assertEquals($foo, $engine->get('foo'), '->set() sets a helper');
$engine->set($foo, 'bar');
$this->assertEquals($foo, $engine->get('bar'), '->set() takes an alias as a second argument');
try {
$engine->get('foobar');
$this->fail('->get() throws an InvalidArgumentException if the helper is not defined');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an InvalidArgumentException if the helper is not defined');
$this->assertEquals('The helper "foobar" is not defined.', $e->getMessage(), '->get() throws an InvalidArgumentException if the helper is not defined');
}
$this->assertTrue($engine->has('foo'), '->has() returns true if the helper exists');
$this->assertFalse($engine->has('foobar'), '->has() returns false if the helper does not exist');
}
public function testExtendRender()
{
$engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper()));
try {
$engine->render('name');
$this->fail('->render() throws an InvalidArgumentException if the template does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->render() throws an InvalidArgumentException if the template does not exist');
$this->assertEquals('The template "name" does not exist (renderer: php).', $e->getMessage(), '->render() throws an InvalidArgumentException if the template does not exist');
}
try {
self::$loader->setTemplate('name.foo', 'foo');
$engine->render('foo:name');
$this->fail('->render() throws an InvalidArgumentException if no renderer is registered for the given renderer');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->render() throws an InvalidArgumentException if no renderer is registered for the given renderer');
$this->assertEquals('The template "foo" does not exist (renderer: name).', $e->getMessage(), '->render() throws an InvalidArgumentException if no renderer is registered for the given renderer');
}
$engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper()));
$engine->set(new \SimpleHelper('bar'));
self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $view->foo.$foo ?>');
self::$loader->setTemplate('layout.php', '-<?php echo $view->slots->get("_content") ?>-');
$this->assertEquals('-barfoo-', $engine->render('foo', array('foo' => 'foo')), '->render() uses the decorator to decorate the template');
$engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper()));
$engine->set(new \SimpleHelper('bar'));
self::$loader->setTemplate('bar.php', 'bar');
self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $foo ?>');
self::$loader->setTemplate('layout.php', '<?php echo $view->render("bar") ?>-<?php echo $view->slots->get("_content") ?>-');
$this->assertEquals('bar-foo-', $engine->render('foo', array('foo' => 'foo', 'bar' => 'bar')), '->render() supports render() calls in templates');
}
public function testEscape()
{
$engine = new ProjectTemplateEngine(self::$loader);
$this->assertEquals('<br />', $engine->escape('<br />'), '->escape() escapes strings');
$foo = new \stdClass();
$this->assertEquals($foo, $engine->escape($foo), '->escape() does nothing on non strings');
}
public function testGetSetCharset()
{
$engine = new ProjectTemplateEngine(self::$loader);
$this->assertEquals('UTF-8', $engine->getCharset(), '->getCharset() returns UTF-8 by default');
$engine->setCharset('ISO-8859-1');
$this->assertEquals('ISO-8859-1', $engine->getCharset(), '->setCharset() changes the default charset to use');
}
}
class ProjectTemplateEngine extends Engine
{
public function getLoader()
{
return $this->loader;
}
public function getRenderers()
{
return $this->renderers;
}
}
class ProjectTemplateRenderer extends PhpRenderer
{
public function getEngine()
{
return $this->engine;
}
}
class ProjectTemplateLoader extends Loader
{
public $templates = array();
public function setTemplate($name, $template)
{
$this->templates[$name] = $template;
}
public function load($template, array $options = array())
{
if (isset($this->templates[$template.'.'.$options['renderer']])) {
return new StringStorage($this->templates[$template.'.'.$options['renderer']]);
}
return false;
}
public function isFresh($template, array $options = array(), $time)
{
return false;
}
}
class FooTemplateRenderer extends Renderer
{
public function evaluate(Storage $template, array $parameters = array())
{
return 'foo';
}
}