twig implementation

This commit is contained in:
Nicolas Lœuillet
2013-08-03 19:26:54 +02:00
parent 2b840e0cfb
commit 4f5b44bd3b
1418 changed files with 108207 additions and 1586 deletions

View File

@ -0,0 +1,69 @@
<?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\Bridge\Twig\Tests\Extension;
use Symfony\Bridge\Twig\Extension\CodeExtension;
class CodeExtensionTest extends \PHPUnit_Framework_TestCase
{
protected $helper;
public function testFormatFile()
{
$expected = sprintf('<a href="txmt://open?url=file://%s&amp;line=25" title="Click to open this file" class="file_link">%s at line 25</a>', __FILE__, __FILE__);
$this->assertEquals($expected, $this->getExtension()->formatFile(__FILE__, 25));
}
/**
* @dataProvider getClassNameProvider
*/
public function testGettingClassAbbreviation($class, $abbr)
{
$this->assertEquals($this->getExtension()->abbrClass($class), $abbr);
}
/**
* @dataProvider getMethodNameProvider
*/
public function testGettingMethodAbbreviation($method, $abbr)
{
$this->assertEquals($this->getExtension()->abbrMethod($method), $abbr);
}
public function getClassNameProvider()
{
return array(
array('F\Q\N\Foo', '<abbr title="F\Q\N\Foo">Foo</abbr>'),
array('Bare', '<abbr title="Bare">Bare</abbr>'),
);
}
public function getMethodNameProvider()
{
return array(
array('F\Q\N\Foo::Method', '<abbr title="F\Q\N\Foo">Foo</abbr>::Method()'),
array('Bare::Method', '<abbr title="Bare">Bare</abbr>::Method()'),
array('Closure', '<abbr title="Closure">Closure</abbr>'),
array('Method', '<abbr title="Method">Method</abbr>()')
);
}
public function testGetName()
{
$this->assertEquals('code', $this->getExtension()->getName());
}
protected function getExtension()
{
return new CodeExtension('txmt://open?url=file://%f&line=%l', '/root', 'UTF-8');
}
}

View File

@ -0,0 +1,30 @@
<?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\Bridge\Twig\Tests\Extension\Fixtures;
// Preventing autoloader throwing E_FATAL when Twig is now available
if (!class_exists('Twig_Environment')) {
class StubFilesystemLoader
{
}
} else {
class StubFilesystemLoader extends \Twig_Loader_Filesystem
{
protected function findTemplate($name)
{
// strip away bundle name
$parts = explode(':', $name);
return parent::findTemplate(end($parts));
}
}
}

View File

@ -0,0 +1,35 @@
<?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\Bridge\Twig\Tests\Extension\Fixtures;
use Symfony\Component\Translation\TranslatorInterface;
class StubTranslator implements TranslatorInterface
{
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
{
return '[trans]'.$id.'[/trans]';
}
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
return '[trans]'.$id.'[/trans]';
}
public function setLocale($locale)
{
}
public function getLocale()
{
}
}

View File

