Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit b1301ae3412dba8d40074c1b72adfa1b55babbcd 1 parent a8499b8
Fabien Potencier fabpot authored
14 README.md
Source Rendered
@@ -7,6 +7,16 @@ Symfony2 components.
7 7 Resources
8 8 ---------
9 9
10   -Unit tests:
  10 +You can run the unit tests with the following command:
11 11
12   -https://github.com/symfony/symfony/tree/master/tests/Symfony/Tests/Bridge/Twig
  12 + phpunit -c src/Symfony/Bridge/Twig/
  13 +
  14 +If you also want to run the unit tests that depend on other Symfony
  15 +Components, declare the following environment variables before running
  16 +PHPUnit:
  17 +
  18 + export HTTP_TWIG=../path/to/Twig
  19 + export HTTP_FORM=../path/to/Form
  20 + export HTTP_TRANSLATION=../path/to/Translation
  21 + export HTTP_EVENT_DISPATCHER=../path/to/EventDispatcher
  22 + export HTTP_LOCALE=../path/to/Locale
29 Tests/Extension/Fixtures/StubFilesystemLoader.php
... ... @@ -0,0 +1,29 @@
  1 +<?php
  2 +
  3 +/*
  4 + * This file is part of the Symfony package.
  5 + *
  6 + * (c) Fabien Potencier <fabien@symfony.com>
  7 + *
  8 + * For the full copyright and license information, please view the LICENSE
  9 + * file that was distributed with this source code.
  10 + */
  11 +
  12 +namespace Symfony\Bridge\Twig\Tests\Extension\Fixtures;
  13 +
  14 +// Preventing autoloader throwing E_FATAL when Twig is now available
  15 +if (!class_exists('Twig_Environment')) {
  16 + class StubFilesystemLoader {
  17 + }
  18 +} else {
  19 + class StubFilesystemLoader extends \Twig_Loader_Filesystem
  20 + {
  21 + protected function findTemplate($name)
  22 + {
  23 + // strip away bundle name
  24 + $parts = explode(':', $name);
  25 +
  26 + return parent::findTemplate(end($parts));
  27 + }
  28 + }
  29 +}
35 Tests/Extension/Fixtures/StubTranslator.php
... ... @@ -0,0 +1,35 @@
  1 +<?php
  2 +
  3 +/*
  4 + * This file is part of the Symfony package.
  5 + *
  6 + * (c) Fabien Potencier <fabien@symfony.com>
  7 + *
  8 + * For the full copyright and license information, please view the LICENSE
  9 + * file that was distributed with this source code.
  10 + */
  11 +
  12 +namespace Symfony\Bridge\Twig\Tests\Extension\Fixtures;
  13 +
  14 +use Symfony\Component\Translation\TranslatorInterface;
  15 +
  16 +class StubTranslator implements TranslatorInterface
  17 +{
  18 + public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  19 + {
  20 + return '[trans]'.$id.'[/trans]';
  21 + }
  22 +
  23 + public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  24 + {
  25 + return '[trans]'.$id.'[/trans]';
  26 + }
  27 +
  28 + public function setLocale($locale)
  29 + {
  30 + }
  31 +
  32 + public function getLocale()
  33 + {
  34 + }
  35 +}
