This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,169 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\DelegatingEngine;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Templating\StreamingEngineInterface;
class DelegatingEngineTest extends TestCase
{
public function testRenderDelegatesToSupportedEngine()
{
$firstEngine = $this->getEngineMock('template.php', false);
$secondEngine = $this->getEngineMock('template.php', true);
$secondEngine->expects($this->once())
->method('render')
->with('template.php', ['foo' => 'bar'])
->willReturn('<html />');
$delegatingEngine = new DelegatingEngine([$firstEngine, $secondEngine]);
$result = $delegatingEngine->render('template.php', ['foo' => 'bar']);
$this->assertSame('<html />', $result);
}
public function testRenderWithNoSupportedEngine()
{
$this->expectException('RuntimeException');
$this->expectExceptionMessage('No engine is able to work with the template "template.php"');
$firstEngine = $this->getEngineMock('template.php', false);
$secondEngine = $this->getEngineMock('template.php', false);
$delegatingEngine = new DelegatingEngine([$firstEngine, $secondEngine]);
$delegatingEngine->render('template.php', ['foo' => 'bar']);
}
public function testStreamDelegatesToSupportedEngine()
{
$streamingEngine = $this->getStreamingEngineMock('template.php', true);
$streamingEngine->expects($this->once())
->method('stream')
->with('template.php', ['foo' => 'bar'])
->willReturn('<html />');
$delegatingEngine = new DelegatingEngine([$streamingEngine]);
$result = $delegatingEngine->stream('template.php', ['foo' => 'bar']);
$this->assertNull($result);
}
public function testStreamRequiresStreamingEngine()
{
$this->expectException('LogicException');
$this->expectExceptionMessage('Template "template.php" cannot be streamed as the engine supporting it does not implement StreamingEngineInterface');
$delegatingEngine = new DelegatingEngine([new TestEngine()]);
$delegatingEngine->stream('template.php', ['foo' => 'bar']);
}
public function testExists()
{
$engine = $this->getEngineMock('template.php', true);
$engine->expects($this->once())
->method('exists')
->with('template.php')
->willReturn(true);
$delegatingEngine = new DelegatingEngine([$engine]);
$this->assertTrue($delegatingEngine->exists('template.php'));
}
public function testSupports()
{
$engine = $this->getEngineMock('template.php', true);
$delegatingEngine = new DelegatingEngine([$engine]);
$this->assertTrue($delegatingEngine->supports('template.php'));
}
public function testSupportsWithNoSupportedEngine()
{
$engine = $this->getEngineMock('template.php', false);
$delegatingEngine = new DelegatingEngine([$engine]);
$this->assertFalse($delegatingEngine->supports('template.php'));
}
public function testGetExistingEngine()
{
$firstEngine = $this->getEngineMock('template.php', false);
$secondEngine = $this->getEngineMock('template.php', true);
$delegatingEngine = new DelegatingEngine([$firstEngine, $secondEngine]);
$this->assertSame($secondEngine, $delegatingEngine->getEngine('template.php'));
}
public function testGetInvalidEngine()
{
$this->expectException('RuntimeException');
$this->expectExceptionMessage('No engine is able to work with the template "template.php"');
$firstEngine = $this->getEngineMock('template.php', false);
$secondEngine = $this->getEngineMock('template.php', false);
$delegatingEngine = new DelegatingEngine([$firstEngine, $secondEngine]);
$delegatingEngine->getEngine('template.php');
}
private function getEngineMock($template, $supports)
{
$engine = $this->getMockBuilder('Symfony\Component\Templating\EngineInterface')->getMock();
$engine->expects($this->once())
->method('supports')
->with($template)
->willReturn($supports);
return $engine;
}
private function getStreamingEngineMock($template, $supports)
{
$engine = $this->getMockForAbstractClass('Symfony\Component\Templating\Tests\MyStreamingEngine');
$engine->expects($this->once())
->method('supports')
->with($template)
->willReturn($supports);
return $engine;
}
}
interface MyStreamingEngine extends StreamingEngineInterface, EngineInterface
{
}
class TestEngine implements EngineInterface
{
public function render($name, array $parameters = [])
{
}
public function exists($name)
{
}
public function supports($name)
{
return true;
}
public function stream()
{
}
}

View File

@@ -0,0 +1,34 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Fixtures;
use Symfony\Component\Templating\Helper\Helper;
class SimpleHelper extends Helper
{
protected $value = '';
public function __construct($value)
{
$this->value = $value;
}
public function __toString()
{
return $this->value;
}
public function getName()
{
return 'foo';
}
}

