TranslationDataCollectorTest.php 5.3 KB

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