147 Tests/Extension/FormExtensionDivLayoutTest.php
... ... @@ -0,0 +1,147 @@
  1 +<?php
  2 +
  3 +/*
  4 + * This file is part of the Symfony package.
  5 + *
  6 + * (c) Fabien Potencier <fabien@symfony.com>
  7 + *
  8 + * For the full copyright and license information, please view the LICENSE
  9 + * file that was distributed with this source code.
  10 + */
  11 +
  12 +namespace Symfony\Bridge\Twig\Tests\Extension;
  13 +
  14 +use Symfony\Component\Form\FormView;
  15 +use Symfony\Bridge\Twig\Extension\FormExtension;
  16 +use Symfony\Bridge\Twig\Extension\TranslationExtension;
  17 +use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
  18 +use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
  19 +use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
  20 +
  21 +class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
  22 +{
  23 + protected $extension;
  24 +
  25 + protected function setUp()
  26 + {
  27 + if (!class_exists('Symfony\Component\Locale\Locale')) {
  28 + $this->markTestSkipped('The "Locale" component is not available');
  29 + }
  30 +
  31 + if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
  32 + $this->markTestSkipped('The "EventDispatcher" component is not available');
  33 + }
  34 +
  35 + if (!class_exists('Symfony\Component\Form\Form')) {
  36 + $this->markTestSkipped('The "Form" component is not available');
  37 + }
  38 +
  39 + if (!class_exists('Twig_Environment')) {
  40 + $this->markTestSkipped('Twig is not available.');
  41 + }
  42 +
  43 + parent::setUp();
  44 +
  45 + $loader = new StubFilesystemLoader(array(
  46 + __DIR__.'/../../../../../../src/Symfony/Bridge/Twig/Resources/views/Form',
  47 + __DIR__,
  48 + ));
  49 +
  50 + $this->extension = new FormExtension($this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'), array(
  51 + 'form_div_layout.html.twig',
  52 + 'custom_widgets.html.twig',
  53 + ));
  54 +
  55 + $environment = new \Twig_Environment($loader);
  56 + $environment->addExtension($this->extension);
  57 + $environment->addExtension(new TranslationExtension(new StubTranslator()));
  58 +
  59 + $this->extension->initRuntime($environment);
  60 + }
  61 +
  62 + protected function tearDown()
  63 + {
  64 + parent::tearDown();
  65 +
  66 + $this->extension = null;
  67 + }
  68 +
  69 + public function testThemeBlockInheritanceUsingUse()
  70 + {
  71 + $view = $this->factory
  72 + ->createNamed('email', 'name')
  73 + ->createView()
  74 + ;
  75 +
  76 + $this->setTheme($view, array('theme_use.html.twig'));
  77 +
  78 + $this->assertMatchesXpath(
  79 + $this->renderWidget($view),
  80 + '/input[@type="email"][@rel="theme"]'
  81 + );
  82 + }
  83 +
  84 + public function testThemeBlockInheritanceUsingExtend()
  85 + {
  86 + $view = $this->factory
  87 + ->createNamed('email', 'name')
  88 + ->createView()
  89 + ;
  90 +
  91 + $this->setTheme($view, array('theme_extends.html.twig'));
  92 +
  93 + $this->assertMatchesXpath(
  94 + $this->renderWidget($view),
  95 + '/input[@type="email"][@rel="theme"]'
  96 + );
  97 + }
  98 +
  99 + protected function renderEnctype(FormView $view)
  100 + {
  101 + return (string)$this->extension->renderEnctype($view);
  102 + }
  103 +
  104 + protected function renderLabel(FormView $view, $label = null, array $vars = array())
  105 + {
  106 + return (string)$this->extension->renderLabel($view, $label, $vars);
  107 + }
  108 +
  109 + protected function renderErrors(FormView $view)
  110 + {
  111 + return (string)$this->extension->renderErrors($view);
  112 + }
  113 +
  114 + protected function renderWidget(FormView $view, array $vars = array())
  115 + {
  116 + return (string)$this->extension->renderWidget($view, $vars);
  117 + }
  118 +
  119 + protected function renderRow(FormView $view, array $vars = array())
  120 + {
  121 + return (string)$this->extension->renderRow($view, $vars);
  122 + }
  123 +
  124 + protected function renderRest(FormView $view, array $vars = array())
  125 + {
  126 + return (string)$this->extension->renderRest($view, $vars);
  127 + }
  128 +
  129 + protected function setTheme(FormView $view, array $themes)
  130 + {
  131 + $this->extension->setTheme($view, $themes);
  132 + }
  133 +
  134 + static public function themeBlockInheritanceProvider()
  135 + {
  136 + return array(
  137 + array(array('theme.html.twig'))
  138 + );
  139 + }
  140 +
  141 + static public function themeInheritanceProvider()
  142 + {
  143 + return array(
  144 + array(array('parent_label.html.twig'), array('child_label.html.twig'))
  145 + );
  146 + }
  147 +}
