4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Translation\Translator;
16 use Symfony\Component\Translation\DataCollectorTranslator;
17 use Symfony\Component\Translation\Loader\ArrayLoader;
19 class DataCollectorTranslatorTest extends TestCase
21 public function testCollectMessages()
23 $collector = $this->createCollector();
24 $collector->setFallbackLocales(array('fr', 'ru'));
26 $collector->trans('foo');
27 $collector->trans('bar');
28 $collector->transChoice('choice', 0);
29 $collector->trans('bar_ru');
30 $collector->trans('bar_ru', array('foo' => 'bar'));
32 $expectedMessages = array();
33 $expectedMessages[] = array(
35 'translation' => 'foo (en)',
37 'domain' => 'messages',
38 'state' => DataCollectorTranslator::MESSAGE_DEFINED,
39 'parameters' => array(),
40 'transChoiceNumber' => null,
42 $expectedMessages[] = array(
44 'translation' => 'bar (fr)',
46 'domain' => 'messages',
47 'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
48 'parameters' => array(),
49 'transChoiceNumber' => null,
51 $expectedMessages[] = array(
53 'translation' => 'choice',
55 'domain' => 'messages',
56 'state' => DataCollectorTranslator::MESSAGE_MISSING,
57 'parameters' => array(),
58 'transChoiceNumber' => 0,
60 $expectedMessages[] = array(
62 'translation' => 'bar (ru)',
64 'domain' => 'messages',
65 'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
66 'parameters' => array(),
67 'transChoiceNumber' => null,
69 $expectedMessages[] = array(
71 'translation' => 'bar (ru)',
73 'domain' => 'messages',
74 'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
75 'parameters' => array('foo' => 'bar'),
76 'transChoiceNumber' => null,
79 $this->assertEquals($expectedMessages, $collector->getCollectedMessages());
82 private function createCollector()
84 $translator = new Translator('en');
85 $translator->addLoader('array', new ArrayLoader());
86 $translator->addResource('array', array('foo' => 'foo (en)'), 'en');
87 $translator->addResource('array', array('bar' => 'bar (fr)'), 'fr');
88 $translator->addResource('array', array('bar_ru' => 'bar (ru)'), 'ru');
90 return new DataCollectorTranslator($translator);