Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
<?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\Bundle\TwigBundle\Tests;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\ContainerAwareRuntimeLoader;
class ContainerAwareRuntimeLoaderTest extends TestCase
{
public function testLoad()
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$container->expects($this->once())->method('get')->with('foo');
$loader = new ContainerAwareRuntimeLoader($container, array(
'FooClass' => 'foo',
));
$loader->load('FooClass');
}
public function testLoadWithoutAMatch()
{
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$logger->expects($this->once())->method('warning')->with('Class "BarClass" is not configured as a Twig runtime. Add the "twig.runtime" tag to the related service in the container.');
$loader = new ContainerAwareRuntimeLoader($this->getMockBuilder(ContainerInterface::class)->getMock(), array(), $logger);
$this->assertNull($loader->load('BarClass'));
}
}

View File

@@ -0,0 +1,92 @@
<?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\Bundle\TwigBundle\Tests\Controller;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
class ExceptionControllerTest extends TestCase
{
public function testShowActionCanBeForcedToShowErrorPage()
{
$twig = $this->createTwigEnv(array('@Twig/Exception/error404.html.twig' => '<html>not found</html>'));
$request = $this->createRequest('html');
$request->attributes->set('showException', false);
$exception = FlattenException::create(new \Exception(), 404);
$controller = new ExceptionController($twig, /* "showException" defaults to --> */ true);
$response = $controller->showAction($request, $exception, null);
$this->assertEquals(200, $response->getStatusCode()); // successful request
$this->assertEquals('<html>not found</html>', $response->getContent());
}
public function testFallbackToHtmlIfNoTemplateForRequestedFormat()
{
$twig = $this->createTwigEnv(array('@Twig/Exception/error.html.twig' => '<html></html>'));
$request = $this->createRequest('txt');
$exception = FlattenException::create(new \Exception());
$controller = new ExceptionController($twig, false);
$controller->showAction($request, $exception);
$this->assertEquals('html', $request->getRequestFormat());
}
public function testFallbackToHtmlWithFullExceptionIfNoTemplateForRequestedFormatAndExceptionsShouldBeShown()
{
$twig = $this->createTwigEnv(array('@Twig/Exception/exception_full.html.twig' => '<html></html>'));
$request = $this->createRequest('txt');
$request->attributes->set('showException', true);
$exception = FlattenException::create(new \Exception());
$controller = new ExceptionController($twig, false);
$controller->showAction($request, $exception);
$this->assertEquals('html', $request->getRequestFormat());
}
public function testResponseHasRequestedMimeType()
{
$twig = $this->createTwigEnv(array('@Twig/Exception/error.json.twig' => '{}'));
$request = $this->createRequest('json');
$exception = FlattenException::create(new \Exception());
$controller = new ExceptionController($twig, false);
$response = $controller->showAction($request, $exception);
$this->assertEquals('json', $request->getRequestFormat());
$this->assertEquals($request->getMimeType('json'), $response->headers->get('Content-Type'));
}
private function createRequest($requestFormat)
{
$request = Request::create('whatever');
$request->headers->set('X-Php-Ob-Level', 1);
$request->setRequestFormat($requestFormat);
return $request;
}
private function createTwigEnv(array $templates)
{
return new Environment(new ArrayLoader($templates));
}
}

View File

