4 * This file is part of the Twig Gettext utility.
6 * (c) Саша Стаменковић <umpirsky@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Twig\Gettext\Test
;
14 use Twig\Gettext\Extractor
;
15 use Twig\Gettext\Loader\Filesystem
;
16 use Symfony\Component\Translation\Loader\PoFileLoader
;
19 * @author Саша Стаменковић <umpirsky@gmail.com>
21 class ExtractorTest
extends \PHPUnit_Framework_TestCase
24 * @var \Twig_Environment
33 protected function setUp()
35 $this->twig
= new \
Twig_Environment(new Filesystem('/'), array(
36 'cache' => '/tmp/cache/'.uniqid(),
39 $this->twig
->addExtension(new \
Twig_Extensions_Extension_I18n());
41 $this->loader
= new PoFileLoader();
45 * @dataProvider testExtractDataProvider
47 public function testExtract(array $templates, array $parameters, array $messages)
49 $extractor = new Extractor($this->twig
);
51 foreach ($templates as $template) {
52 $extractor->addTemplate($template);
54 foreach ($parameters as $parameter) {
55 $extractor->addGettextParameter($parameter);
58 $extractor->extract();
60 $catalog = $this->loader
->load($this->getPotFile(), null);
62 foreach ($messages as $message) {
64 $catalog->has($message),
65 sprintf('Message "%s" not found in catalog.', $message)
70 public function testExtractDataProvider()
75 __DIR__
.'/Fixtures/twig/singular.twig',
76 __DIR__
.'/Fixtures/twig/plural.twig',
78 $this->getGettextParameters(),
82 'Hey %name%, I have one apple.',
83 'Hey %name%, I have %count% apples.',
89 public function testExtractNoTranslations()
91 $extractor = new Extractor($this->twig
);
93 $extractor->addTemplate(__DIR__
.'/Fixtures/twig/empty.twig');
94 $extractor->setGettextParameters($this->getGettextParameters());
96 $extractor->extract();
98 $catalog = $this->loader
->load($this->getPotFile(), null);
100 $this->assertEmpty($catalog->all('messages'));
103 private function getPotFile()
105 return __DIR__
.'/Fixtures/messages.pot';
108 private function getGettextParameters()
117 protected function tearDown()
119 if (file_exists($this->getPotFile())) {
120 unlink($this->getPotFile());