18
18
19
19
namespace JMS \TranslationBundle \Tests \Translation \Extractor \File ;
20
20
21
+ use Prophecy \Argument ;
21
22
use Symfony \Bridge \Twig \Form \TwigRendererEngine ;
22
23
use Symfony \Bridge \Twig \Form \TwigRenderer ;
23
24
use Symfony \Component \Routing \RequestContext ;
35
36
use JMS \TranslationBundle \Model \MessageCatalogue ;
36
37
use JMS \TranslationBundle \Twig \TranslationExtension ;
37
38
use JMS \TranslationBundle \Translation \Extractor \File \TwigFileExtractor ;
38
- use Symfony \Bundle \FrameworkBundle \Routing \Router ;
39
- use Symfony \Component \DependencyInjection \Container ;
40
39
use Symfony \Bridge \Twig \Extension \FormExtension ;
41
40
42
41
class TwigFileExtractorTest extends \PHPUnit_Framework_TestCase
43
42
{
43
+ /**
44
+ * @var \Twig_Environment
45
+ */
46
+ private $ env ;
47
+
48
+ protected function setUp ()
49
+ {
50
+ $ this ->env = $ this ->createTwigEnvironment ();
51
+ }
52
+
44
53
public function testExtractSimpleTemplate ()
45
54
{
46
55
$ expected = new MessageCatalogue ();
@@ -136,12 +145,47 @@ public function testEmbeddedTemplate()
136
145
$ this ->assertEquals ($ expected , $ this ->extract ('embedded_template.html.twig ' ));
137
146
}
138
147
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
+
139
166
private function extract ($ file , TwigFileExtractor $ extractor = null )
140
167
{
141
168
if (!is_file ($ file = __DIR__ .'/Fixture/ ' .$ file )) {
142
169
throw new RuntimeException (sprintf ('The file "%s" does not exist. ' , $ file ));
143
170
}
144
171
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
+ {
145
189
$ env = new \Twig_Environment ();
146
190
$ env ->addExtension (new SymfonyTranslationExtension ($ translator = new IdentityTranslator (new MessageSelector ())));
147
191
$ env ->addExtension (new TranslationExtension ($ translator , true ));
@@ -157,15 +201,6 @@ private function extract($file, TwigFileExtractor $extractor = null)
157
201
}
158
202
}
159
203
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 ;
170
205
}
171
206
}
0 commit comments