103 Tests/Extension/FormExtensionTableLayoutTest.php
... ... @@ -0,0 +1,103 @@
  1 +<?php
  2 +
  3 +/*
  4 + * This file is part of the Symfony package.
  5 + *
  6 + * (c) Fabien Potencier <fabien@symfony.com>
  7 + *
  8 + * For the full copyright and license information, please view the LICENSE
  9 + * file that was distributed with this source code.
  10 + */
  11 +
  12 +namespace Symfony\Bridge\Twig\Tests\Extension;
  13 +
  14 +use Symfony\Component\Form\FormView;
  15 +use Symfony\Bridge\Twig\Extension\FormExtension;
  16 +use Symfony\Bridge\Twig\Extension\TranslationExtension;
  17 +use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
  18 +use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
  19 +use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
  20 +
  21 +class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
  22 +{
  23 + protected $extension;
  24 +
  25 + protected function setUp()
  26 + {
  27 + if (!class_exists('Symfony\Component\Locale\Locale')) {
  28 + $this->markTestSkipped('The "Locale" component is not available');
  29 + }
  30 +
  31 + if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
  32 + $this->markTestSkipped('The "EventDispatcher" component is not available');
  33 + }
  34 +
  35 + if (!class_exists('Symfony\Component\Form\Form')) {
  36 + $this->markTestSkipped('The "Form" component is not available');
  37 + }
  38 +
  39 + if (!class_exists('Twig_Environment')) {
  40 + $this->markTestSkipped('Twig is not available.');
  41 + }
  42 +
  43 + parent::setUp();
  44 +
  45 + $loader = new StubFilesystemLoader(array(
  46 + __DIR__.'/../../../../../../src/Symfony/Bridge/Twig/Resources/views/Form',
  47 + __DIR__,
  48 + ));
  49 +
  50 + $this->extension = new FormExtension($this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'), array(
  51 + 'form_table_layout.html.twig',
  52 + 'custom_widgets.html.twig',
  53 + ));
  54 +
  55 + $environment = new \Twig_Environment($loader);
  56 + $environment->addExtension($this->extension);
  57 + $environment->addExtension(new TranslationExtension(new StubTranslator()));
  58 +
  59 + $this->extension->initRuntime($environment);
  60 + }
  61 +
  62 + protected function tearDown()
  63 + {
  64 + parent::tearDown();
  65 +
  66 + $this->extension = null;
  67 + }
  68 +
  69 + protected function renderEnctype(FormView $view)
  70 + {
  71 + return (string)$this->extension->renderEnctype($view);
  72 + }
  73 +
  74 + protected function renderLabel(FormView $view, $label = null, array $vars = array())
  75 + {
  76 + return (string)$this->extension->renderLabel($view, $label, $vars);
  77 + }
  78 +
  79 + protected function renderErrors(FormView $view)
  80 + {
  81 + return (string)$this->extension->renderErrors($view);
  82 + }
  83 +
  84 + protected function renderWidget(FormView $view, array $vars = array())
  85 + {
  86 + return (string)$this->extension->renderWidget($view, $vars);
  87 + }
  88 +
  89 + protected function renderRow(FormView $view, array $vars = array())
  90 + {
  91 + return (string)$this->extension->renderRow($view, $vars);
  92 + }
  93 +
  94 + protected function renderRest(FormView $view, array $vars = array())
  95 + {
  96 + return (string)$this->extension->renderRest($view, $vars);
  97 + }
  98 +
  99 + protected function setTheme(FormView $view, array $themes)
  100 + {
  101 + $this->extension->setTheme($view, $themes);
  102 + }
  103 +}