@ -0,0 +1,209 @@
<?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\Bridge\Twig\Tests\Extension;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
{
/**
* @var FormExtension
*/
protected $extension;
protected function setUp()
{
if (!class_exists('Symfony\Component\Locale\Locale')) {
$this->markTestSkipped('The "Locale" component is not available');
}
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
$this->markTestSkipped('The "EventDispatcher" component is not available');
}
if (!class_exists('Symfony\Component\Form\Form')) {
$this->markTestSkipped('The "Form" component is not available');
}
if (!class_exists('Twig_Environment')) {
$this->markTestSkipped('Twig is not available.');
}
parent::setUp();
$rendererEngine = new TwigRendererEngine(array(
'form_div_layout.html.twig',
'custom_widgets.html.twig',
));
$renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'));
$this->extension = new FormExtension($renderer);
$loader = new StubFilesystemLoader(array(
__DIR__.'/../../Resources/views/Form',
__DIR__,
));
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addGlobal('global', '');
$environment->addExtension($this->extension);
$this->extension->initRuntime($environment);
}
protected function tearDown()
{
parent::tearDown();
$this->extension = null;
}
public function testThemeBlockInheritanceUsingUse()
{
$view = $this->factory
->createNamed('name', 'email')
->createView()
;
$this->setTheme($view, array('theme_use.html.twig'));
$this->assertMatchesXpath(
$this->renderWidget($view),
'/input[@type="email"][@rel="theme"]'
);
}
public function testThemeBlockInheritanceUsingExtend()
{
$view = $this->factory
->createNamed('name', 'email')
->createView()
;
$this->setTheme($view, array('theme_extends.html.twig'));
$this->assertMatchesXpath(
$this->renderWidget($view),
'/input[@type="email"][@rel="theme"]'
);
}
public function isSelectedChoiceProvider()
{
// The commented cases should not be necessary anymore, because the
// choice lists should assure that both values passed here are always
// strings
return array(
// array(true, 0, 0),
array(true, '0', '0'),
array(true, '1', '1'),
// array(true, false, 0),
// array(true, true, 1),
array(true, '', ''),
// array(true, null, ''),
array(true, '1.23', '1.23'),
array(true, 'foo', 'foo'),
array(true, 'foo10', 'foo10'),
array(true, 'foo', array(1, 'foo', 'foo10')),
array(false, 10, array(1, 'foo', 'foo10')),
array(false, 0, array(1, 'foo', 'foo10')),
);
}
/**
* @dataProvider isSelectedChoiceProvider
*/
public function testIsChoiceSelected($expected, $choice, $value)
{
$choice = new ChoiceView($choice, $choice, $choice.' label');
$this->assertSame($expected, $this->extension->isSelectedChoice($choice, $value));
}
protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
}
protected function renderEnctype(FormView $view)
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'enctype');
}
protected function renderLabel(FormView $view, $label = null, array $vars = array())
{
if ($label !== null) {
$vars += array('label' => $label);
}
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars);
}
protected function renderErrors(FormView $view)
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors');
}
protected function renderWidget(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars);
}
protected function renderRow(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars);
}
protected function renderRest(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars);
}
protected function renderStart(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars);
}
protected function renderEnd(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars);
}
protected function setTheme(FormView $view, array $themes)
{
$this->extension->renderer->setTheme($view, $themes);
}
public static function themeBlockInheritanceProvider()
{
return array(
array(array('theme.html.twig'))
);
}
public static function themeInheritanceProvider()
{
return array(
array(array('parent_label.html.twig'), array('child_label.html.twig'))
);
}
}

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\Bridge\Twig\Tests\Extension;
use Symfony\Component\Form\FormView;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
{
/**
* @var FormExtension
*/
protected $extension;
protected function setUp()
{
if (!class_exists('Symfony\Component\Locale\Locale')) {
$this->markTestSkipped('The "Locale" component is not available');
}
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
$this->markTestSkipped('The "EventDispatcher" component is not available');
}
if (!class_exists('Symfony\Component\Form\Form')) {
$this->markTestSkipped('The "Form" component is not available');
}
if (!class_exists('Twig_Environment')) {
$this->markTestSkipped('Twig is not available.');
}
parent::setUp();
$rendererEngine = new TwigRendererEngine(array(
'form_table_layout.html.twig',
'custom_widgets.html.twig',
));
$renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'));
$this->extension = new FormExtension($renderer);
$loader = new StubFilesystemLoader(array(
__DIR__.'/../../Resources/views/Form',
__DIR__,
));
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addGlobal('global', '');
$environment->addExtension($this->extension);
$this->extension->initRuntime($environment);
}
protected function tearDown()
{
parent::tearDown();
$this->extension = null;
}
protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
}
protected function renderEnctype(FormView $view)
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'enctype');
}
protected function renderLabel(FormView $view, $label = null, array $vars = array())
{
if ($label !== null) {
$vars += array('label' => $label);
}
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars);
}
protected function renderErrors(FormView $view)
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors');
}
protected function renderWidget(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars);
}
protected function renderRow(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars);
}
protected function renderRest(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars);
}
protected function renderStart(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars);
}
protected function renderEnd(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars);
}
protected function setTheme(FormView $view, array $themes)
{
$this->extension->renderer->setTheme($view, $themes);
}
}

View File

