]>
Commit | Line | Data |
---|---|---|
a4565e88 NL |
1 | <?php |
2 | ||
3 | /** | |
4 | * This file is part of the Twig Gettext utility. | |
5 | * | |
6 | * (c) Саша Стаменковић <umpirsky@gmail.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 Twig\Gettext\Test; | |
13 | ||
14 | use Twig\Gettext\Extractor; | |
15 | use Twig\Gettext\Loader\Filesystem; | |
16 | use Symfony\Component\Translation\Loader\PoFileLoader; | |
17 | ||
18 | /** | |
19 | * @author Саша Стаменковић <umpirsky@gmail.com> | |
20 | */ | |
21 | class ExtractorTest extends \PHPUnit_Framework_TestCase | |
22 | { | |
23 | /** | |
24 | * @var \Twig_Environment | |
25 | */ | |
26 | protected $twig; | |
27 | ||
28 | /** | |
29 | * @var PoFileLoader | |
30 | */ | |
31 | protected $loader; | |
32 | ||
33 | protected function setUp() | |
34 | { | |
35 | $this->twig = new \Twig_Environment(new Filesystem('/'), array( | |
36 | 'cache' => '/tmp/cache/'.uniqid(), | |
37 | 'auto_reload' => true | |
38 | )); | |
39 | $this->twig->addExtension(new \Twig_Extensions_Extension_I18n()); | |
40 | ||
41 | $this->loader = new PoFileLoader(); | |
42 | } | |
43 | ||
44 | /** | |
45 | * @dataProvider testExtractDataProvider | |
46 | */ | |
47 | public function testExtract(array $templates, array $parameters, array $messages) | |
48 | { | |
49 | $extractor = new Extractor($this->twig); | |
50 | ||
51 | foreach ($templates as $template) { | |
52 | $extractor->addTemplate($template); | |
53 | } | |
54 | foreach ($parameters as $parameter) { | |
55 | $extractor->addGettextParameter($parameter); | |
56 | } | |
57 | ||
58 | $extractor->extract(); | |
59 | ||
60 | $catalog = $this->loader->load($this->getPotFile(), null); | |
61 | ||
62 | foreach ($messages as $message) { | |
63 | $this->assertTrue( | |
64 | $catalog->has($message), | |
65 | sprintf('Message "%s" not found in catalog.', $message) | |
66 | ); | |
67 | } | |
68 | } | |
69 | ||
70 | public function testExtractDataProvider() | |
71 | { | |
72 | return array( | |
73 | array( | |
74 | array( | |
75 | __DIR__.'/Fixtures/twig/singular.twig', | |
76 | __DIR__.'/Fixtures/twig/plural.twig', | |
77 | ), | |
78 | $this->getGettextParameters(), | |
79 | array( | |
80 | 'Hello %name%!', | |
81 | 'Hello World!', | |
82 | 'Hey %name%, I have one apple.', | |
83 | 'Hey %name%, I have %count% apples.', | |
84 | ), | |
85 | ), | |
86 | ); | |
87 | } | |
88 | ||
89 | public function testExtractNoTranslations() | |
90 | { | |
91 | $extractor = new Extractor($this->twig); | |
92 | ||
93 | $extractor->addTemplate(__DIR__.'/Fixtures/twig/empty.twig'); | |
94 | $extractor->setGettextParameters($this->getGettextParameters()); | |
95 | ||
96 | $extractor->extract(); | |
97 | ||
98 | $catalog = $this->loader->load($this->getPotFile(), null); | |
99 | ||
100 | $this->assertEmpty($catalog->all('messages')); | |
101 | } | |
102 | ||
103 | private function getPotFile() | |
104 | { | |
105 | return __DIR__.'/Fixtures/messages.pot'; | |
106 | } | |
107 | ||
108 | private function getGettextParameters() | |
109 | { | |
110 | return array( | |
111 | '--force-po', | |
112 | '-o', | |
113 | $this->getPotFile(), | |
114 | ); | |
115 | } | |
116 | ||
117 | protected function tearDown() | |
118 | { | |
119 | if (file_exists($this->getPotFile())) { | |
120 | unlink($this->getPotFile()); | |
121 | } | |
122 | } | |
123 | } |