149 Tests/Extension/TranslationExtensionTest.php
... ... @@ -0,0 +1,149 @@
  1 +<?php
  2 +
  3 +/*
  4 + * This file is part of the Symfony package.
  5 + *
  6 + * (c) Fabien Potencier <fabien@symfony.com>
  7 + *
  8 + * For the full copyright and license information, please view the LICENSE
  9 + * file that was distributed with this source code.
  10 + */
  11 +
  12 +namespace Symfony\Bridge\Twig\Tests\Extension;
  13 +
  14 +use Symfony\Bridge\Twig\Extension\TranslationExtension;
  15 +use Symfony\Component\Translation\Translator;
  16 +use Symfony\Component\Translation\MessageSelector;
  17 +use Symfony\Component\Translation\Loader\ArrayLoader;
  18 +use Symfony\Bridge\Twig\Tests\TestCase;
  19 +
  20 +class TranslationExtensionTest extends TestCase
  21 +{
  22 + protected function setUp()
  23 + {
  24 + if (!class_exists('Symfony\Component\Translation\Translator')) {
  25 + $this->markTestSkipped('The "Translation" component is not available');
  26 + }
  27 +
  28 + if (!class_exists('Twig_Environment')) {
  29 + $this->markTestSkipped('Twig is not available.');
  30 + }
  31 + }
  32 +
  33 + public function testEscaping()
  34 + {
  35 + $output = $this->getTemplate('{% trans %}Percent: %value%%% (%msg%){% endtrans %}')->render(array('value' => 12, 'msg' => 'approx.'));
  36 +
  37 + $this->assertEquals('Percent: 12% (approx.)', $output);
  38 + }
  39 +
  40 + /**
  41 + * @dataProvider getTransTests
  42 + */
  43 + public function testTrans($template, $expected, array $variables = array())
  44 + {
  45 + if ($expected != $this->getTemplate($template)->render($variables)) {
  46 + print $template."\n";
  47 + $loader = new \Twig_Loader_Array(array('index' => $template));
  48 + $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  49 + $twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
  50 +
  51 + echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSource('index'), 'index')))."\n\n";
  52 + $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
  53 + }
  54 +
  55 + $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
  56 + }
  57 +
  58 + public function getTransTests()
  59 + {
  60 + return array(
  61 + // trans tag
  62 + array('{% trans %}Hello{% endtrans %}', 'Hello'),
  63 + array('{% trans %}%name%{% endtrans %}', 'Symfony2', array('name' => 'Symfony2')),
  64 +
  65 + array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'),
  66 +
  67 + array('{% trans %}Hello %name%{% endtrans %}', 'Hello Symfony2', array('name' => 'Symfony2')),
  68 + array('{% trans with { \'%name%\': \'Symfony2\' } %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
  69 + array('{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans with vars %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
  70 +
  71 + array('{% trans into "fr"%}Hello{% endtrans %}', 'Hello'),
  72 +
  73 + // transchoice
  74 + array('{% transchoice count from "messages" %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  75 + 'There is no apples', array('count' => 0)),
  76 + array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  77 + 'There is 5 apples', array('count' => 5)),
  78 + array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
  79 + 'There is 5 apples (Symfony2)', array('count' => 5, 'name' => 'Symfony2')),
  80 + array('{% transchoice count with { \'%name%\': \'Symfony2\' } %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
  81 + 'There is 5 apples (Symfony2)', array('count' => 5)),
  82 + array('{% transchoice count into "fr"%}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  83 + 'There is no apples', array('count' => 0)),
  84 +
  85 + // trans filter
  86 + array('{{ "Hello"|trans }}', 'Hello'),
  87 + array('{{ name|trans }}', 'Symfony2', array('name' => 'Symfony2')),
  88 + array('{{ hello|trans({ \'%name%\': \'Symfony2\' }) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  89 + array('{% set vars = { \'%name%\': \'Symfony2\' } %}{{ hello|trans(vars) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  90 + array('{{ "Hello"|trans({}, "messages", "fr") }}', 'Hello'),
  91 +
  92 + // transchoice filter
  93 + 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)),
  94 + 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%)')),
  95 + 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)),
  96 + );
  97 + }
  98 +
  99 + public function testDefaultTranslationDomain()
  100 + {
  101 + $templates = array(
  102 + 'index' => '
  103 + {%- extends "base" %}
  104 +
  105 + {%- trans_default_domain "foo" %}
  106 +
  107 + {%- block content %}
  108 + {%- trans %}foo{% endtrans %}
  109 + {%- trans from "custom" %}foo{% endtrans %}
  110 + {{- "foo"|trans }}
  111 + {{- "foo"|trans({}, "custom") }}
  112 + {{- "foo"|transchoice(1) }}
  113 + {{- "foo"|transchoice(1, {}, "custom") }}
  114 + {% endblock %}
  115 + ',
  116 +
  117 + 'base' => '
  118 + {%- block content "" %}
  119 + ',
  120 + );
  121 +
  122 + $translator = new Translator('en', new MessageSelector());
  123 + $translator->addLoader('array', new ArrayLoader());
  124 + $translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
  125 + $translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
  126 + $translator->addResource('array', array('foo' => 'foo (foo)'), 'en', 'foo');
  127 +
  128 + $template = $this->getTemplate($templates, $translator);
  129 +
  130 + $this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
  131 + }
  132 +
  133 + protected function getTemplate($template, $translator = null)
  134 + {
  135 + if (null === $translator) {
  136 + $translator = new Translator('en', new MessageSelector());
  137 + }
  138 +
  139 + if (is_array($template)) {
  140 + $loader = new \Twig_Loader_Array($template);
  141 + } else {
  142 + $loader = new \Twig_Loader_Array(array('index' => $template));
  143 + }
  144 + $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  145 + $twig->addExtension(new TranslationExtension($translator));
  146 +
  147 + return $twig->loadTemplate('index');
  148 + }
  149 +}