@ -0,0 +1,68 @@
<?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\Bridge\Twig\Tests\Extension;
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
use Symfony\Bridge\Twig\Tests\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
class HttpKernelExtensionTest extends TestCase
{
protected function setUp()
{
parent::setUp();
if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
$this->markTestSkipped('The "HttpKernel" component is not available');
}
if (!class_exists('Twig_Environment')) {
$this->markTestSkipped('Twig is not available.');
}
}
/**
* @expectedException \Twig_Error_Runtime
*/
public function testFragmentWithError()
{
$kernel = $this->getFragmentHandler($this->throwException(new \Exception('foo')));
$loader = new \Twig_Loader_Array(array('index' => '{{ fragment("foo") }}'));
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
$twig->addExtension(new HttpKernelExtension($kernel));
$this->renderTemplate($kernel);
}
protected function getFragmentHandler($return)
{
$strategy = $this->getMock('Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface');
$strategy->expects($this->once())->method('getName')->will($this->returnValue('inline'));
$strategy->expects($this->once())->method('render')->will($return);
$renderer = new FragmentHandler(array($strategy));
$renderer->setRequest(Request::create('/'));
return $renderer;
}
protected function renderTemplate(FragmentHandler $renderer, $template = '{{ render("foo") }}')
{
$loader = new \Twig_Loader_Array(array('index' => $template));
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
$twig->addExtension(new HttpKernelExtension($renderer));
return $twig->render('index');
}
}

View File

