Permalink
Browse files

moved component and bridge unit tests to the src/ directory

This is the first step to make each Symfony Component and Bridge self-contained.
  • Loading branch information...
1 parent a8499b8 commit b1301ae3412dba8d40074c1b72adfa1b55babbcd @fabpot fabpot committed Mar 28, 2012
View
14 README.md
@@ -7,6 +7,16 @@ Symfony2 components.
Resources
---------
-Unit tests:
+You can run the unit tests with the following command:
-https://github.com/symfony/symfony/tree/master/tests/Symfony/Tests/Bridge/Twig
+ phpunit -c src/Symfony/Bridge/Twig/
+
+If you also want to run the unit tests that depend on other Symfony
+Components, declare the following environment variables before running
+PHPUnit:
+
+ export HTTP_TWIG=../path/to/Twig
+ export HTTP_FORM=../path/to/Form
+ export HTTP_TRANSLATION=../path/to/Translation
+ export HTTP_EVENT_DISPATCHER=../path/to/EventDispatcher
+ export HTTP_LOCALE=../path/to/Locale
View
29 Tests/Extension/Fixtures/StubFilesystemLoader.php
@@ -0,0 +1,29 @@
+<?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
35 Tests/Extension/Fixtures/StubTranslator.php
@@ -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
147 Tests/Extension/FormExtensionDivLayoutTest.php
@@ -0,0 +1,147 @@
+<?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\Extension\FormExtension;
+use Symfony\Bridge\Twig\Extension\TranslationExtension;
+use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
+use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
+use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
+
+class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
+{
+ 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();
+
+ $loader = new StubFilesystemLoader(array(
+ __DIR__.'/../../../../../../src/Symfony/Bridge/Twig/Resources/views/Form',
+ __DIR__,
+ ));
+
+ $this->extension = new FormExtension($this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'), array(
+ 'form_div_layout.html.twig',
+ 'custom_widgets.html.twig',
+ ));
+
+ $environment = new \Twig_Environment($loader);
+ $environment->addExtension($this->extension);
+ $environment->addExtension(new TranslationExtension(new StubTranslator()));
+
+ $this->extension->initRuntime($environment);
+ }
+
+ protected function tearDown()
+ {
+ parent::tearDown();
+
+ $this->extension = null;
+ }
+
+ public function testThemeBlockInheritanceUsingUse()
+ {
+ $view = $this->factory
+ ->createNamed('email', 'name')
+ ->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('email', 'name')
+ ->createView()
+ ;
+
+ $this->setTheme($view, array('theme_extends.html.twig'));
+
+ $this->assertMatchesXpath(
+ $this->renderWidget($view),
+ '/input[@type="email"][@rel="theme"]'
+ );
+ }
+
+ protected function renderEnctype(FormView $view)
+ {
+ return (string)$this->extension->renderEnctype($view);
+ }
+
+ protected function renderLabel(FormView $view, $label = null, array $vars = array())
+ {
+ return (string)$this->extension->renderLabel($view, $label, $vars);
+ }
+
+ protected function renderErrors(FormView $view)
+ {
+ return (string)$this->extension->renderErrors($view);
+ }
+
+ protected function renderWidget(FormView $view, array $vars = array())
+ {
+ return (string)$this->extension->renderWidget($view, $vars);
+ }
+
+ protected function renderRow(FormView $view, array $vars = array())
+ {
+ return (string)$this->extension->renderRow($view, $vars);
+ }
+
+ protected function renderRest(FormView $view, array $vars = array())
+ {
+ return (string)$this->extension->renderRest($view, $vars);
+ }
+
+ protected function setTheme(FormView $view, array $themes)
+ {
+ $this->extension->setTheme($view, $themes);
+ }
+
+ static public function themeBlockInheritanceProvider()
+ {
+ return array(
+ array(array('theme.html.twig'))
+ );
+ }
+
+ static public function themeInheritanceProvider()
+ {
+ return array(
+ array(array('parent_label.html.twig'), array('child_label.html.twig'))
+ );
+ }
+}
View
103 Tests/Extension/FormExtensionTableLayoutTest.php
@@ -0,0 +1,103 @@
+<?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\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
+{
+ 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();
+
+ $loader = new StubFilesystemLoader(array(
+ __DIR__.'/../../../../../../src/Symfony/Bridge/Twig/Resources/views/Form',
+ __DIR__,
+ ));
+
+ $this->extension = new FormExtension($this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'), array(
+ 'form_table_layout.html.twig',
+ 'custom_widgets.html.twig',
+ ));
+
+ $environment = new \Twig_Environment($loader);
+ $environment->addExtension($this->extension);
+ $environment->addExtension(new TranslationExtension(new StubTranslator()));
+
+ $this->extension->initRuntime($environment);
+ }
+
+ protected function tearDown()
+ {
+ parent::tearDown();
+
+ $this->extension = null;
+ }
+
+ protected function renderEnctype(FormView $view)
+ {
+ return (string)$this->extension->renderEnctype($view);
+ }
+
+ protected function renderLabel(FormView $view, $label = null, array $vars = array())
+ {
+ return (string)$this->extension->renderLabel($view, $label, $vars);
+ }
+
+ protected function renderErrors(FormView $view)
+ {
+ return (string)$this->extension->renderErrors($view);
+ }
+
+ protected function renderWidget(FormView $view, array $vars = array())
+ {
+ return (string)$this->extension->renderWidget($view, $vars);
+ }
+
+ protected function renderRow(FormView $view, array $vars = array())
+ {
+ return (string)$this->extension->renderRow($view, $vars);
+ }
+
+ protected function renderRest(FormView $view, array $vars = array())
+ {
+ return (string)$this->extension->renderRest($view, $vars);
+ }
+
+ protected function setTheme(FormView $view, array $themes)
+ {
+ $this->extension->setTheme($view, $themes);
+ }
+}
View
149 Tests/Extension/TranslationExtensionTest.php
@@ -0,0 +1,149 @@
+<?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()
+ {
+ 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
3 Tests/Extension/child_label.html.twig
@@ -0,0 +1,3 @@
+{% block field_label %}
+ <label>child</label>
+{% endblock field_label %}
View
5 Tests/Extension/custom_widgets.html.twig
@@ -0,0 +1,5 @@
+{% block _text_id_widget %}
+<div id="container">
+ {{ form_widget(form) }}
+</div>
+{% endblock _text_id_widget %}
View
3 Tests/Extension/parent_label.html.twig
@@ -0,0 +1,3 @@
+{% block field_label %}
+ <label>parent</label>
+{% endblock field_label %}
View
6 Tests/Extension/theme.html.twig
@@ -0,0 +1,6 @@
+{% block field_widget %}
+{% spaceless %}
+ {% set type = type|default('text') %}
+ <input type="{{ type }}" {{ block('attributes') }} value="{{ value }}" rel="theme" />
+{% endspaceless %}
+{% endblock field_widget %}
View
8 Tests/Extension/theme_extends.html.twig
@@ -0,0 +1,8 @@
+{% extends 'form_div_layout.html.twig' %}
+
+{% block field_widget %}
+{% spaceless %}
+ {% set type = type|default('text') %}
+ <input type="{{ type }}" {{ block('attributes') }} value="{{ value }}" rel="theme" />
+{% endspaceless %}
+{% endblock field_widget %}
View
8 Tests/Extension/theme_use.html.twig
@@ -0,0 +1,8 @@
+{% use 'form_div_layout.html.twig' %}
+
+{% block field_widget %}
+{% spaceless %}
+ {% set type = type|default('text') %}
+ <input type="{{ type }}" {{ block('attributes') }} value="{{ value }}" rel="theme" />
+{% endspaceless %}
+{% endblock field_widget %}
View
85 Tests/Node/FormThemeTest.php
@@ -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(
+ 'echo $this->env->getExtension(\'form\')->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(
+ 'echo $this->env->getExtension(\'form\')->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
22 Tests/TestCase.php
@@ -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;
+
+class TestCase extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Twig_Environment')) {
+ $this->markTestSkipped('Twig is not available.');
+ }
+ }
+}
View
108 Tests/TokenParser/FormThemeTokenParserTest.php
@@ -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
71 Tests/Translation/TwigExtractorTest.php
@@ -0,0 +1,71 @@
+<?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 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')),
+ );
+ }
+}
View
37 Tests/bootstrap.php
@@ -0,0 +1,37 @@
+<?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.
+ */
+
+spl_autoload_register(function ($class) {
+ foreach (array(
+ 'SYMFONY_FORM' => 'Form',
+ 'SYMFONY_TRANSLATION' => 'Translation',
+ 'SYMFONY_LOCALE' => 'Locale',
+ 'SYMFONY_EVENT_DISPATCHER' => 'EventDispatcher',
+ ) as $env => $name) {
+ if (isset($_SERVER[$env]) && 0 === strpos(ltrim($class, '/'), 'Symfony\Component\\'.$name)) {
+ if (file_exists($file = $_SERVER[$env].'/'.substr(str_replace('\\', '/', $class), strlen('Symfony\Component\\'.$name)).'.php')) {
+ require_once $file;
+ }
+ }
+ }
+
+ if (isset($_SERVER['TWIG']) && 0 === strpos(ltrim($class, '/'), 'Twig_')) {
+ if (file_exists($file = $_SERVER['TWIG'].'/lib/'.str_replace('_', '/', $class).'.php')) {
+ require_once $file;
+ }
+ }
+
+ if (0 === strpos(ltrim($class, '/'), 'Symfony\Bridge\Twig')) {
+ if (file_exists($file = __DIR__.'/../'.substr(str_replace('\\', '/', $class), strlen('Symfony\Bridge\Twig')).'.php')) {
+ require_once $file;
+ }
+ }
+});
View
29 phpunit.xml.dist
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<phpunit backupGlobals="false"
+ backupStaticAttributes="false"
+ colors="true"
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+ processIsolation="false"
+ stopOnFailure="false"
+ syntaxCheck="false"
+ bootstrap="tests/bootstrap.php"
+>
+ <testsuites>
+ <testsuite name="Symfony Twig Bridge Test Suite">
+ <directory>./Tests/</directory>
+ </testsuite>
+ </testsuites>
+
+ <filter>
+ <whitelist>
+ <directory>./</directory>
+ <exclude>
+ <directory>./Resources</directory>
+ <directory>./Tests</directory>
+ </exclude>
+ </whitelist>
+ </filter>
+</phpunit>

0 comments on commit b1301ae

Please sign in to comment.