View File

@@ -0,0 +1 @@
<?php echo $foo ?>

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Helper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Helper\Helper;
class HelperTest extends TestCase
{
public function testGetSetCharset()
{
$helper = new ProjectTemplateHelper();
$helper->setCharset('ISO-8859-1');
$this->assertSame('ISO-8859-1', $helper->getCharset(), '->setCharset() sets the charset set related to this helper');
}
}
class ProjectTemplateHelper extends Helper
{
public function getName()
{
return 'foo';
}
}

View File

@@ -0,0 +1,81 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Helper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Helper\SlotsHelper;
class SlotsHelperTest extends TestCase
{
public function testHasGetSet()
{
$helper = new SlotsHelper();
$helper->set('foo', 'bar');
$this->assertEquals('bar', $helper->get('foo'), '->set() sets a slot value');
$this->assertEquals('bar', $helper->get('bar', 'bar'), '->get() takes a default value to return if the slot does not exist');
$this->assertTrue($helper->has('foo'), '->has() returns true if the slot exists');
$this->assertFalse($helper->has('bar'), '->has() returns false if the slot does not exist');
}
public function testOutput()
{
$helper = new SlotsHelper();
$helper->set('foo', 'bar');
ob_start();
$ret = $helper->output('foo');
$output = ob_get_clean();
$this->assertEquals('bar', $output, '->output() outputs the content of a slot');
$this->assertTrue($ret, '->output() returns true if the slot exists');
ob_start();
$ret = $helper->output('bar', 'bar');
$output = ob_get_clean();
$this->assertEquals('bar', $output, '->output() takes a default value to return if the slot does not exist');
$this->assertTrue($ret, '->output() returns true if the slot does not exist but a default value is provided');
ob_start();
$ret = $helper->output('bar');
$output = ob_get_clean();
$this->assertEquals('', $output, '->output() outputs nothing if the slot does not exist');
$this->assertFalse($ret, '->output() returns false if the slot does not exist');
}
public function testStartStop()
{
$helper = new SlotsHelper();
$helper->start('bar');
echo 'foo';
$helper->stop();
$this->assertEquals('foo', $helper->get('bar'), '->start() starts a slot');
$this->assertTrue($helper->has('bar'), '->starts() starts a slot');
$helper->start('bar');
try {
$helper->start('bar');
$helper->stop();
$this->fail('->start() throws an InvalidArgumentException if a slot with the same name is already started');
} catch (\Exception $e) {
$helper->stop();
$this->assertInstanceOf('\InvalidArgumentException', $e, '->start() throws an InvalidArgumentException if a slot with the same name is already started');
$this->assertEquals('A slot named "bar" is already started.', $e->getMessage(), '->start() throws an InvalidArgumentException if a slot with the same name is already started');
}
try {
$helper->stop();
$this->fail('->stop() throws an LogicException if no slot is started');
} catch (\Exception $e) {
$this->assertInstanceOf('\LogicException', $e, '->stop() throws an LogicException if no slot is started');
$this->assertEquals('No slot started.', $e->getMessage(), '->stop() throws an LogicException if no slot is started');
}
}
}

View File

@@ -0,0 +1,94 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Loader\CacheLoader;
use Symfony\Component\Templating\Loader\Loader;
use Symfony\Component\Templating\Storage\StringStorage;
use Symfony\Component\Templating\TemplateReference;
use Symfony\Component\Templating\TemplateReferenceInterface;
class CacheLoaderTest extends TestCase
{
public function testConstructor()
{
$loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), sys_get_temp_dir());
$this->assertSame($loader->getLoader(), $varLoader, '__construct() takes a template loader as its first argument');
$this->assertEquals(sys_get_temp_dir(), $loader->getDir(), '__construct() takes a directory where to store the cache as its second argument');
}
public function testLoad()
{
$dir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.mt_rand(111111, 999999);
mkdir($dir, 0777, true);
$loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), $dir);
$this->assertFalse($loader->load(new TemplateReference('foo', 'php')), '->load() returns false if the embed loader is not able to load the template');
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$logger
->expects($this->once())
->method('debug')
->with('Storing template in cache.', ['name' => 'index']);
$loader->setLogger($logger);
$loader->load(new TemplateReference('index'));
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$logger
->expects($this->once())
->method('debug')
->with('Fetching template from cache.', ['name' => 'index']);
$loader->setLogger($logger);
$loader->load(new TemplateReference('index'));
}
}
class ProjectTemplateLoader extends CacheLoader
{
public function getDir()
{
return $this->dir;
}
public function getLoader()
{
return $this->loader;
}
}
class ProjectTemplateLoaderVar extends Loader
{
public function getIndexTemplate()
{
return 'Hello World';
}
public function getSpecialTemplate()
{
return 'Hello {{ name }}';
}
public function load(TemplateReferenceInterface $template)
{
if (method_exists($this, $method = 'get'.ucfirst($template->get('name')).'Template')) {
return new StringStorage($this->$method());
}
return false;
}
public function isFresh(TemplateReferenceInterface $template, $time)
{
return false;
}
}

