8dea492153897de4a137ea17a213566f40bacc30
[yaffs-website] / vendor / symfony / translation / Tests / DataCollector / TranslationDataCollectorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Translation\Tests\DataCollector;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Translation\DataCollectorTranslator;
16 use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
17 use Symfony\Component\VarDumper\Cloner\VarCloner;
18
19 class TranslationDataCollectorTest extends TestCase
20 {
21     protected function setUp()
22     {
23         if (!class_exists('Symfony\Component\HttpKernel\DataCollector\DataCollector')) {
24             $this->markTestSkipped('The "DataCollector" is not available');
25         }
26     }
27
28     public function testCollectEmptyMessages()
29     {
30         $translator = $this->getTranslator();
31         $translator->expects($this->any())->method('getCollectedMessages')->will($this->returnValue(array()));
32
33         $dataCollector = new TranslationDataCollector($translator);
34         $dataCollector->lateCollect();
35
36         $this->assertEquals(0, $dataCollector->getCountMissings());
37         $this->assertEquals(0, $dataCollector->getCountFallbacks());
38         $this->assertEquals(0, $dataCollector->getCountDefines());
39         $this->assertEquals(array(), $dataCollector->getMessages());
40     }
41
42     public function testCollect()
43     {
44         $cloner = new VarCloner();
45
46         $collectedMessages = array(
47             array(
48                   'id' => 'foo',
49                   'translation' => 'foo (en)',
50                   'locale' => 'en',
51                   'domain' => 'messages',
52                   'state' => DataCollectorTranslator::MESSAGE_DEFINED,
53                   'parameters' => array(),
54                   'transChoiceNumber' => null,
55             ),
56             array(
57                   'id' => 'bar',
58                   'translation' => 'bar (fr)',
59                   'locale' => 'fr',
60                   'domain' => 'messages',
61                   'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
62                   'parameters' => array(),
63                   'transChoiceNumber' => null,
64             ),
65             array(
66                   'id' => 'choice',
67                   'translation' => 'choice',
68                   'locale' => 'en',
69                   'domain' => 'messages',
70                   'state' => DataCollectorTranslator::MESSAGE_MISSING,
71                   'parameters' => array('%count%' => 3),
72                   'transChoiceNumber' => 3,
73             ),
74             array(
75                   'id' => 'choice',
76                   'translation' => 'choice',
77                   'locale' => 'en',
78                   'domain' => 'messages',
79                   'state' => DataCollectorTranslator::MESSAGE_MISSING,
80                   'parameters' => array('%count%' => 3),
81                   'transChoiceNumber' => 3,
82             ),
83             array(
84                   'id' => 'choice',
85                   'translation' => 'choice',
86                   'locale' => 'en',
87                   'domain' => 'messages',
88                   'state' => DataCollectorTranslator::MESSAGE_MISSING,
89                   'parameters' => array('%count%' => 4, '%foo%' => 'bar'),
90                   'transChoiceNumber' => 4,
91             ),
92         );
93         $expectedMessages = array(
94             array(
95                   'id' => 'foo',
96                   'translation' => 'foo (en)',
97                   'locale' => 'en',
98                   'domain' => 'messages',
99                   'state' => DataCollectorTranslator::MESSAGE_DEFINED,
100                   'count' => 1,
101                   'parameters' => array(),
102                   'transChoiceNumber' => null,
103             ),
104             array(
105                   'id' => 'bar',
106                   'translation' => 'bar (fr)',
107                   'locale' => 'fr',
108                   'domain' => 'messages',
109                   'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
110                   'count' => 1,
111                   'parameters' => array(),
112                   'transChoiceNumber' => null,
113             ),
114             array(
115                   'id' => 'choice',
116                   'translation' => 'choice',
117                   'locale' => 'en',
118                   'domain' => 'messages',
119                   'state' => DataCollectorTranslator::MESSAGE_MISSING,
120                   'count' => 3,
121                   'parameters' => array(
122                       $cloner->cloneVar(array('%count%' => 3)),
123                       $cloner->cloneVar(array('%count%' => 3)),
124                       $cloner->cloneVar(array('%count%' => 4, '%foo%' => 'bar')),
125                   ),
126                   'transChoiceNumber' => 3,
127             ),
128         );
129
130         $translator = $this->getTranslator();
131         $translator->expects($this->any())->method('getCollectedMessages')->will($this->returnValue($collectedMessages));
132
133         $dataCollector = new TranslationDataCollector($translator);
134         $dataCollector->lateCollect();
135
136         $this->assertEquals(1, $dataCollector->getCountMissings());
137         $this->assertEquals(1, $dataCollector->getCountFallbacks());
138         $this->assertEquals(1, $dataCollector->getCountDefines());
139         $this->assertEquals($expectedMessages, array_values($dataCollector->getMessages()));
140     }
141
142     private function getTranslator()
143     {
144         $translator = $this
145             ->getMockBuilder('Symfony\Component\Translation\DataCollectorTranslator')
146             ->disableOriginalConstructor()
147             ->getMock()
148         ;
149
150         return $translator;
151     }
152 }