@ -0,0 +1,60 @@
<?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\Bridge\Twig\Tests\Extension;
use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Bridge\Twig\Tests\TestCase;
class RoutingExtensionTest extends TestCase
{
protected function setUp()
{
parent::setUp();
if (!class_exists('Symfony\Component\Routing\Route')) {
$this->markTestSkipped('The "Routing" component is not available');
}
}
/**
* @dataProvider getEscapingTemplates
*/
public function testEscaping($template, $mustBeEscaped)
{
$twig = new \Twig_Environment(null, array('debug' => true, 'cache' => false, 'autoescape' => true, 'optimizations' => 0));
$twig->addExtension(new RoutingExtension($this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface')));
$nodes = $twig->parse($twig->tokenize($template));
$this->assertSame($mustBeEscaped, $nodes->getNode('body')->getNode(0)->getNode('expr') instanceof \Twig_Node_Expression_Filter);
}
public function getEscapingTemplates()
{
return array(
array('{{ path("foo") }}', false),
array('{{ path("foo", {}) }}', false),
array('{{ path("foo", { foo: "foo" }) }}', false),
array('{{ path("foo", foo) }}', true),
array('{{ path("foo", { foo: foo }) }}', true),
array('{{ path("foo", { foo: ["foo", "bar"] }) }}', true),
array('{{ path("foo", { foo: "foo", bar: "bar" }) }}', true),
array('{{ path(name = "foo", parameters = {}) }}', false),
array('{{ path(name = "foo", parameters = { foo: "foo" }) }}', false),
array('{{ path(name = "foo", parameters = foo) }}', true),
array('{{ path(name = "foo", parameters = { foo: ["foo", "bar"] }) }}', true),
array('{{ path(name = "foo", parameters = { foo: foo }) }}', true),
array('{{ path(name = "foo", parameters = { foo: "foo", bar: "bar" }) }}', true),
);
}
}

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\Bridge\Twig\Tests\Extension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Bridge\Twig\Tests\TestCase;
class TranslationExtensionTest extends TestCase
{
protected function setUp()
{
parent::setUp();
if (!class_exists('Symfony\Component\Translation\Translator')) {
$this->markTestSkipped('The "Translation" component is not available');
}
if (!class_exists('Twig_Environment')) {
$this->markTestSkipped('Twig is not available.');
}
}
public function testEscaping()
{
$output = $this->getTemplate('{% trans %}Percent: %value%%% (%msg%){% endtrans %}')->render(array('value' => 12, 'msg' => 'approx.'));
$this->assertEquals('Percent: 12% (approx.)', $output);
}
/**
* @dataProvider getTransTests
*/
public function testTrans($template, $expected, array $variables = array())
{
if ($expected != $this->getTemplate($template)->render($variables)) {
print $template."\n";
$loader = new \Twig_Loader_Array(array('index' => $template));
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
$twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSource('index'), 'index')))."\n\n";
$this->assertEquals($expected, $this->getTemplate($template)->render($variables));
}
$this->assertEquals($expected, $this->getTemplate($template)->render($variables));
}
public function getTransTests()
{
return array(
// trans tag
array('{% trans %}Hello{% endtrans %}', 'Hello'),
array('{% trans %}%name%{% endtrans %}', 'Symfony2', array('name' => 'Symfony2')),
array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'),
array('{% trans %}Hello %name%{% endtrans %}', 'Hello Symfony2', array('name' => 'Symfony2')),
array('{% trans with { \'%name%\': \'Symfony2\' } %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
array('{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans with vars %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
array('{% trans into "fr"%}Hello{% endtrans %}', 'Hello'),
// transchoice
array('{% transchoice count from "messages" %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
'There is no apples', array('count' => 0)),
array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
'There is 5 apples', array('count' => 5)),
array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
'There is 5 apples (Symfony2)', array('count' => 5, 'name' => 'Symfony2')),
array('{% transchoice count with { \'%name%\': \'Symfony2\' } %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
'There is 5 apples (Symfony2)', array('count' => 5)),
array('{% transchoice count into "fr"%}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
'There is no apples', array('count' => 0)),
// trans filter
array('{{ "Hello"|trans }}', 'Hello'),
array('{{ name|trans }}', 'Symfony2', array('name' => 'Symfony2')),
array('{{ hello|trans({ \'%name%\': \'Symfony2\' }) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
array('{% set vars = { \'%name%\': \'Symfony2\' } %}{{ hello|trans(vars) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
array('{{ "Hello"|trans({}, "messages", "fr") }}', 'Hello'),
// transchoice filter
array('{{ "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples"|transchoice(count) }}', 'There is 5 apples', array('count' => 5)),
array('{{ text|transchoice(5, {\'%name%\': \'Symfony2\'}) }}', 'There is 5 apples (Symfony2)', array('text' => '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%)')),
array('{{ "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples"|transchoice(count, {}, "messages", "fr") }}', 'There is 5 apples', array('count' => 5)),
);
}
public function testDefaultTranslationDomain()
{
$templates = array(
'index' => '
{%- extends "base" %}
{%- trans_default_domain "foo" %}
{%- block content %}
{%- trans %}foo{% endtrans %}
{%- trans from "custom" %}foo{% endtrans %}
{{- "foo"|trans }}
{{- "foo"|trans({}, "custom") }}
{{- "foo"|transchoice(1) }}
{{- "foo"|transchoice(1, {}, "custom") }}
{% endblock %}
',
'base' => '
{%- block content "" %}
',
);
$translator = new Translator('en', new MessageSelector());
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
$translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
$translator->addResource('array', array('foo' => 'foo (foo)'), 'en', 'foo');
$template = $this->getTemplate($templates, $translator);
$this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
}
protected function getTemplate($template, $translator = null)
{
if (null === $translator) {
$translator = new Translator('en', new MessageSelector());
}
if (is_array($template)) {
$loader = new \Twig_Loader_Array($template);
} else {
$loader = new \Twig_Loader_Array(array('index' => $template));
}
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
$twig->addExtension(new TranslationExtension($translator));
return $twig->loadTemplate('index');
}
}

View File

@ -0,0 +1,3 @@
{% block form_label %}
<label>{{ global }}child</label>
{% endblock form_label %}

View File

@ -0,0 +1,16 @@
{% block _text_id_widget %}
{% spaceless %}
<div id="container">
{{ form_widget(form) }}
</div>
{% endspaceless %}
{% endblock _text_id_widget %}
{% block _name_entry_label %}
{% spaceless %}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
<label>Custom label: {{ label|trans({}, translation_domain) }}</label>
{% endspaceless %}
{% endblock _name_entry_label %}

View File

@ -0,0 +1,3 @@
{% block form_label %}
<label>parent</label>
{% endblock form_label %}

View File

@ -0,0 +1,6 @@
{% block form_widget_simple %}
{% spaceless %}
{% set type = type|default('text') %}
<input type="{{ type }}" {{ block('widget_attributes') }} value="{{ value }}" rel="theme" />
{% endspaceless %}
{% endblock form_widget_simple %}

View File

@ -0,0 +1,8 @@
{% extends 'form_div_layout.html.twig' %}
{% block form_widget_simple %}
{% spaceless %}
{% set type = type|default('text') %}
<input type="{{ type }}" {{ block('widget_attributes') }} value="{{ value }}" rel="theme" />
{% endspaceless %}
{% endblock form_widget_simple %}

View File

@ -0,0 +1,8 @@
{% use 'form_div_layout.html.twig' %}
{% block form_widget_simple %}
{% spaceless %}
{% set type = type|default('text') %}
<input type="{{ type }}" {{ block('widget_attributes') }} value="{{ value }}" rel="theme" />
{% endspaceless %}
{% endblock form_widget_simple %}

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\Bridge\Twig\Tests\Node;
use Symfony\Bridge\Twig\Tests\TestCase;
use Symfony\Bridge\Twig\Node\FormThemeNode;
class FormThemeTest extends TestCase
{
protected function setUp()
{
parent::setUp();
if (version_compare(\Twig_Environment::VERSION, '1.5.0', '<')) {
$this->markTestSkipped('Requires Twig version to be at least 1.5.0.');
}
}
public function testConstructor()
{
$form = new \Twig_Node_Expression_Name('form', 0);
$resources = new \Twig_Node(array(
new \Twig_Node_Expression_Constant('tpl1', 0),
new \Twig_Node_Expression_Constant('tpl2', 0)
));
$node = new FormThemeNode($form, $resources, 0);
$this->assertEquals($form, $node->getNode('form'));
$this->assertEquals($resources, $node->getNode('resources'));
}
public function testCompile()
{
$form = new \Twig_Node_Expression_Name('form', 0);
$resources = new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant(0, 0),
new \Twig_Node_Expression_Constant('tpl1', 0),
new \Twig_Node_Expression_Constant(1, 0),
new \Twig_Node_Expression_Constant('tpl2', 0)
), 0);
$node = new FormThemeNode($form, $resources, 0);
$compiler = new \Twig_Compiler(new \Twig_Environment());
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"));',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
$resources = new \Twig_Node_Expression_Constant('tpl1', 0);
$node = new FormThemeNode($form, $resources, 0);
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->setTheme(%s, "tpl1");',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
protected function getVariableGetter($name)
{
if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
}
return sprintf('$this->getContext($context, "%s")', $name);
}
}