View File

@@ -0,0 +1,63 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Loader\ChainLoader;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Component\Templating\TemplateReference;
class ChainLoaderTest extends TestCase
{
protected $loader1;
protected $loader2;
protected function setUp()
{
$fixturesPath = realpath(__DIR__.'/../Fixtures/');
$this->loader1 = new FilesystemLoader($fixturesPath.'/null/%name%');
$this->loader2 = new FilesystemLoader($fixturesPath.'/templates/%name%');
}
public function testConstructor()
{
$loader = new ProjectTemplateLoader1([$this->loader1, $this->loader2]);
$this->assertEquals([$this->loader1, $this->loader2], $loader->getLoaders(), '__construct() takes an array of template loaders as its second argument');
}
public function testAddLoader()
{
$loader = new ProjectTemplateLoader1([$this->loader1]);
$loader->addLoader($this->loader2);
$this->assertEquals([$this->loader1, $this->loader2], $loader->getLoaders(), '->addLoader() adds a template loader at the end of the loaders');
}
public function testLoad()
{
$loader = new ProjectTemplateLoader1([$this->loader1, $this->loader2]);
$this->assertFalse($loader->load(new TemplateReference('bar', 'php')), '->load() returns false if the template is not found');
$this->assertFalse($loader->load(new TemplateReference('foo', 'php')), '->load() returns false if the template does not exist for the given renderer');
$this->assertInstanceOf(
'Symfony\Component\Templating\Storage\FileStorage',
$loader->load(new TemplateReference('foo.php', 'php')),
'->load() returns a FileStorage if the template exists'
);
}
}
class ProjectTemplateLoader1 extends ChainLoader
{
public function getLoaders()
{
return $this->loaders;
}
}

View File

