|
26 | 26 | use Symfony\Component\Routing\Generator\UrlGenerator;
|
27 | 27 | use Symfony\Bridge\Twig\Extension\RoutingExtension;
|
28 | 28 | use JMS\TranslationBundle\Twig\RemovingNodeVisitor;
|
29 |
| -use JMS\TranslationBundle\Twig2\RemovingNodeVisitor as Twig2RemovingNodeVisitor; |
30 | 29 | use JMS\TranslationBundle\Twig\DefaultApplyingNodeVisitor;
|
31 |
| -use JMS\TranslationBundle\Twig2\DefaultApplyingNodeVisitor as Twig2DefaultApplyingNodeVisitor; |
32 | 30 | use JMS\TranslationBundle\Exception\RuntimeException;
|
33 | 31 | use Symfony\Component\Translation\MessageSelector;
|
34 | 32 | use Symfony\Component\Translation\IdentityTranslator;
|
35 | 33 | use Symfony\Bridge\Twig\Extension\TranslationExtension as SymfonyTranslationExtension;
|
36 | 34 | use JMS\TranslationBundle\Model\Message;
|
37 | 35 | use JMS\TranslationBundle\Model\MessageCatalogue;
|
38 | 36 | use JMS\TranslationBundle\Twig\TranslationExtension;
|
39 |
| -use JMS\TranslationBundle\Twig2\TranslationExtension as Twig2TranslationExtension; |
40 | 37 | use JMS\TranslationBundle\Translation\Extractor\File\TwigFileExtractor;
|
41 |
| -use JMS\TranslationBundle\Translation\Extractor\File\Twig2FileExtractor; |
42 | 38 | use Symfony\Bridge\Twig\Extension\FormExtension;
|
43 | 39 |
|
44 | 40 | class TwigFileExtractorTest extends \PHPUnit_Framework_TestCase
|
45 | 41 | {
|
46 |
| - /** @var \Twig_Environment */ |
47 |
| - protected $env; |
48 |
| - |
49 |
| - /** @var \JMS\TranslationBundle\Translation\Extractor\File\TwigFileExtractor */ |
50 |
| - protected $extractor; |
51 |
| - |
52 |
| - public function setUp() |
53 |
| - { |
54 |
| - $this->env = new \Twig_Environment(new \Twig_Loader_Array(array())); |
55 |
| - $this->env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector()))); |
56 |
| - |
57 |
| - if (defined('Twig_Environment::MAJOR_VERSION') && \Twig_Environment::MAJOR_VERSION > 1) { |
58 |
| - $this->env->addExtension(new Twig2TranslationExtension($translator, true)); |
59 |
| - } else { |
60 |
| - $this->env->addExtension(new TranslationExtension($translator, true)); |
61 |
| - } |
62 |
| - |
63 |
| - $this->env->addExtension(new RoutingExtension(new UrlGenerator(new RouteCollection(), new RequestContext()))); |
64 |
| - $this->env->addExtension(new FormExtension(new TwigRenderer(new TwigRendererEngine()))); |
65 |
| - |
66 |
| - foreach ($this->env->getNodeVisitors() as $visitor) { |
67 |
| - if ($visitor instanceof DefaultApplyingNodeVisitor || $visitor instanceof Twig2DefaultApplyingNodeVisitor) { |
68 |
| - $visitor->setEnabled(false); |
69 |
| - } |
70 |
| - if ($visitor instanceof RemovingNodeVisitor || $visitor instanceof Twig2RemovingNodeVisitor) { |
71 |
| - $visitor->setEnabled(false); |
72 |
| - } |
73 |
| - } |
74 |
| - |
75 |
| - if (defined('Twig_Environment::MAJOR_VERSION') && \Twig_Environment::MAJOR_VERSION > 1) { |
76 |
| - $this->extractor = new Twig2FileExtractor($this->env, new FileSourceFactory('faux')); |
77 |
| - } else { |
78 |
| - $this->extractor = new TwigFileExtractor($this->env, new FileSourceFactory('faux')); |
79 |
| - } |
80 |
| - } |
81 |
| - |
82 | 42 | public function testExtractSimpleTemplate()
|
83 | 43 | {
|
84 | 44 | $expected = new MessageCatalogue();
|
@@ -183,11 +143,26 @@ private function extract($file, TwigFileExtractor $extractor = null)
|
183 | 143 | throw new RuntimeException(sprintf('The file "%s" does not exist.', $file));
|
184 | 144 | }
|
185 | 145 |
|
| 146 | + $env = new \Twig_Environment(new \Twig_Loader_Array(array())); |
| 147 | + $env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector()))); |
| 148 | + $env->addExtension(new TranslationExtension($translator, true)); |
| 149 | + $env->addExtension(new RoutingExtension(new UrlGenerator(new RouteCollection(), new RequestContext()))); |
| 150 | + $env->addExtension(new FormExtension(new TwigRenderer(new TwigRendererEngine()))); |
| 151 | + |
| 152 | + foreach ($env->getNodeVisitors() as $visitor) { |
| 153 | + if ($visitor instanceof DefaultApplyingNodeVisitor) { |
| 154 | + $visitor->setEnabled(false); |
| 155 | + } |
| 156 | + if ($visitor instanceof RemovingNodeVisitor) { |
| 157 | + $visitor->setEnabled(false); |
| 158 | + } |
| 159 | + } |
| 160 | + |
186 | 161 | if (null === $extractor) {
|
187 |
| - $extractor = $this->extractor; |
| 162 | + $extractor = new TwigFileExtractor($env, new FileSourceFactory('faux')); |
188 | 163 | }
|
189 | 164 |
|
190 |
| - $ast = $this->env->parse($this->env->tokenize(new \Twig_Source(file_get_contents($file), $file))); |
| 165 | + $ast = $env->parse($env->tokenize(new \Twig_Source(file_get_contents($file), $file))); |
191 | 166 |
|
192 | 167 | $catalogue = new MessageCatalogue();
|
193 | 168 | $extractor->visitTwigFile(new \SplFileInfo($file), $catalogue, $ast);
|
|
0 commit comments