View File

@ -0,0 +1,282 @@
<?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\Bridge\Twig\Tests\Node;
use Symfony\Bridge\Twig\Tests\TestCase;
use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode;
class SearchAndRenderBlockNodeTest extends TestCase
{
protected function setUp()
{
parent::setUp();
if (version_compare(\Twig_Environment::VERSION, '1.5.0', '<')) {
$this->markTestSkipped('Requires Twig version to be at least 1.5.0.');
}
}
public function testCompileWidget()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
));
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
$compiler = new \Twig_Compiler(new \Twig_Environment());
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'widget\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileWidgetWithVariables()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant('foo', 0),
new \Twig_Node_Expression_Constant('bar', 0),
), 0),
));
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
$compiler = new \Twig_Compiler(new \Twig_Environment());
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithLabel()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Constant('my label', 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new \Twig_Compiler(new \Twig_Environment());
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithNullLabel()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Constant(null, 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new \Twig_Compiler(new \Twig_Environment());
// "label" => null must not be included in the output!
// Otherwise the default label is overwritten with null.
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithEmptyStringLabel()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Constant('', 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new \Twig_Compiler(new \Twig_Environment());
// "label" => null must not be included in the output!
// Otherwise the default label is overwritten with null.
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithDefaultLabel()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new \Twig_Compiler(new \Twig_Environment());
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithAttributes()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Constant(null, 0),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant('foo', 0),
new \Twig_Node_Expression_Constant('bar', 0),
), 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new \Twig_Compiler(new \Twig_Environment());
// "label" => null must not be included in the output!
// Otherwise the default label is overwritten with null.
// https://github.com/symfony/symfony/issues/5029
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithLabelAndAttributes()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Constant('value in argument', 0),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant('foo', 0),
new \Twig_Node_Expression_Constant('bar', 0),
new \Twig_Node_Expression_Constant('label', 0),
new \Twig_Node_Expression_Constant('value in attributes', 0),
), 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new \Twig_Compiler(new \Twig_Environment());
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in argument"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithLabelThatEvaluatesToNull()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Conditional(
// if
new \Twig_Node_Expression_Constant(true, 0),
// then
new \Twig_Node_Expression_Constant(null, 0),
// else
new \Twig_Node_Expression_Constant(null, 0),
0
),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new \Twig_Compiler(new \Twig_Environment());
// "label" => null must not be included in the output!
// Otherwise the default label is overwritten with null.
// https://github.com/symfony/symfony/issues/5029
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Conditional(
// if
new \Twig_Node_Expression_Constant(true, 0),
// then
new \Twig_Node_Expression_Constant(null, 0),
// else
new \Twig_Node_Expression_Constant(null, 0),
0
),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant('foo', 0),
new \Twig_Node_Expression_Constant('bar', 0),
new \Twig_Node_Expression_Constant('label', 0),
new \Twig_Node_Expression_Constant('value in attributes', 0),
), 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new \Twig_Compiler(new \Twig_Environment());
// "label" => null must not be included in the output!
// Otherwise the default label is overwritten with null.
// https://github.com/symfony/symfony/issues/5029
$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in attributes") + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
protected function getVariableGetter($name)
{
if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
}
return sprintf('$this->getContext($context, "%s")', $name);
}
}