3  Tests/Extension/child_label.html.twig
... ... @@ -0,0 +1,3 @@
  1 +{% block field_label %}
  2 + <label>child</label>
  3 +{% endblock field_label %}
5 Tests/Extension/custom_widgets.html.twig
... ... @@ -0,0 +1,5 @@
  1 +{% block _text_id_widget %}
  2 +<div id="container">
  3 + {{ form_widget(form) }}
  4 +</div>
  5 +{% endblock _text_id_widget %}
3  Tests/Extension/parent_label.html.twig
... ... @@ -0,0 +1,3 @@
  1 +{% block field_label %}
  2 + <label>parent</label>
  3 +{% endblock field_label %}
6 Tests/Extension/theme.html.twig
... ... @@ -0,0 +1,6 @@
  1 +{% block field_widget %}
  2 +{% spaceless %}
  3 + {% set type = type|default('text') %}
  4 + <input type="{{ type }}" {{ block('attributes') }} value="{{ value }}" rel="theme" />
  5 +{% endspaceless %}
  6 +{% endblock field_widget %}
8 Tests/Extension/theme_extends.html.twig
... ... @@ -0,0 +1,8 @@
  1 +{% extends 'form_div_layout.html.twig' %}
  2 +
  3 +{% block field_widget %}
  4 +{% spaceless %}
  5 + {% set type = type|default('text') %}
  6 + <input type="{{ type }}" {{ block('attributes') }} value="{{ value }}" rel="theme" />
  7 +{% endspaceless %}
  8 +{% endblock field_widget %}
8 Tests/Extension/theme_use.html.twig
... ... @@ -0,0 +1,8 @@
  1 +{% use 'form_div_layout.html.twig' %}
  2 +
  3 +{% block field_widget %}
  4 +{% spaceless %}
  5 + {% set type = type|default('text') %}
  6 + <input type="{{ type }}" {{ block('attributes') }} value="{{ value }}" rel="theme" />
  7 +{% endspaceless %}
  8 +{% endblock field_widget %}
85 Tests/Node/FormThemeTest.php
... ... @@ -0,0 +1,85 @@
  1 +<?php
  2 +
  3 +/*
  4 + * This file is part of the Symfony package.
  5 + *
  6 + * (c) Fabien Potencier <fabien@symfony.com>
  7 + *
  8 + * For the full copyright and license information, please view the LICENSE
  9 + * file that was distributed with this source code.
  10 + */
  11 +
  12 +namespace Symfony\Bridge\Twig\Tests\Node;
  13 +
  14 +use Symfony\Bridge\Twig\Tests\TestCase;
  15 +use Symfony\Bridge\Twig\Node\FormThemeNode;
  16 +
  17 +class FormThemeTest extends TestCase
  18 +{
  19 + protected function setUp()
  20 + {
  21 + parent::setUp();
  22 +
  23 + if (version_compare(\Twig_Environment::VERSION, '1.5.0', '<')) {
  24 + $this->markTestSkipped('Requires Twig version to be at least 1.5.0.');
  25 + }
  26 + }
  27 +
  28 + public function testConstructor()
  29 + {
  30 + $form = new \Twig_Node_Expression_Name('form', 0);
  31 + $resources = new \Twig_Node(array(
  32 + new \Twig_Node_Expression_Constant('tpl1', 0),
  33 + new \Twig_Node_Expression_Constant('tpl2', 0)
  34 + ));
  35 +
  36 + $node = new FormThemeNode($form, $resources, 0);
  37 +
  38 + $this->assertEquals($form, $node->getNode('form'));
  39 + $this->assertEquals($resources, $node->getNode('resources'));
  40 + }
  41 +
  42 + public function testCompile()
  43 + {
  44 + $form = new \Twig_Node_Expression_Name('form', 0);
  45 + $resources = new \Twig_Node_Expression_Array(array(
  46 + new \Twig_Node_Expression_Constant(0, 0),
  47 + new \Twig_Node_Expression_Constant('tpl1', 0),
  48 + new \Twig_Node_Expression_Constant(1, 0),
  49 + new \Twig_Node_Expression_Constant('tpl2', 0)
  50 + ), 0);
  51 +
  52 + $node = new FormThemeNode($form, $resources, 0);
  53 +
  54 + $compiler = new \Twig_Compiler(new \Twig_Environment());
  55 +
  56 + $this->assertEquals(
  57 + sprintf(
  58 + 'echo $this->env->getExtension(\'form\')->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"));',
  59 + $this->getVariableGetter('form')
  60 + ),
  61 + trim($compiler->compile($node)->getSource())
  62 + );
  63 +
  64 + $resources = new \Twig_Node_Expression_Constant('tpl1', 0);
  65 +
  66 + $node = new FormThemeNode($form, $resources, 0);
  67 +
  68 + $this->assertEquals(
  69 + sprintf(
  70 + 'echo $this->env->getExtension(\'form\')->setTheme(%s, "tpl1");',
  71 + $this->getVariableGetter('form')
  72 + ),
  73 + trim($compiler->compile($node)->getSource())
  74 + );
  75 + }
  76 +
  77 + protected function getVariableGetter($name)
  78 + {
  79 + if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
  80 + return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
  81 + }
  82 +
  83 + return sprintf('$this->getContext($context, "%s")', $name);
  84 + }
  85 +}