@@ -0,0 +1,53 @@
<?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\Bundle\TwigBundle\Tests\Controller;
use Symfony\Bundle\TwigBundle\Controller\PreviewErrorController;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class PreviewErrorControllerTest extends TestCase
{
public function testForwardRequestToConfiguredController()
{
$request = Request::create('whatever');
$response = new Response('');
$code = 123;
$logicalControllerName = 'foo:bar:baz';
$kernel = $this->getMockBuilder('\Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
$kernel
->expects($this->once())
->method('handle')
->with(
$this->callback(function (Request $request) use ($logicalControllerName, $code) {
$this->assertEquals($logicalControllerName, $request->attributes->get('_controller'));
$exception = $request->attributes->get('exception');
$this->assertInstanceOf(FlattenException::class, $exception);
$this->assertEquals($code, $exception->getStatusCode());
$this->assertFalse($request->attributes->get('showException'));
return true;
}),
$this->equalTo(HttpKernelInterface::SUB_REQUEST)
)
->will($this->returnValue($response));
$controller = new PreviewErrorController($kernel, $logicalControllerName);
$this->assertSame($response, $controller->previewErrorPageAction($request, $code));
}
}

View File

@@ -0,0 +1,47 @@
<?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\Bundle\TwigBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExtensionPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
class ExtensionPassTest extends TestCase
{
public function testProcessDoesNotDropExistingFileLoaderMethodCalls()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.root_dir', __DIR__);
$container->register('twig.app_variable', '\Symfony\Bridge\Twig\AppVariable');
$container->register('templating', '\Symfony\Bundle\TwigBundle\TwigEngine');
$container->register('twig.extension.yaml');
$container->register('twig.extension.debug.stopwatch');
$container->register('twig.extension.expression');
$nativeTwigLoader = new Definition('\Twig\Loader\FilesystemLoader');
$nativeTwigLoader->addMethodCall('addPath', array());
$container->setDefinition('twig.loader.native_filesystem', $nativeTwigLoader);
$filesystemLoader = new Definition('\Symfony\Bundle\TwigBundle\Loader\FilesystemLoader');
$filesystemLoader->setArguments(array(null, null, null));
$filesystemLoader->addMethodCall('addPath', array());
$container->setDefinition('twig.loader.filesystem', $filesystemLoader);
$extensionPass = new ExtensionPass();
$extensionPass->process($container);
$this->assertCount(2, $filesystemLoader->getMethodCalls());
}
}

View File

@@ -0,0 +1,151 @@
<?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\Bundle\TwigBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigLoaderPass;
class TwigLoaderPassTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $builder;
/**
* @var Definition
*/
private $chainLoader;
/**
* @var TwigLoaderPass
*/
private $pass;
protected function setUp()
{
$this->builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'setAlias', 'getDefinition'))->getMock();
$this->chainLoader = new Definition('loader');
$this->pass = new TwigLoaderPass();
}
public function testMapperPassWithOneTaggedLoaders()
{
$serviceIds = array(
'test_loader_1' => array(
array(),
),
);
$this->builder->expects($this->once())
->method('hasDefinition')
->with('twig')
->will($this->returnValue(true));
$this->builder->expects($this->once())
->method('findTaggedServiceIds')
->with('twig.loader')
->will($this->returnValue($serviceIds));
$this->builder->expects($this->once())
->method('setAlias')
->with('twig.loader', 'test_loader_1');
$this->pass->process($this->builder);
}
public function testMapperPassWithTwoTaggedLoaders()
{
$serviceIds = array(
'test_loader_1' => array(
array(),
),
'test_loader_2' => array(
array(),
),
);
$this->builder->expects($this->once())
->method('hasDefinition')
->with('twig')
->will($this->returnValue(true));
$this->builder->expects($this->once())
->method('findTaggedServiceIds')
->with('twig.loader')
->will($this->returnValue($serviceIds));
$this->builder->expects($this->once())
->method('getDefinition')
->with('twig.loader.chain')
->will($this->returnValue($this->chainLoader));
$this->builder->expects($this->once())
->method('setAlias')
->with('twig.loader', 'twig.loader.chain');
$this->pass->process($this->builder);
$calls = $this->chainLoader->getMethodCalls();
$this->assertCount(2, $calls);
$this->assertEquals('addLoader', $calls[0][0]);
$this->assertEquals('addLoader', $calls[1][0]);
$this->assertEquals('test_loader_1', (string) $calls[0][1][0]);
$this->assertEquals('test_loader_2', (string) $calls[1][1][0]);
}
public function testMapperPassWithTwoTaggedLoadersWithPriority()
{
$serviceIds = array(
'test_loader_1' => array(
array('priority' => 100),
),
'test_loader_2' => array(
array('priority' => 200),
),
);
$this->builder->expects($this->once())
->method('hasDefinition')
->with('twig')
->will($this->returnValue(true));
$this->builder->expects($this->once())
->method('findTaggedServiceIds')
->with('twig.loader')
->will($this->returnValue($serviceIds));
$this->builder->expects($this->once())
->method('getDefinition')
->with('twig.loader.chain')
->will($this->returnValue($this->chainLoader));
$this->builder->expects($this->once())
->method('setAlias')
->with('twig.loader', 'twig.loader.chain');
$this->pass->process($this->builder);
$calls = $this->chainLoader->getMethodCalls();
$this->assertCount(2, $calls);
$this->assertEquals('addLoader', $calls[0][0]);
$this->assertEquals('addLoader', $calls[1][0]);
$this->assertEquals('test_loader_2', (string) $calls[0][1][0]);
$this->assertEquals('test_loader_1', (string) $calls[1][1][0]);
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
*/
public function testMapperPassWithZeroTaggedLoaders()
{
$this->builder->expects($this->once())
->method('hasDefinition')
->with('twig')
->will($this->returnValue(true));
$this->builder->expects($this->once())
->method('findTaggedServiceIds')
->with('twig.loader')
->will($this->returnValue(array()));
$this->pass->process($this->builder);
}
}

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\Bundle\TwigBundle\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\TwigBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;
class ConfigurationTest extends TestCase
{
public function testDoNoDuplicateDefaultFormResources()
{
$input = array(
'form_themes' => array('form_div_layout.html.twig'),
);
$processor = new Processor();
$config = $processor->processConfiguration(new Configuration(), array($input));
$this->assertEquals(array('form_div_layout.html.twig'), $config['form_themes']);
}
}