@@ -0,0 +1,85 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Component\Templating\TemplateReference;
class FilesystemLoaderTest extends TestCase
{
protected static $fixturesPath;
public static function setUpBeforeClass()
{
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
}
public function testConstructor()
{
$pathPattern = self::$fixturesPath.'/templates/%name%.%engine%';
$loader = new ProjectTemplateLoader2($pathPattern);
$this->assertEquals([$pathPattern], $loader->getTemplatePathPatterns(), '__construct() takes a path as its second argument');
$loader = new ProjectTemplateLoader2([$pathPattern]);
$this->assertEquals([$pathPattern], $loader->getTemplatePathPatterns(), '__construct() takes an array of paths as its second argument');
}
public function testIsAbsolutePath()
{
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('c:\\\\foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('c:/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('\\server\\foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('https://server/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('phar://server/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
}
public function testLoad()
{
$pathPattern = self::$fixturesPath.'/templates/%name%';
$path = self::$fixturesPath.'/templates';
$loader = new ProjectTemplateLoader2($pathPattern);
$storage = $loader->load(new TemplateReference($path.'/foo.php', 'php'));
$this->assertInstanceOf('Symfony\Component\Templating\Storage\FileStorage', $storage, '->load() returns a FileStorage if you pass an absolute path');
$this->assertEquals($path.'/foo.php', (string) $storage, '->load() returns a FileStorage pointing to the passed absolute path');
$this->assertFalse($loader->load(new TemplateReference('bar', 'php')), '->load() returns false if the template is not found');
$storage = $loader->load(new TemplateReference('foo.php', 'php'));
$this->assertInstanceOf('Symfony\Component\Templating\Storage\FileStorage', $storage, '->load() returns a FileStorage if you pass a relative template that exists');
$this->assertEquals($path.'/foo.php', (string) $storage, '->load() returns a FileStorage pointing to the absolute path of the template');
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$logger->expects($this->exactly(2))->method('debug');
$loader = new ProjectTemplateLoader2($pathPattern);
$loader->setLogger($logger);
$this->assertFalse($loader->load(new TemplateReference('foo.xml', 'php')), '->load() returns false if the template does not exist for the given engine');
$loader = new ProjectTemplateLoader2([self::$fixturesPath.'/null/%name%', $pathPattern]);
$loader->setLogger($logger);
$loader->load(new TemplateReference('foo.php', 'php'));
}
}
class ProjectTemplateLoader2 extends FilesystemLoader
{
public function getTemplatePathPatterns()
{
return $this->templatePathPatterns;
}
public static function isAbsolutePath($path)
{
return parent::isAbsolutePath($path);
}
}

View File

@@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Loader\Loader;
use Symfony\Component\Templating\TemplateReferenceInterface;
class LoaderTest extends TestCase
{
public function testGetSetLogger()
{
$loader = new ProjectTemplateLoader4();
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$loader->setLogger($logger);
$this->assertSame($logger, $loader->getLogger(), '->setLogger() sets the logger instance');
}
}
class ProjectTemplateLoader4 extends Loader
{
public function load(TemplateReferenceInterface $template)
{
}
public function getLogger()
{
return $this->logger;
}
public function isFresh(TemplateReferenceInterface $template, $time)
{
return false;
}
}

View File

@@ -0,0 +1,226 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Helper\SlotsHelper;
use Symfony\Component\Templating\Loader\Loader;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\Storage\StringStorage;
use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Component\Templating\TemplateReference;
use Symfony\Component\Templating\TemplateReferenceInterface;
class PhpEngineTest extends TestCase
{
protected $loader;
protected function setUp()
{
$this->loader = new ProjectTemplateLoader();
}
protected function tearDown()
{
$this->loader = null;
}
public function testConstructor()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$this->assertEquals($this->loader, $engine->getLoader(), '__construct() takes a loader instance as its second first argument');
}
public function testOffsetGet()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$engine->set($helper = new \Symfony\Component\Templating\Tests\Fixtures\SimpleHelper('bar'), 'foo');
$this->assertEquals($helper, $engine['foo'], '->offsetGet() returns the value of a helper');
try {
$engine['bar'];
$this->fail('->offsetGet() throws an InvalidArgumentException if the helper is not defined');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->offsetGet() throws an InvalidArgumentException if the helper is not defined');
$this->assertEquals('The helper "bar" is not defined.', $e->getMessage(), '->offsetGet() throws an InvalidArgumentException if the helper is not defined');
}
}
public function testGetSetHas()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$foo = new \Symfony\Component\Templating\Tests\Fixtures\SimpleHelper('foo');
$engine->set($foo);
$this->assertEquals($foo, $engine->get('foo'), '->set() sets a helper');
$engine[$foo] = 'bar';
$this->assertEquals($foo, $engine->get('bar'), '->set() takes an alias as a second argument');
$this->assertArrayHasKey('bar', $engine);
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->assertArrayHasKey('bar', $engine);
$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 testUnsetHelper()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$foo = new \Symfony\Component\Templating\Tests\Fixtures\SimpleHelper('foo');
$engine->set($foo);
$this->expectException('\LogicException');
unset($engine['foo']);
}
public function testExtendRender()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader, []);
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.', $e->getMessage(), '->render() throws an InvalidArgumentException if the template does not exist');
}
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader, [new SlotsHelper()]);
$engine->set(new \Symfony\Component\Templating\Tests\Fixtures\SimpleHelper('bar'));
$this->loader->setTemplate('foo.php', '<?php $this->extend("layout.php"); echo $this[\'foo\'].$foo ?>');
$this->loader->setTemplate('layout.php', '-<?php echo $this[\'slots\']->get("_content") ?>-');
$this->assertEquals('-barfoo-', $engine->render('foo.php', ['foo' => 'foo']), '->render() uses the decorator to decorate the template');
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader, [new SlotsHelper()]);
$engine->set(new \Symfony\Component\Templating\Tests\Fixtures\SimpleHelper('bar'));
$this->loader->setTemplate('bar.php', 'bar');
$this->loader->setTemplate('foo.php', '<?php $this->extend("layout.php"); echo $foo ?>');
$this->loader->setTemplate('layout.php', '<?php echo $this->render("bar.php") ?>-<?php echo $this[\'slots\']->get("_content") ?>-');
$this->assertEquals('bar-foo-', $engine->render('foo.php', ['foo' => 'foo', 'bar' => 'bar']), '->render() supports render() calls in templates');
}
public function testRenderParameter()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$this->loader->setTemplate('foo.php', '<?php echo $template . $parameters ?>');
$this->assertEquals('foobar', $engine->render('foo.php', ['template' => 'foo', 'parameters' => 'bar']), '->render() extract variables');
}
/**
* @dataProvider forbiddenParameterNames
*/
public function testRenderForbiddenParameter($name)
{
$this->expectException('InvalidArgumentException');
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$this->loader->setTemplate('foo.php', 'bar');
$engine->render('foo.php', [$name => 'foo']);
}
public function forbiddenParameterNames()
{
return [
['this'],
['view'],
];
}
public function testEscape()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$this->assertEquals('&lt;br /&gt;', $engine->escape('<br />'), '->escape() escapes strings');
$foo = new \stdClass();
$this->assertEquals($foo, $engine->escape($foo), '->escape() does nothing on non strings');
}
public function testGetSetCharset()
{
$helper = new SlotsHelper();
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader, [$helper]);
$this->assertEquals('UTF-8', $engine->getCharset(), 'EngineInterface::getCharset() returns UTF-8 by default');
$this->assertEquals('UTF-8', $helper->getCharset(), 'HelperInterface::getCharset() returns UTF-8 by default');
$engine->setCharset('ISO-8859-1');
$this->assertEquals('ISO-8859-1', $engine->getCharset(), 'EngineInterface::setCharset() changes the default charset to use');
$this->assertEquals('ISO-8859-1', $helper->getCharset(), 'EngineInterface::setCharset() changes the default charset of helper');
}
public function testGlobalVariables()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$engine->addGlobal('global_variable', 'lorem ipsum');
$this->assertEquals([
'global_variable' => 'lorem ipsum',
], $engine->getGlobals());
}
public function testGlobalsGetPassedToTemplate()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$engine->addGlobal('global', 'global variable');
$this->loader->setTemplate('global.php', '<?php echo $global; ?>');
$this->assertEquals('global variable', $engine->render('global.php'));
$this->assertEquals('overwritten', $engine->render('global.php', ['global' => 'overwritten']));
}
public function testGetLoader()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$this->assertSame($this->loader, $engine->getLoader());
}
}
class ProjectTemplateEngine extends PhpEngine
{
public function getLoader()
{
return $this->loader;
}
}
class ProjectTemplateLoader extends Loader
{
public $templates = [];
public function setTemplate($name, $content)
{
$template = new TemplateReference($name, 'php');
$this->templates[$template->getLogicalName()] = $content;
}
public function load(TemplateReferenceInterface $template)
{
if (isset($this->templates[$template->getLogicalName()])) {
return new StringStorage($this->templates[$template->getLogicalName()]);
}
return false;
}
public function isFresh(TemplateReferenceInterface $template, $time)
{
return false;
}
}