22 Tests/TestCase.php
... ... @@ -0,0 +1,22 @@
  1 +<?php
  2 +
  3 +/*
  4 + * This file is part of the Symfony package.
  5 + *
  6 + * (c) Fabien Potencier <fabien@symfony.com>
  7 + *
  8 + * For the full copyright and license information, please view the LICENSE
  9 + * file that was distributed with this source code.
  10 + */
  11 +
  12 +namespace Symfony\Bridge\Twig\Tests;
  13 +
  14 +class TestCase extends \PHPUnit_Framework_TestCase
  15 +{
  16 + protected function setUp()
  17 + {
  18 + if (!class_exists('Twig_Environment')) {
  19 + $this->markTestSkipped('Twig is not available.');
  20 + }
  21 + }
  22 +}
108 Tests/TokenParser/FormThemeTokenParserTest.php
... ... @@ -0,0 +1,108 @@
  1 +<?php
  2 +
  3 +/*
  4 + * This file is part of the Symfony package.
  5 + *
  6 + * (c) Fabien Potencier <fabien@symfony.com>
  7 + *
  8 + * For the full copyright and license information, please view the LICENSE
  9 + * file that was distributed with this source code.
  10 + */
  11 +
  12 +namespace Symfony\Bridge\Twig\Tests\Node;
  13 +
  14 +use Symfony\Bridge\Twig\Tests\TestCase;
  15 +use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
  16 +use Symfony\Bridge\Twig\Node\FormThemeNode;
  17 +
  18 +class FormThemeTokenParserTest extends TestCase
  19 +{
  20 + protected function setUp()
  21 + {
  22 + parent::setUp();
  23 +
  24 + if (version_compare(\Twig_Environment::VERSION, '1.5.0', '<')) {
  25 + $this->markTestSkipped('Requires Twig version to be at least 1.5.0.');
  26 + }
  27 + }
  28 +
  29 + /**
  30 + * @dataProvider getTestsForFormTheme
  31 + */
  32 + public function testCompile($source, $expected)
  33 + {
  34 + $env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
  35 + $env->addTokenParser(new FormThemeTokenParser());
  36 + $stream = $env->tokenize($source);
  37 + $parser = new \Twig_Parser($env);
  38 +
  39 + $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0));
  40 + }
  41 +
  42 + public function getTestsForFormTheme()
  43 + {
  44 + return array(
  45 + array(
  46 + '{% form_theme form "tpl1" %}',
  47 + new FormThemeNode(
  48 + new \Twig_Node_Expression_Name('form', 1),
  49 + new \Twig_Node_Expression_Array(array(
  50 + new \Twig_Node_Expression_Constant(0, 1),
  51 + new \Twig_Node_Expression_Constant('tpl1', 1),
  52 + ), 1),
  53 + 1,
  54 + 'form_theme'
  55 + )
  56 + ),
  57 + array(
  58 + '{% form_theme form "tpl1" "tpl2" %}',
  59 + new FormThemeNode(
  60 + new \Twig_Node_Expression_Name('form', 1),
  61 + new \Twig_Node_Expression_Array(array(
  62 + new \Twig_Node_Expression_Constant(0, 1),
  63 + new \Twig_Node_Expression_Constant('tpl1', 1),
  64 + new \Twig_Node_Expression_Constant(1, 1),
  65 + new \Twig_Node_Expression_Constant('tpl2', 1)
  66 + ), 1),
  67 + 1,
  68 + 'form_theme'
  69 + )
  70 + ),
  71 + array(
  72 + '{% form_theme form with "tpl1" %}',
  73 + new FormThemeNode(
  74 + new \Twig_Node_Expression_Name('form', 1),
  75 + new \Twig_Node_Expression_Constant('tpl1', 1),
  76 + 1,
  77 + 'form_theme'
  78 + )
  79 + ),
  80 + array(
  81 + '{% form_theme form with ["tpl1"] %}',
  82 + new FormThemeNode(
  83 + new \Twig_Node_Expression_Name('form', 1),
  84 + new \Twig_Node_Expression_Array(array(
  85 + new \Twig_Node_Expression_Constant(0, 1),
  86 + new \Twig_Node_Expression_Constant('tpl1', 1),
  87 + ), 1),
  88 + 1,
  89 + 'form_theme'
  90 + )
  91 + ),
  92 + array(
  93 + '{% form_theme form with ["tpl1", "tpl2"] %}',
  94 + new FormThemeNode(
  95 + new \Twig_Node_Expression_Name('form', 1),
  96 + new \Twig_Node_Expression_Array(array(
  97 + new \Twig_Node_Expression_Constant(0, 1),
  98 + new \Twig_Node_Expression_Constant('tpl1', 1),
  99 + new \Twig_Node_Expression_Constant(1, 1),
  100 + new \Twig_Node_Expression_Constant('tpl2', 1)
  101 + ), 1),
  102 + 1,
  103 + 'form_theme'
  104 + )
  105 + ),
  106 + );
  107 + }
  108 +}