View File

@@ -0,0 +1 @@
This is a layout

View File

@@ -0,0 +1,6 @@
<?php
$container->loadFromExtension('twig', array(
'autoescape_service' => 'my_project.some_bundle.template_escaping_guesser',
'autoescape_service_method' => 'guess',
));

View File

@@ -0,0 +1,3 @@
<?php
$container->loadFromExtension('twig', array());

View File

@@ -0,0 +1,7 @@
<?php
$container->loadFromExtension('twig', array(
'paths' => array(
'namespaced_path3' => 'namespace3',
),
));

View File

@@ -0,0 +1,14 @@
<?php
$container->loadFromExtension('twig', array(
'date' => array(
'format' => 'Y-m-d',
'interval_format' => '%d',
'timezone' => 'Europe/Berlin',
),
'number_format' => array(
'decimals' => 2,
'decimal_point' => ',',
'thousands_separator' => '.',
),
));

View File

@@ -0,0 +1,26 @@
<?php
$container->loadFromExtension('twig', array(
'form_themes' => array(
'MyBundle::form.html.twig',
),
'globals' => array(
'foo' => '@bar',
'baz' => '@@qux',
'pi' => 3.14,
'bad' => array('key' => 'foo'),
),
'auto_reload' => true,
'autoescape' => true,
'base_template_class' => 'stdClass',
'cache' => '/tmp',
'charset' => 'ISO-8859-1',
'debug' => true,
'strict_variables' => true,
'paths' => array(
'path1',
'path2',
'namespaced_path1' => 'namespace1',
'namespaced_path2' => 'namespace2',
),
));

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:twig="http://symfony.com/schema/dic/twig"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
<twig:config autoescape-service="my_project.some_bundle.template_escaping_guesser" autoescape-service-method="guess" />
</container>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:twig="http://symfony.com/schema/dic/twig"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
<twig:config />
</container>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:twig="http://symfony.com/schema/dic/twig"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
<twig:config auto-reload="true" autoescape="true" base-template-class="stdClass" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true">
<twig:path namespace="namespace3">namespaced_path3</twig:path>
</twig:config>
</container>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:twig="http://symfony.com/schema/dic/twig"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
<twig:config>
<twig:date format="Y-m-d" interval-format="%d" timezone="Europe/Berlin" />
<twig:number-format decimals="2" decimal-point="," thousands-separator="." />
</twig:config>
</container>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:twig="http://symfony.com/schema/dic/twig"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
<twig:config auto-reload="true" autoescape="true" base-template-class="stdClass" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true">
<twig:form-theme>MyBundle::form.html.twig</twig:form-theme>
<twig:global key="foo" id="bar" type="service" />
<twig:global key="baz">@@qux</twig:global>
<twig:global key="pi">3.14</twig:global>
<twig:path>path1</twig:path>
<twig:path>path2</twig:path>
<twig:path namespace="namespace1">namespaced_path1</twig:path>
<twig:path namespace="namespace2">namespaced_path2</twig:path>
</twig:config>
</container>

