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