71 Tests/Translation/TwigExtractorTest.php
... ... @@ -0,0 +1,71 @@
  1 +<?php
  2 +
  3 +/*
  4 + * This file is part of the Symfony package.
  5 + *
  6 + * (c) Fabien Potencier <fabien@symfony.com>
  7 + *
  8 + * For the full copyright and license information, please view the LICENSE
  9 + * file that was distributed with this source code.
  10 + */
  11 +
  12 +namespace Symfony\Bridge\Twig\Tests\Translation;
  13 +
  14 +use Symfony\Bridge\Twig\Extension\TranslationExtension;
  15 +use Symfony\Bridge\Twig\Translation\TwigExtractor;
  16 +use Symfony\Component\Translation\MessageCatalogue;
  17 +use Symfony\Bridge\Twig\Tests\TestCase;
  18 +
  19 +class TwigExtractorTest extends TestCase
  20 +{
  21 + protected function setUp()
  22 + {
  23 + if (!class_exists('Symfony\Component\Translation\Translator')) {
  24 + $this->markTestSkipped('The "Translation" component is not available');
  25 + }
  26 + }
  27 +
  28 + /**
  29 + * @dataProvider getExtractData
  30 + */
  31 + public function testExtract($template, $messages)
  32 + {
  33 + $loader = new \Twig_Loader_Array(array());
  34 + $twig = new \Twig_Environment($loader, array(
  35 + 'strict_variables' => true,
  36 + 'debug' => true,
  37 + 'cache' => false,
  38 + 'autoescape' => false,
  39 + ));
  40 + $twig->addExtension(new TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface')));
  41 +
  42 + $extractor = new TwigExtractor($twig);
  43 + $extractor->setPrefix('prefix');
  44 + $catalogue = new MessageCatalogue('en');
  45 +
  46 + $m = new \ReflectionMethod($extractor, 'extractTemplate');
  47 + $m->setAccessible(true);
  48 + $m->invoke($extractor, $template, $catalogue);
  49 +
  50 + foreach ($messages as $key => $domain) {
  51 + $this->assertTrue($catalogue->has($key, $domain));
  52 + $this->assertEquals('prefix'.$key, $catalogue->get($key, $domain));
  53 + }
  54 + }
  55 +
  56 + public function getExtractData()
  57 + {
  58 + return array(
  59 + array('{{ "new key" | trans() }}', array('new key' => 'messages')),
  60 + array('{{ "new key" | trans() | upper }}', array('new key' => 'messages')),
  61 + array('{{ "new key" | trans({}, "domain") }}', array('new key' => 'domain')),
  62 + array('{{ "new key" | transchoice(1) }}', array('new key' => 'messages')),
  63 + array('{{ "new key" | transchoice(1) | upper }}', array('new key' => 'messages')),
  64 + array('{{ "new key" | transchoice(1, {}, "domain") }}', array('new key' => 'domain')),
  65 + array('{% trans %}new key{% endtrans %}', array('new key' => 'messages')),
  66 + array('{% trans from "domain" %}new key{% endtrans %}', array('new key' => 'domain')),
  67 + array('{% set foo = "new key" | trans %}', array('new key' => 'messages')),
  68 + array('{{ 1 ? "new key" | trans : "another key" | trans }}', array('new key' => 'messages', 'another key' => 'messages')),
  69 + );
  70 + }
  71 +}
37 Tests/bootstrap.php
... ... @@ -0,0 +1,37 @@
  1 +<?php
  2 +
  3 +/*
  4 + * This file is part of the Symfony package.
  5 + *
  6 + * (c) Fabien Potencier <fabien@symfony.com>
  7 + *
  8 + * For the full copyright and license information, please view the LICENSE
  9 + * file that was distributed with this source code.
  10 + */
  11 +
  12 +spl_autoload_register(function ($class) {
  13 + foreach (array(
  14 + 'SYMFONY_FORM' => 'Form',
  15 + 'SYMFONY_TRANSLATION' => 'Translation',
  16 + 'SYMFONY_LOCALE' => 'Locale',
  17 + 'SYMFONY_EVENT_DISPATCHER' => 'EventDispatcher',
  18 + ) as $env => $name) {
  19 + if (isset($_SERVER[$env]) && 0 === strpos(ltrim($class, '/'), 'Symfony\Component\\'.$name)) {
  20 + if (file_exists($file = $_SERVER[$env].'/'.substr(str_replace('\\', '/', $class), strlen('Symfony\Component\\'.$name)).'.php')) {
  21 + require_once $file;
  22 + }
  23 + }
  24 + }
  25 +
  26 + if (isset($_SERVER['TWIG']) && 0 === strpos(ltrim($class, '/'), 'Twig_')) {
  27 + if (file_exists($file = $_SERVER['TWIG'].'/lib/'.str_replace('_', '/', $class).'.php')) {
  28 + require_once $file;
  29 + }
  30 + }
  31 +
  32 + if (0 === strpos(ltrim($class, '/'), 'Symfony\Bridge\Twig')) {
  33 + if (file_exists($file = __DIR__.'/../'.substr(str_replace('\\', '/', $class), strlen('Symfony\Bridge\Twig')).'.php')) {
  34 + require_once $file;
  35 + }
  36 + }
  37 +});
29 phpunit.xml.dist
... ... @@ -0,0 +1,29 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +
  3 +<phpunit backupGlobals="false"
  4 + backupStaticAttributes="false"
  5 + colors="true"
  6 + convertErrorsToExceptions="true"
  7 + convertNoticesToExceptions="true"
  8 + convertWarningsToExceptions="true"
  9 + processIsolation="false"
  10 + stopOnFailure="false"
  11 + syntaxCheck="false"
  12 + bootstrap="tests/bootstrap.php"
  13 +>
  14 + <testsuites>
  15 + <testsuite name="Symfony Twig Bridge Test Suite">
  16 + <directory>./Tests/</directory>
  17 + </testsuite>
  18 + </testsuites>
  19 +
  20 + <filter>
  21 + <whitelist>
  22 + <directory>./</directory>
  23 + <exclude>
  24 + <directory>./Resources</directory>
  25 + <directory>./Tests</directory>
  26 + </exclude>
  27 + </whitelist>
  28 + </filter>
  29 +</phpunit>

0 comments on commit b1301ae

Please sign in to comment.
Something went wrong with that request. Please try again.