View File

@ -0,0 +1,25 @@
<?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\Bridge\Twig\Tests\NodeVisitor;
use Symfony\Bridge\Twig\NodeVisitor\Scope;
use Symfony\Bridge\Twig\Tests\TestCase;
class ScopeTest extends TestCase
{
public function testScopeInitiation()
{
$scope = new Scope();
$scope->enter();
$this->assertNull($scope->get('test'));
}
}

View File

@ -0,0 +1,83 @@
<?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\Bridge\Twig\Tests\NodeVisitor;
use Symfony\Bridge\Twig\NodeVisitor\TranslationDefaultDomainNodeVisitor;
use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
use Symfony\Bridge\Twig\Tests\TestCase;
class TranslationDefaultDomainNodeVisitorTest extends TestCase
{
private static $message = 'message';
private static $domain = 'domain';
/** @dataProvider getDefaultDomainAssignmentTestData */
public function testDefaultDomainAssignment(\Twig_Node $node)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$visitor = new TranslationDefaultDomainNodeVisitor();
// visit trans_default_domain tag
$defaultDomain = TwigNodeProvider::getTransDefaultDomainTag(self::$domain);
$visitor->enterNode($defaultDomain, $env);
$visitor->leaveNode($defaultDomain, $env);
// visit tested node
$enteredNode = $visitor->enterNode($node, $env);
$leavedNode = $visitor->leaveNode($node, $env);
$this->assertSame($node, $enteredNode);
$this->assertSame($node, $leavedNode);
// extracting tested node messages
$visitor = new TranslationNodeVisitor();
$visitor->enable();
$visitor->enterNode($node, $env);
$visitor->leaveNode($node, $env);
$this->assertEquals(array(array(self::$message, self::$domain)), $visitor->getMessages());
}
/** @dataProvider getDefaultDomainAssignmentTestData */
public function testNewModuleWithoutDefaultDomainTag(\Twig_Node $node)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$visitor = new TranslationDefaultDomainNodeVisitor();
// visit trans_default_domain tag
$newModule = TwigNodeProvider::getModule('test');
$visitor->enterNode($newModule, $env);
$visitor->leaveNode($newModule, $env);
// visit tested node
$enteredNode = $visitor->enterNode($node, $env);
$leavedNode = $visitor->leaveNode($node, $env);
$this->assertSame($node, $enteredNode);
$this->assertSame($node, $leavedNode);
// extracting tested node messages
$visitor = new TranslationNodeVisitor();
$visitor->enable();
$visitor->enterNode($node, $env);
$visitor->leaveNode($node, $env);
$this->assertEquals(array(array(self::$message, null)), $visitor->getMessages());
}
public function getDefaultDomainAssignmentTestData()
{
return array(
array(TwigNodeProvider::getTransFilter(self::$message)),
array(TwigNodeProvider::getTransChoiceFilter(self::$message)),
array(TwigNodeProvider::getTransTag(self::$message)),
);
}
}

View File