View File

@@ -0,0 +1,3 @@
twig:
autoescape_service: my_project.some_bundle.template_escaping_guesser
autoescape_service_method: guess

View File

@@ -0,0 +1 @@
twig:

View File

@@ -0,0 +1,3 @@
twig:
paths:
namespaced_path3: namespace3

View File

@@ -0,0 +1,9 @@
twig:
date:
format: Y-m-d
interval_format: '%d'
timezone: Europe/Berlin
number_format:
decimals: 2
decimal_point: ','
thousands_separator: .

View File

@@ -0,0 +1,20 @@
twig:
form_themes:
- MyBundle::form.html.twig
globals:
foo: "@bar"
baz: "@@qux"
pi: 3.14
bad: {key: foo}
auto_reload: true
autoescape: true
base_template_class: stdClass
cache: /tmp
charset: ISO-8859-1
debug: true
strict_variables: true
paths:
path1: ''
path2: ''
namespaced_path1: namespace1
namespaced_path2: namespace2

View File

@@ -0,0 +1,348 @@
<?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\Bundle\TwigBundle\Tests\DependencyInjection;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\RuntimeLoaderPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\TwigExtension;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
class TwigExtensionTest extends TestCase
{
public function testLoadEmptyConfiguration()
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$container->loadFromExtension('twig', array());
$this->compileContainer($container);
$this->assertEquals('Twig\Environment', $container->getDefinition('twig')->getClass(), '->load() loads the twig.xml file');
$this->assertContains('form_div_layout.html.twig', $container->getParameter('twig.form.resources'), '->load() includes default template for form resources');
// Twig options
$options = $container->getDefinition('twig')->getArgument(1);
$this->assertEquals('%kernel.cache_dir%/twig', $options['cache'], '->load() sets default value for cache option');
$this->assertEquals('%kernel.charset%', $options['charset'], '->load() sets default value for charset option');
$this->assertEquals('%kernel.debug%', $options['debug'], '->load() sets default value for debug option');
}
/**
* @dataProvider getFormats
*/
public function testLoadFullConfiguration($format)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'full', $format);
$this->compileContainer($container);
$this->assertEquals('Twig\Environment', $container->getDefinition('twig')->getClass(), '->load() loads the twig.xml file');
// Form resources
$resources = $container->getParameter('twig.form.resources');
$this->assertContains('form_div_layout.html.twig', $resources, '->load() includes default template for form resources');
$this->assertContains('MyBundle::form.html.twig', $resources, '->load() merges new templates into form resources');
// Globals
$calls = $container->getDefinition('twig')->getMethodCalls();
$this->assertEquals('app', $calls[0][1][0], '->load() registers services as Twig globals');
$this->assertEquals(new Reference('twig.app_variable'), $calls[0][1][1]);
$this->assertEquals('foo', $calls[2][1][0], '->load() registers services as Twig globals');
$this->assertEquals(new Reference('bar'), $calls[2][1][1], '->load() registers services as Twig globals');
$this->assertEquals('baz', $calls[3][1][0], '->load() registers variables as Twig globals');
$this->assertEquals('@qux', $calls[3][1][1], '->load() allows escaping of service identifiers');
$this->assertEquals('pi', $calls[4][1][0], '->load() registers variables as Twig globals');
$this->assertEquals(3.14, $calls[4][1][1], '->load() registers variables as Twig globals');
// Yaml and Php specific configs
if (in_array($format, array('yml', 'php'))) {
$this->assertEquals('bad', $calls[5][1][0], '->load() registers variables as Twig globals');
$this->assertEquals(array('key' => 'foo'), $calls[5][1][1], '->load() registers variables as Twig globals');
}
// Twig options
$options = $container->getDefinition('twig')->getArgument(1);
$this->assertTrue($options['auto_reload'], '->load() sets the auto_reload option');
$this->assertTrue($options['autoescape'], '->load() sets the autoescape option');
$this->assertEquals('stdClass', $options['base_template_class'], '->load() sets the base_template_class option');
$this->assertEquals('/tmp', $options['cache'], '->load() sets the cache option');
$this->assertEquals('ISO-8859-1', $options['charset'], '->load() sets the charset option');
$this->assertTrue($options['debug'], '->load() sets the debug option');
$this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
}
/**
* @dataProvider getFormats
*/
public function testLoadCustomTemplateEscapingGuesserConfiguration($format)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'customTemplateEscapingGuesser', $format);
$this->compileContainer($container);
$options = $container->getDefinition('twig')->getArgument(1);
$this->assertEquals(array(new Reference('my_project.some_bundle.template_escaping_guesser'), 'guess'), $options['autoescape']);
}
/**
* @dataProvider getFormats
*/
public function testLoadDefaultTemplateEscapingGuesserConfiguration($format)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'empty', $format);
$this->compileContainer($container);
$options = $container->getDefinition('twig')->getArgument(1);
$this->assertEquals('name', $options['autoescape']);
}
/**
* @dataProvider getFormats
*/
public function testLoadCustomDateFormats($fileFormat)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'formats', $fileFormat);
$this->compileContainer($container);
$environmentConfigurator = $container->getDefinition('twig.configurator.environment');
$this->assertSame('Y-m-d', $environmentConfigurator->getArgument(0));
$this->assertSame('%d', $environmentConfigurator->getArgument(1));
$this->assertSame('Europe/Berlin', $environmentConfigurator->getArgument(2));
$this->assertSame(2, $environmentConfigurator->getArgument(3));
$this->assertSame(',', $environmentConfigurator->getArgument(4));
$this->assertSame('.', $environmentConfigurator->getArgument(5));
}
public function testGlobalsWithDifferentTypesAndValues()
{
$globals = array(
'array' => array(),
'false' => false,
'float' => 2.0,
'integer' => 3,
'null' => null,
'object' => new \stdClass(),
'string' => 'foo',
'true' => true,
);
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$container->loadFromExtension('twig', array('globals' => $globals));
$this->compileContainer($container);
$calls = $container->getDefinition('twig')->getMethodCalls();
foreach (array_slice($calls, 2) as $call) {
$this->assertEquals(key($globals), $call[1][0]);
$this->assertSame(current($globals), $call[1][1]);
next($globals);
}
}
/**
* @dataProvider getFormats
*/
public function testTwigLoaderPaths($format)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'full', $format);
$this->loadFromFile($container, 'extra', $format);
$this->compileContainer($container);
$def = $container->getDefinition('twig.loader.native_filesystem');
$paths = array();
foreach ($def->getMethodCalls() as $call) {
if ('addPath' === $call[0] && false === strpos($call[1][0], 'Form')) {
$paths[] = $call[1];
}
}
$this->assertEquals(array(
array('path1'),
array('path2'),
array('namespaced_path1', 'namespace1'),
array('namespaced_path2', 'namespace2'),
array('namespaced_path3', 'namespace3'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildChildChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'Twig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'Twig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'Twig'),
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'Twig'),
array(__DIR__.'/Fixtures/Resources/TwigBundle/views', 'Twig'),
array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'ChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
array(__DIR__.'/Fixtures/Resources/views'),
), $paths);
}
public function getFormats()
{
return array(
array('php'),
array('yml'),
array('xml'),
);
}
/**
* @dataProvider stopwatchExtensionAvailabilityProvider
*/
public function testStopwatchExtensionAvailability($debug, $stopwatchEnabled, $expected)
{
$container = $this->createContainer();
$container->setParameter('kernel.debug', $debug);
if ($stopwatchEnabled) {
$container->register('debug.stopwatch', 'Symfony\Component\Stopwatch\Stopwatch');
}
$container->registerExtension(new TwigExtension());
$container->loadFromExtension('twig', array());
$this->compileContainer($container);
$tokenParsers = $container->get('twig.extension.debug.stopwatch')->getTokenParsers();
$stopwatchIsAvailable = new \ReflectionProperty($tokenParsers[0], 'stopwatchIsAvailable');
$stopwatchIsAvailable->setAccessible(true);
$this->assertSame($expected, $stopwatchIsAvailable->getValue($tokenParsers[0]));
}
public function stopwatchExtensionAvailabilityProvider()
{
return array(
'debug-and-stopwatch-enabled' => array(true, true, true),
'only-stopwatch-enabled' => array(false, true, false),
'only-debug-enabled' => array(true, false, false),
'debug-and-stopwatch-disabled' => array(false, false, false),
);
}
public function testRuntimeLoader()
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$container->loadFromExtension('twig', array());
$container->setParameter('kernel.environment', 'test');
$container->setParameter('debug.file_link_format', 'test');
$container->setParameter('foo', 'FooClass');
$container->register('http_kernel', 'FooClass');
$container->register('templating.locator', 'FooClass');
$container->register('templating.name_parser', 'FooClass');
$container->register('foo', '%foo%')->addTag('twig.runtime');
$container->addCompilerPass(new RuntimeLoaderPass(), PassConfig::TYPE_BEFORE_REMOVING);
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->compile();
$loader = $container->getDefinition('twig.runtime_loader');
$args = $loader->getArgument(1);
$this->assertArrayHasKey('Symfony\Bridge\Twig\Form\TwigRenderer', $args);
$this->assertArrayHasKey('FooClass', $args);
$this->assertContains('twig.form.renderer', $args);
$this->assertContains('foo', $args);
}
private function createContainer()
{
$container = new ContainerBuilder(new ParameterBag(array(
'kernel.cache_dir' => __DIR__,
'kernel.root_dir' => __DIR__.'/Fixtures',
'kernel.charset' => 'UTF-8',
'kernel.debug' => false,
'kernel.bundles' => array(
'TwigBundle' => 'Symfony\\Bundle\\TwigBundle\\TwigBundle',
'ChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildTwigBundle\\ChildTwigBundle',
'ChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildTwigBundle\\ChildChildTwigBundle',
'ChildChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildTwigBundle\\ChildChildChildTwigBundle',
'ChildChildChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildChildTwigBundle\\ChildChildChildChildTwigBundle',
),
'kernel.bundles_metadata' => array(
'ChildChildChildChildTwigBundle' => array(
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildChildTwigBundle\\ChildChildChildChildTwigBundle',
'parent' => 'ChildChildChildTwigBundle',
'path' => __DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle',
),
'TwigBundle' => array(
'namespace' => 'Symfony\\Bundle\\TwigBundle',
'parent' => null,
'path' => realpath(__DIR__.'/../..'),
),
'ChildTwigBundle' => array(
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildTwigBundle\\ChildTwigBundle',
'parent' => 'TwigBundle',
'path' => __DIR__.'/Fixtures/Bundle/ChildTwigBundle',
),
'ChildChildChildTwigBundle' => array(
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildTwigBundle\\ChildChildChildTwigBundle',
'parent' => 'ChildChildTwigBundle',
'path' => __DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle',
),
'ChildChildTwigBundle' => array(
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildTwigBundle\\ChildChildTwigBundle',
'parent' => 'ChildTwigBundle',
'path' => __DIR__.'/Fixtures/Bundle/ChildChildTwigBundle',
),
),
)));
return $container;
}
private function compileContainer(ContainerBuilder $container)
{
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->compile();
}
private function loadFromFile(ContainerBuilder $container, $file, $format)
{
$locator = new FileLocator(__DIR__.'/Fixtures/'.$format);
switch ($format) {
case 'php':
$loader = new PhpFileLoader($container, $locator);
break;
case 'xml':
$loader = new XmlFileLoader($container, $locator);
break;
case 'yml':
$loader = new YamlFileLoader($container, $locator);
break;
default:
throw new \InvalidArgumentException(sprintf('Unsupported format: %s', $format));
}
$loader->load($file.'.'.$format);
}
}

