TranslationDataCollectorTest.php 5.1 KB

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