@ -0,0 +1,61 @@
<?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\Bridge\Twig\Tests\NodeVisitor;
use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
use Symfony\Bridge\Twig\Tests\TestCase;
class TranslationNodeVisitorTest extends TestCase
{
/** @dataProvider getMessagesExtractionTestData */
public function testMessagesExtraction(\Twig_Node $node, array $expectedMessages)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$visitor = new TranslationNodeVisitor();
$visitor->enable();
$visitor->enterNode($node, $env);
$visitor->leaveNode($node, $env);
$this->assertEquals($expectedMessages, $visitor->getMessages());
}
public function testMessageExtractionWithInvalidDomainNode()
{
$message = 'new key';
$node = new \Twig_Node_Expression_Filter(
new \Twig_Node_Expression_Constant($message, 0),
new \Twig_Node_Expression_Constant('trans', 0),
new \Twig_Node(array(
new \Twig_Node_Expression_Array(array(), 0),
new \Twig_Node_Expression_Name('variable', 0),
)),
0
);
$this->testMessagesExtraction($node, array(array($message, TranslationNodeVisitor::UNDEFINED_DOMAIN)));
}
public function getMessagesExtractionTestData()
{
$message = 'new key';
$domain = 'domain';
return array(
array(TwigNodeProvider::getTransFilter($message), array(array($message, null))),
array(TwigNodeProvider::getTransChoiceFilter($message), array(array($message, null))),
array(TwigNodeProvider::getTransTag($message), array(array($message, null))),
array(TwigNodeProvider::getTransFilter($message, $domain), array(array($message, $domain))),
array(TwigNodeProvider::getTransChoiceFilter($message, $domain), array(array($message, $domain))),
array(TwigNodeProvider::getTransTag($message, $domain), array(array($message, $domain))),
);
}
}

View File

@ -0,0 +1,77 @@
<?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\Bridge\Twig\Tests\NodeVisitor;
use Symfony\Bridge\Twig\Node\TransDefaultDomainNode;
use Symfony\Bridge\Twig\Node\TransNode;
class TwigNodeProvider
{
public static function getModule($content)
{
return new \Twig_Node_Module(
new \Twig_Node_Expression_Constant($content, 0),
null,
new \Twig_Node_Expression_Array(array(), 0),
new \Twig_Node_Expression_Array(array(), 0),
new \Twig_Node_Expression_Array(array(), 0),
null,
null
);
}
public static function getTransFilter($message, $domain = null)
{
$arguments = $domain ? array(
new \Twig_Node_Expression_Array(array(), 0),
new \Twig_Node_Expression_Constant($domain, 0),
) : array();
return new \Twig_Node_Expression_Filter(
new \Twig_Node_Expression_Constant($message, 0),
new \Twig_Node_Expression_Constant('trans', 0),
new \Twig_Node($arguments),
0
);
}
public static function getTransChoiceFilter($message, $domain = null)
{
$arguments = $domain ? array(
new \Twig_Node_Expression_Constant(0, 0),
new \Twig_Node_Expression_Array(array(), 0),
new \Twig_Node_Expression_Constant($domain, 0),
) : array();
return new \Twig_Node_Expression_Filter(
new \Twig_Node_Expression_Constant($message, 0),
new \Twig_Node_Expression_Constant('transchoice', 0),
new \Twig_Node($arguments),
0
);
}
public static function getTransTag($message, $domain = null)
{
return new TransNode(
new \Twig_Node_Body(array(), array('data' => $message)),
$domain ? new \Twig_Node_Expression_Constant($domain, 0) : null
);
}
public static function getTransDefaultDomainTag($domain)
{
return new TransDefaultDomainNode(
new \Twig_Node_Expression_Constant($domain, 0)
);
}
}

View File

@ -0,0 +1,22 @@
<?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\Bridge\Twig\Tests;
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!class_exists('Twig_Environment')) {
$this->markTestSkipped('Twig is not available.');
}
}
}

View File