View File

@@ -0,0 +1 @@
{# Twig template #}

View File

@@ -0,0 +1 @@
{# Twig template #}

View File

@@ -0,0 +1 @@
{# Twig template #}

View File

@@ -0,0 +1 @@
{# Twig template #}

View File

@@ -0,0 +1,118 @@
<?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\Bundle\TwigBundle\Tests;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\TwigBundle\TwigBundle;
class CacheWarmingTest extends TestCase
{
public function testCacheIsProperlyWarmedWhenTemplatingIsAvailable()
{
$kernel = new CacheWarmingKernel(true);
$kernel->boot();
$warmer = $kernel->getContainer()->get('cache_warmer');
$warmer->enableOptionalWarmers();
$warmer->warmUp($kernel->getCacheDir());
$this->assertFileExists($kernel->getCacheDir().'/twig');
}
public function testCacheIsProperlyWarmedWhenTemplatingIsDisabled()
{
$kernel = new CacheWarmingKernel(false);
$kernel->boot();
$warmer = $kernel->getContainer()->get('cache_warmer');
$warmer->enableOptionalWarmers();
$warmer->warmUp($kernel->getCacheDir());
$this->assertFileExists($kernel->getCacheDir().'/twig');
}
protected function setUp()
{
$this->deleteTempDir();
}
protected function tearDown()
{
$this->deleteTempDir();
}
private function deleteTempDir()
{
if (!file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION.'/CacheWarmingKernel')) {
return;
}
$fs = new Filesystem();
$fs->remove($dir);
}
}
class CacheWarmingKernel extends Kernel
{
private $withTemplating;
public function __construct($withTemplating)
{
$this->withTemplating = $withTemplating;
parent::__construct(($withTemplating ? 'with' : 'without').'_templating', true);
}
public function getName()
{
return 'CacheWarming';
}
public function registerBundles()
{
return array(new FrameworkBundle(), new TwigBundle());
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function ($container) {
$container->loadFromExtension('framework', array(
'secret' => '$ecret',
'form' => array('enabled' => false),
));
});
if ($this->withTemplating) {
$loader->load(function ($container) {
$container->loadFromExtension('framework', array(
'secret' => '$ecret',
'templating' => array('engines' => array('twig')),
'router' => array('resource' => '%kernel.root_dir%/Resources/config/empty_routing.yml'),
'form' => array('enabled' => false),
));
});
}
}
public function getCacheDir()
{
return sys_get_temp_dir().'/'.Kernel::VERSION.'/CacheWarmingKernel/cache/'.$this->environment;
}
public function getLogDir()
{
return sys_get_temp_dir().'/'.Kernel::VERSION.'/CacheWarmingKernel/logs';
}
}

View File

@@ -0,0 +1,79 @@
<?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\Bundle\TwigBundle\Tests;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\TwigBundle\TwigBundle;
class NoTemplatingEntryTest extends TestCase
{
public function test()
{
$kernel = new NoTemplatingEntryKernel('dev', true);
$kernel->boot();
$container = $kernel->getContainer();
$content = $container->get('twig')->render('index.html.twig');
$this->assertContains('{ a: b }', $content);
}
protected function setUp()
{
$this->deleteTempDir();
}
protected function tearDown()
{
$this->deleteTempDir();
}
protected function deleteTempDir()
{
if (!file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION.'/NoTemplatingEntryKernel')) {
return;
}
$fs = new Filesystem();
$fs->remove($dir);
}
}
class NoTemplatingEntryKernel extends Kernel
{
public function registerBundles()
{
return array(new FrameworkBundle(), new TwigBundle());
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function ($container) {
$container->loadFromExtension('framework', array(
'secret' => '$ecret',
'form' => array('enabled' => false),
));
});
}
public function getCacheDir()
{
return sys_get_temp_dir().'/'.Kernel::VERSION.'/NoTemplatingEntryKernel/cache/'.$this->environment;
}
public function getLogDir()
{
return sys_get_temp_dir().'/'.Kernel::VERSION.'/NoTemplatingEntryKernel/logs';
}
}

