Skip to content

Commit 061bc9f

Browse files
author
Benoît Burnichon
committed
Add some tests to TwigFileExtractor
1 parent 667a141 commit 061bc9f

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% set key="expression" %}
2+
3+
{{ key|trans|desc("Foo Bar")|meaning("Some Meaning")}}
4+
5+
{{ "text.foo_bar"|trans({}, key) }}
6+
7+
{% trans with {'%name%': 'Johannes'} from key %}text.name{% endtrans %}

Tests/Translation/Extractor/File/TwigFileExtractorTest.php

+47-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace JMS\TranslationBundle\Tests\Translation\Extractor\File;
2020

21+
use Prophecy\Argument;
2122
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
2223
use Symfony\Bridge\Twig\Form\TwigRenderer;
2324
use Symfony\Component\Routing\RequestContext;
@@ -35,12 +36,20 @@
3536
use JMS\TranslationBundle\Model\MessageCatalogue;
3637
use JMS\TranslationBundle\Twig\TranslationExtension;
3738
use JMS\TranslationBundle\Translation\Extractor\File\TwigFileExtractor;
38-
use Symfony\Bundle\FrameworkBundle\Routing\Router;
39-
use Symfony\Component\DependencyInjection\Container;
4039
use Symfony\Bridge\Twig\Extension\FormExtension;
4140

4241
class TwigFileExtractorTest extends \PHPUnit_Framework_TestCase
4342
{
43+
/**
44+
* @var \Twig_Environment
45+
*/
46+
private $env;
47+
48+
protected function setUp()
49+
{
50+
$this->env = $this->createTwigEnvironment();
51+
}
52+
4453
public function testExtractSimpleTemplate()
4554
{
4655
$expected = new MessageCatalogue();
@@ -136,12 +145,47 @@ public function testEmbeddedTemplate()
136145
$this->assertEquals($expected, $this->extract('embedded_template.html.twig'));
137146
}
138147

148+
public function testSimpleTemplateWithExpressions()
149+
{
150+
$logger = $this->prophesize('Psr\Log\LoggerInterface');
151+
$logger->error(Argument::any(), Argument::any())
152+
->shouldBeCalledTimes(3);
153+
154+
$extractor = new TwigFileExtractor($this->env);
155+
$extractor->setLogger($logger->reveal());
156+
157+
$this->extract('simple_template_with_expressions.html.twig', $extractor);
158+
}
159+
160+
public function testSimpleTemplateWithExpressionsWithoutLogger()
161+
{
162+
$this->setExpectedException('JMS\TranslationBundle\Exception\RuntimeException');
163+
$this->extract('simple_template_with_expressions.html.twig');
164+
}
165+
139166
private function extract($file, TwigFileExtractor $extractor = null)
140167
{
141168
if (!is_file($file = __DIR__.'/Fixture/'.$file)) {
142169
throw new RuntimeException(sprintf('The file "%s" does not exist.', $file));
143170
}
144171

172+
if (null === $extractor) {
173+
$extractor = new TwigFileExtractor($this->env);
174+
}
175+
176+
$ast = $this->env->parse($this->env->tokenize(file_get_contents($file), $file));
177+
178+
$catalogue = new MessageCatalogue();
179+
$extractor->visitTwigFile(new \SplFileInfo($file), $catalogue, $ast);
180+
181+
return $catalogue;
182+
}
183+
184+
/**
185+
* @return \Twig_Environment
186+
*/
187+
private function createTwigEnvironment()
188+
{
145189
$env = new \Twig_Environment();
146190
$env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector())));
147191
$env->addExtension(new TranslationExtension($translator, true));
@@ -157,15 +201,6 @@ private function extract($file, TwigFileExtractor $extractor = null)
157201
}
158202
}
159203

160-
if (null === $extractor) {
161-
$extractor = new TwigFileExtractor($env);
162-
}
163-
164-
$ast = $env->parse($env->tokenize(file_get_contents($file), $file));
165-
166-
$catalogue = new MessageCatalogue();
167-
$extractor->visitTwigFile(new \SplFileInfo($file), $catalogue, $ast);
168-
169-
return $catalogue;
204+
return $env;
170205
}
171206
}

0 commit comments

Comments
 (0)