@ -0,0 +1,108 @@
<?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\Bridge\Twig\Tests\Node;
use Symfony\Bridge\Twig\Tests\TestCase;
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
use Symfony\Bridge\Twig\Node\FormThemeNode;
class FormThemeTokenParserTest extends TestCase
{
protected function setUp()
{
parent::setUp();
if (version_compare(\Twig_Environment::VERSION, '1.5.0', '<')) {
$this->markTestSkipped('Requires Twig version to be at least 1.5.0.');
}
}
/**
* @dataProvider getTestsForFormTheme
*/
public function testCompile($source, $expected)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$env->addTokenParser(new FormThemeTokenParser());
$stream = $env->tokenize($source);
$parser = new \Twig_Parser($env);
$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0));
}
public function getTestsForFormTheme()
{
return array(
array(
'{% form_theme form "tpl1" %}',
new FormThemeNode(
new \Twig_Node_Expression_Name('form', 1),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant(0, 1),
new \Twig_Node_Expression_Constant('tpl1', 1),
), 1),
1,
'form_theme'
)
),
array(
'{% form_theme form "tpl1" "tpl2" %}',
new FormThemeNode(
new \Twig_Node_Expression_Name('form', 1),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant(0, 1),
new \Twig_Node_Expression_Constant('tpl1', 1),
new \Twig_Node_Expression_Constant(1, 1),
new \Twig_Node_Expression_Constant('tpl2', 1)
), 1),
1,
'form_theme'
)
),
array(
'{% form_theme form with "tpl1" %}',
new FormThemeNode(
new \Twig_Node_Expression_Name('form', 1),
new \Twig_Node_Expression_Constant('tpl1', 1),
1,
'form_theme'
)
),
array(
'{% form_theme form with ["tpl1"] %}',
new FormThemeNode(
new \Twig_Node_Expression_Name('form', 1),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant(0, 1),
new \Twig_Node_Expression_Constant('tpl1', 1),
), 1),
1,
'form_theme'
)
),
array(
'{% form_theme form with ["tpl1", "tpl2"] %}',
new FormThemeNode(
new \Twig_Node_Expression_Name('form', 1),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant(0, 1),
new \Twig_Node_Expression_Constant('tpl1', 1),
new \Twig_Node_Expression_Constant(1, 1),
new \Twig_Node_Expression_Constant('tpl2', 1)
), 1),
1,
'form_theme'
)
),
);
}
}

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\Bridge\Twig\Tests\Translation;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Translation\TwigExtractor;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Bridge\Twig\Tests\TestCase;
class TwigExtractorTest extends TestCase
{
protected function setUp()
{
if (!class_exists('Symfony\Component\Translation\Translator')) {
$this->markTestSkipped('The "Translation" component is not available');
}
}
/**
* @dataProvider getExtractData
*/
public function testExtract($template, $messages)
{
$loader = new \Twig_Loader_Array(array());
$twig = new \Twig_Environment($loader, array(
'strict_variables' => true,
'debug' => true,
'cache' => false,
'autoescape' => false,
));
$twig->addExtension(new TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface')));
$extractor = new TwigExtractor($twig);
$extractor->setPrefix('prefix');
$catalogue = new MessageCatalogue('en');
$m = new \ReflectionMethod($extractor, 'extractTemplate');
$m->setAccessible(true);
$m->invoke($extractor, $template, $catalogue);
foreach ($messages as $key => $domain) {
$this->assertTrue($catalogue->has($key, $domain));
$this->assertEquals('prefix'.$key, $catalogue->get($key, $domain));
}
}
public function getExtractData()
{
return array(
array('{{ "new key" | trans() }}', array('new key' => 'messages')),
array('{{ "new key" | trans() | upper }}', array('new key' => 'messages')),
array('{{ "new key" | trans({}, "domain") }}', array('new key' => 'domain')),
array('{{ "new key" | transchoice(1) }}', array('new key' => 'messages')),
array('{{ "new key" | transchoice(1) | upper }}', array('new key' => 'messages')),
array('{{ "new key" | transchoice(1, {}, "domain") }}', array('new key' => 'domain')),
array('{% trans %}new key{% endtrans %}', array('new key' => 'messages')),
array('{% trans %} new key {% endtrans %}', array('new key' => 'messages')),
array('{% trans from "domain" %}new key{% endtrans %}', array('new key' => 'domain')),
array('{% set foo = "new key" | trans %}', array('new key' => 'messages')),
array('{{ 1 ? "new key" | trans : "another key" | trans }}', array('new key' => 'messages', 'another key' => 'messages')),
// make sure 'trans_default_domain' tag is supported
array('{% trans_default_domain "domain" %}{{ "new key"|trans }}', array('new key' => 'domain')),
array('{% trans_default_domain "domain" %}{{ "new key"|transchoice }}', array('new key' => 'domain')),
array('{% trans_default_domain "domain" %}{% trans %}new key{% endtrans %}', array('new key' => 'domain')),
// make sure this works with twig's named arguments
array('{{ "new key" | trans(domain="domain") }}', array('new key' => 'domain')),
array('{{ "new key" | transchoice(domain="domain", count=1) }}', array('new key' => 'domain')),
);
}
}