View File

@@ -0,0 +1,26 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Storage;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Storage\FileStorage;
class FileStorageTest extends TestCase
{
public function testGetContent()
{
$storage = new FileStorage('foo');
$this->assertInstanceOf('Symfony\Component\Templating\Storage\Storage', $storage, 'FileStorage is an instance of Storage');
$storage = new FileStorage(__DIR__.'/../Fixtures/templates/foo.php');
$this->assertEquals('<?php echo $foo ?>'."\n", $storage->getContent(), '->getContent() returns the content of the template');
}
}

View File

@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Storage;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Storage\Storage;
class StorageTest extends TestCase
{
public function testMagicToString()
{
$storage = new TestStorage('foo');
$this->assertEquals('foo', (string) $storage, '__toString() returns the template name');
}
}
class TestStorage extends Storage
{
public function getContent()
{
}
}

View File

@@ -0,0 +1,26 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Storage;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Storage\StringStorage;
class StringStorageTest extends TestCase
{
public function testGetContent()
{
$storage = new StringStorage('foo');
$this->assertInstanceOf('Symfony\Component\Templating\Storage\Storage', $storage, 'StringStorage is an instance of Storage');
$storage = new StringStorage('foo');
$this->assertEquals('foo', $storage->getContent(), '->getContent() returns the content of the template');
}
}

View File

@@ -0,0 +1,51 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Component\Templating\TemplateReference;
class TemplateNameParserTest extends TestCase
{
protected $parser;
protected function setUp()
{
$this->parser = new TemplateNameParser();
}
protected function tearDown()
{
$this->parser = null;
}
/**
* @dataProvider getLogicalNameToTemplateProvider
*/
public function testParse($name, $ref)
{
$template = $this->parser->parse($name);
$this->assertEquals($template->getLogicalName(), $ref->getLogicalName());
$this->assertEquals($template->getLogicalName(), $name);
}
public function getLogicalNameToTemplateProvider()
{
return [
['/path/to/section/name.engine', new TemplateReference('/path/to/section/name.engine', 'engine')],
['name.engine', new TemplateReference('name.engine', 'engine')],
['name', new TemplateReference('name')],
];
}
}