12require_once dirname(__FILE__).
'/FilesystemHelper.php';
25 $this->assertEquals(
'{{ foo }}',
$stream->getSource());
26 $this->assertEquals(
'foo',
$stream->getFilename());
36 $this->assertContains(
'getTemplateName',
$env->compileSource(
'{{ foo }}',
'foo'));
53 'html' =>
'{{ foo }} {{ foo }}',
54 'js' =>
'{{ bar }} {{ bar }}',
60 'autoescape' => array($this,
'escapingStrategyCallback'),
63 $this->assertEquals(
'foo<br/ > foo<br/ >', $twig->render(
'html', array(
'foo' =>
'foo<br/ >')));
64 $this->assertEquals(
'foo\x3Cbr\x2F\x20\x3E foo\x3Cbr\x2F\x20\x3E', $twig->render(
'js', array(
'bar' =>
'foo<br/ >')));
75 $loader = $this->getMockBuilder(
'Twig_EnvironmentTestLoaderInterface')->getMock();
77 $loader->expects($this->any())->method(
'getSourceContext')->will($this->returnValue(
new Twig_Source(
'',
'')));
82 $twig->addGlobal(
'foo',
'foo');
84 $twig->addGlobal(
'foo',
'bar');
85 $globals = $twig->getGlobals();
86 $this->assertEquals(
'bar', $globals[
'foo']);
90 $twig->addGlobal(
'foo',
'foo');
92 $twig->loadTemplate(
'index');
93 $twig->addGlobal(
'foo',
'bar');
94 $globals = $twig->getGlobals();
95 $this->assertEquals(
'bar', $globals[
'foo']);
99 $twig->addGlobal(
'foo',
'foo');
101 $twig->getFunctions();
102 $twig->addGlobal(
'foo',
'bar');
103 $globals = $twig->getGlobals();
104 $this->assertEquals(
'bar', $globals[
'foo']);
109 $twig->addGlobal(
'foo',
'foo');
111 $twig->getFunctions();
112 $twig->loadTemplate(
'index');
113 $twig->addGlobal(
'foo',
'bar');
114 $globals = $twig->getGlobals();
115 $this->assertEquals(
'bar', $globals[
'foo']);
119 $twig->addGlobal(
'foo',
'bar');
120 $template = $twig->loadTemplate(
'index');
121 $this->assertEquals(
'bar',
$template->render(array()));
176 $options = array(
'cache' => $cache,
'auto_reload' =>
false,
'debug' =>
false);
181 $key = $cache->generateKey(
'index', $twig->getTemplateClass(
'index'));
182 $cache->write(
$key, $twig->compileSource(
new Twig_Source(
'{{ foo }}',
'index')));
186 ->getMockBuilder(
'Twig_Environment')
187 ->setConstructorArgs(array($loader,
$options))
188 ->setMethods(array(
'initExtensions'))
192 $twig->expects($this->never())->method(
'initExtensions');
195 $output = $twig->render(
'index', array(
'foo' =>
'bar'));
196 $this->assertEquals(
'bar',
$output);
203 $templateName = __FUNCTION__;
204 $templateContent = __FUNCTION__;
206 $cache = $this->getMockBuilder(
'Twig_CacheInterface')->getMock();
207 $loader = $this->
getMockLoader($templateName, $templateContent);
208 $twig =
new Twig_Environment($loader, array(
'cache' => $cache,
'auto_reload' =>
true,
'debug' =>
false));
212 $cache->expects($this->
once())
213 ->method(
'generateKey')
214 ->will($this->returnValue(
'key'));
215 $cache->expects($this->
once())
216 ->method(
'getTimestamp')
217 ->will($this->returnValue(0));
218 $loader->expects($this->never())
220 $cache->expects($this->
once())
222 $cache->expects($this->
once())
225 $twig->loadTemplate($templateName);
230 $templateName = __FUNCTION__;
231 $templateContent = __FUNCTION__;
233 $cache = $this->getMockBuilder(
'Twig_CacheInterface')->getMock();
234 $loader = $this->
getMockLoader($templateName, $templateContent);
235 $twig =
new Twig_Environment($loader, array(
'cache' => $cache,
'auto_reload' =>
true,
'debug' =>
false));
241 $cache->expects($this->
once())
242 ->method(
'generateKey')
243 ->will($this->returnValue(
'key'));
244 $cache->expects($this->
once())
245 ->method(
'getTimestamp')
246 ->will($this->returnValue($now));
247 $loader->expects($this->
once())
249 ->will($this->returnValue(
true));
250 $cache->expects($this->atLeastOnce())
253 $twig->loadTemplate($templateName);
258 $templateName = __FUNCTION__;
259 $templateContent = __FUNCTION__;
261 $cache = $this->getMockBuilder(
'Twig_CacheInterface')->getMock();
262 $loader = $this->
getMockLoader($templateName, $templateContent);
263 $twig =
new Twig_Environment($loader, array(
'cache' => $cache,
'auto_reload' =>
true,
'debug' =>
false));
267 $cache->expects($this->
once())
268 ->method(
'generateKey')
269 ->will($this->returnValue(
'key'));
270 $cache->expects($this->
once())
271 ->method(
'getTimestamp')
272 ->will($this->returnValue($now));
273 $loader->expects($this->
once())
275 ->will($this->returnValue(
false));
276 $cache->expects($this->
once())
278 $cache->expects($this->
once())
281 $twig->loadTemplate($templateName);
289 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_LoaderInterface')->getMock());
293 $twig->addExtension($ext1);
294 $twig->addExtension($ext2);
296 $this->assertTrue($twig->hasExtension(
'ext1'));
297 $this->assertTrue($twig->hasExtension(
'ext2'));
299 $this->assertTrue($twig->hasExtension(
'Twig_Tests_EnvironmentTest_Extension_DynamicWithDeprecatedName'));
301 $this->assertSame($ext1, $twig->getExtension(
'ext1'));
302 $this->assertSame($ext2, $twig->getExtension(
'ext2'));
307 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_LoaderInterface')->getMock());
309 $this->assertTrue($twig->hasExtension(
'Twig_Tests_EnvironmentTest_Extension'));
310 $this->assertTrue($twig->hasExtension(
'\Twig_Tests_EnvironmentTest_Extension'));
312 $this->assertSame($ext, $twig->getExtension(
'Twig_Tests_EnvironmentTest_Extension'));
313 $this->assertSame($ext, $twig->getExtension(
'\Twig_Tests_EnvironmentTest_Extension'));
315 $this->assertTrue($twig->hasExtension(
'Twig\Tests\EnvironmentTest\Extension'));
316 $this->assertSame($ext, $twig->getExtension(
'Twig\Tests\EnvironmentTest\Extension'));
321 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_LoaderInterface')->getMock());
324 $this->assertArrayHasKey(
'test', $twig->getTags());
325 $this->assertArrayHasKey(
'foo_filter', $twig->getFilters());
326 $this->assertArrayHasKey(
'foo_function', $twig->getFunctions());
327 $this->assertArrayHasKey(
'foo_test', $twig->getTests());
328 $this->assertArrayHasKey(
'foo_unary', $twig->getUnaryOperators());
329 $this->assertArrayHasKey(
'foo_binary', $twig->getBinaryOperators());
330 $this->assertArrayHasKey(
'foo_global', $twig->getGlobals());
331 $visitors = $twig->getNodeVisitors();
333 foreach ($visitors as $visitor) {
338 $this->assertTrue($found);
346 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_LoaderInterface')->getMock());
349 $this->deprecations = array();
350 set_error_handler(array($this,
'handleError'));
352 $this->assertArrayHasKey(
'foo_global', $twig->getGlobals());
354 $this->assertCount(1, $this->deprecations);
355 $this->assertContains(
'Defining the getGlobals() method in the "Twig_Tests_EnvironmentTest_Extension_WithGlobals" extension ', $this->deprecations[0]);
357 restore_error_handler();
365 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_LoaderInterface')->getMock());
367 $twig->removeExtension(
'environment_test');
369 $this->assertArrayNotHasKey(
'test', $twig->getTags());
370 $this->assertArrayNotHasKey(
'foo_filter', $twig->getFilters());
371 $this->assertArrayNotHasKey(
'foo_function', $twig->getFunctions());
372 $this->assertArrayNotHasKey(
'foo_test', $twig->getTests());
373 $this->assertArrayNotHasKey(
'foo_unary', $twig->getUnaryOperators());
374 $this->assertArrayNotHasKey(
'foo_binary', $twig->getBinaryOperators());
375 $this->assertArrayNotHasKey(
'foo_global', $twig->getGlobals());
376 $this->assertCount(2, $twig->getNodeVisitors());
383 $extension = eval(<<<
EOF
384class Twig_Tests_EnvironmentTest_ExtensionInEval extends
Twig_Extension
389 $extension =
new Twig_Tests_EnvironmentTest_ExtensionInEval();
394 $twig->addExtension($extension);
396 $this->assertInstanceOf(
'Twig_ExtensionInterface', $twig->getExtension(get_class($extension)));
397 $this->assertTrue($twig->isTemplateFresh(
'page', time()));
402 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_LoaderInterface')->getMock());
404 $twig->initRuntime();
408 $this->addToAssertionCount(1);
416 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_LoaderInterface')->getMock());
419 $this->deprecations = array();
420 set_error_handler(array($this,
'handleError'));
422 $twig->initRuntime();
424 $this->assertCount(1, $this->deprecations);
425 $this->assertContains(
'Defining the initRuntime() method in the "Twig_Tests_EnvironmentTest_ExtensionWithDeprecationInitRuntime" extension is deprecated since version 1.23.', $this->deprecations[0]);
427 restore_error_handler();
432 if (E_USER_DEPRECATED ===
$type) {
433 $this->deprecations[] = $msg;
442 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_LoaderInterface')->getMock());
445 $this->deprecations = array();
446 set_error_handler(array($this,
'handleError'));
451 $this->assertCount(1, $this->deprecations);
452 $this->assertContains(
'The possibility to register the same extension twice', $this->deprecations[0]);
454 restore_error_handler();
459 $runtimeLoader = $this->getMockBuilder(
'Twig_RuntimeLoaderInterface')->getMock();
463 'func_array' =>
'{{ from_runtime_array("foo") }}',
464 'func_array_default' =>
'{{ from_runtime_array() }}',
465 'func_array_named_args' =>
'{{ from_runtime_array(name="foo") }}',
466 'func_string' =>
'{{ from_runtime_string("foo") }}',
467 'func_string_default' =>
'{{ from_runtime_string() }}',
468 'func_string_named_args' =>
'{{ from_runtime_string(name="foo") }}',
473 $twig->addRuntimeLoader($runtimeLoader);
475 $this->assertEquals(
'foo', $twig->render(
'func_array'));
476 $this->assertEquals(
'bar', $twig->render(
'func_array_default'));
477 $this->assertEquals(
'foo', $twig->render(
'func_array_named_args'));
478 $this->assertEquals(
'foo', $twig->render(
'func_string'));
479 $this->assertEquals(
'bar', $twig->render(
'func_string_default'));
480 $this->assertEquals(
'foo', $twig->render(
'func_string_named_args'));
490 'base.html.twig' =>
'{% extends "base.html.twig" %}',
493 $twig->loadTemplate(
'base.html.twig');
503 'base1.html.twig' =>
'{% extends "base2.html.twig" %}',
504 'base2.html.twig' =>
'{% extends "base1.html.twig" %}',
507 $twig->loadTemplate(
'base1.html.twig');
513 $loader = $this->getMockBuilder(
'Twig_EnvironmentTestLoaderInterface')->getMock();
515 $loader->expects($this->any())
516 ->method(
'getSourceContext')
517 ->with($templateName)
518 ->will($this->returnValue(
new Twig_Source($templateContent, $templateName)));
519 $loader->expects($this->any())
520 ->method(
'getCacheKey')
521 ->with($templateName)
522 ->will($this->returnValue($templateName));
533 'foo_global' =>
'foo_global',
578 array(
'foo_unary' => array()),
579 array(
'foo_binary' => array()),
586 'foo_global' =>
'foo_global',
590class_alias(
'Twig_Tests_EnvironmentTest_Extension',
'Twig\Tests\EnvironmentTest\Extension',
false);
596 return 'environment_test';
664 new Twig_SimpleFunction(
'from_runtime_array', array(
'Twig_Tests_EnvironmentTest_Runtime',
'fromRuntime')),
665 new Twig_SimpleFunction(
'from_runtime_string',
'Twig_Tests_EnvironmentTest_Runtime::fromRuntime'),
671 return 'from_runtime';
const EOF
How fgetc() reports an End Of File.
An exception for terminatinating execution or to throw for unit testing.
Implements a cache on the filesystem.
Stores the Twig configuration.
Loads a template from an array.
Represents a template filter.
Represents a template function.
Represents a template test.
Holds information about a non-compiled Twig template.
initRuntime(Twig_Environment $env)
initRuntime(Twig_Environment $env)
getFunctions()
Returns a list of functions to add to the existing list.
getOperators()
Returns a list of operators to add to the existing list.
getTokenParsers()
Returns the token parser instances to add to the existing list.
getFunctions()
Returns a list of functions to add to the existing list.
getNodeVisitors()
Returns the node visitor instances to add to the existing list.
getTests()
Returns a list of tests to add to the existing list.
getFilters()
Returns a list of filters to add to the existing list.
enterNode(Twig_NodeInterface $node, Twig_Environment $env)
Called before child nodes are visited.
leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
Called after child nodes are visited.
getPriority()
Returns the priority for this visitor.
parse(Twig_Token $token)
Parses a token and returns a node.
getTag()
Gets the tag name associated with this token parser.
testOverrideExtension()
@requires PHP 5.3
testHasGetExtensionByClassName()
testLegacyTokenizeSignature()
@group legacy
testRenderNoLoader()
@expectedException LogicException @expectedExceptionMessage You must set a loader first.
testInitRuntimeWithAnExtensionUsingInitRuntimeDeprecation()
@requires PHP 5.3
testAutoReloadOutdatedCacheHit()
testFailLoadTemplateOnComplexCircularReference()
@expectedException Twig_Error_Runtime @expectedExceptionMessage Circular reference detected for Twig ...
testFailLoadTemplateOnCircularReference()
@expectedException Twig_Error_Runtime @expectedExceptionMessage Circular reference detected for Twig ...
testRemoveExtension()
@group legacy
testLegacyCompileSourceSignature()
@group legacy
testAutoReloadCacheMiss()
testInitRuntimeWithAnExtensionUsingInitRuntimeNoDeprecation()
testHasGetExtensionWithDynamicName()
@group legacy
testAddExtensionWithDeprecatedGetGlobals()
@requires PHP 5.3
getMockLoader($templateName, $templateContent)
testExtensionsAreNotInitializedWhenRenderingACompiledTemplate()
escapingStrategyCallback($name)
Base class for all token parsers.
Enables usage of the deprecated Twig_Extension::getGlobals() method.
Enables usage of the deprecated Twig_Extension::initRuntime() method.
Interface all loaders must implement.
Represents a node in the AST.
Twig_NodeVisitorInterface is the interface the all node visitor classes must implement.
Adds a getSourceContext() method for loaders.
$stream
PHP stream implementation.
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.