View File

@@ -0,0 +1 @@
{{ {a: 'b'}|yaml_encode }}

View File

@@ -0,0 +1,131 @@
<?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\Bundle\TwigBundle\Tests\Loader;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Bundle\TwigBundle\Loader\FilesystemLoader;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
class FilesystemLoaderTest extends TestCase
{
public function testGetSourceContext()
{
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock();
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
$locator
->expects($this->once())
->method('locate')
->will($this->returnValue(__DIR__.'/../DependencyInjection/Fixtures/Resources/views/layout.html.twig'))
;
$loader = new FilesystemLoader($locator, $parser);
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views', 'namespace');
// Twig-style
$this->assertEquals("This is a layout\n", $loader->getSourceContext('@namespace/layout.html.twig')->getCode());
// Symfony-style
$this->assertEquals("This is a layout\n", $loader->getSourceContext('TwigBundle::layout.html.twig')->getCode());
}
public function testExists()
{
// should return true for templates that Twig does not find, but Symfony does
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock();
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
$locator
->expects($this->once())
->method('locate')
->will($this->returnValue($template = __DIR__.'/../DependencyInjection/Fixtures/Resources/views/layout.html.twig'))
;
$loader = new FilesystemLoader($locator, $parser);
$this->assertTrue($loader->exists($template));
}
/**
* @expectedException \Twig\Error\LoaderError
*/
public function testTwigErrorIfLocatorThrowsInvalid()
{
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock();
$parser
->expects($this->once())
->method('parse')
->with('name.format.engine')
->will($this->returnValue(new TemplateReference('', '', 'name', 'format', 'engine')))
;
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
$locator
->expects($this->once())
->method('locate')
->will($this->throwException(new \InvalidArgumentException('Unable to find template "NonExistent".')))
;
$loader = new FilesystemLoader($locator, $parser);
$loader->getCacheKey('name.format.engine');
}
/**
* @expectedException \Twig\Error\LoaderError
*/
public function testTwigErrorIfLocatorReturnsFalse()
{
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock();
$parser
->expects($this->once())
->method('parse')
->with('name.format.engine')
->will($this->returnValue(new TemplateReference('', '', 'name', 'format', 'engine')))
;
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
$locator
->expects($this->once())
->method('locate')
->will($this->returnValue(false))
;
$loader = new FilesystemLoader($locator, $parser);
$loader->getCacheKey('name.format.engine');
}
/**
* @expectedException \Twig\Error\LoaderError
* @expectedExceptionMessageRegExp /Unable to find template "name\.format\.engine" \(looked into: .*Tests.Loader.\.\..DependencyInjection.Fixtures.Resources.views\)/
*/
public function testTwigErrorIfTemplateDoesNotExist()
{
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock();
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
$loader = new FilesystemLoader($locator, $parser);
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views');
$method = new \ReflectionMethod('Symfony\Bundle\TwigBundle\Loader\FilesystemLoader', 'findTemplate');
$method->setAccessible(true);
$method->invoke($loader, 'name.format.engine');
}
public function testTwigSoftErrorIfTemplateDoesNotExist()
{
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock();
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
$loader = new FilesystemLoader($locator, $parser);
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views');
$method = new \ReflectionMethod('Symfony\Bundle\TwigBundle\Loader\FilesystemLoader', 'findTemplate');
$method->setAccessible(true);
$this->assertFalse($method->invoke($loader, 'name.format.engine', false));
}
}

View File

@@ -0,0 +1,42 @@
<?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\Bundle\TwigBundle\Tests;
use Symfony\Bundle\TwigBundle\TemplateIterator;
class TemplateIteratorTest extends TestCase
{
public function testGetIterator()
{
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
$bundle->expects($this->any())->method('getName')->will($this->returnValue('BarBundle'));
$bundle->expects($this->any())->method('getPath')->will($this->returnValue(__DIR__.'/Fixtures/templates/BarBundle'));
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Kernel')->disableOriginalConstructor()->getMock();
$kernel->expects($this->any())->method('getBundles')->will($this->returnValue(array(
$bundle,
)));
$iterator = new TemplateIterator($kernel, __DIR__.'/Fixtures/templates', array(__DIR__.'/Fixtures/templates/Foo' => 'Foo'));
$sorted = iterator_to_array($iterator);
sort($sorted);
$this->assertEquals(
array(
'@Bar/index.html.twig',
'@Foo/index.html.twig',
'layout.html.twig',
'sub/sub.html.twig',
),
$sorted
);
}
}

View File

@@ -0,0 +1,18 @@
<?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\Bundle\TwigBundle\Tests;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
class TestCase extends PHPUnitTestCase
{
}