PluralizationRulesTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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;
  11. use Symfony\Component\Translation\PluralizationRules;
  12. /**
  13. * Test should cover all languages mentioned on http://translate.sourceforge.net/wiki/l10n/pluralforms
  14. * and Plural forms mentioned on http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms
  15. *
  16. * See also https://developer.mozilla.org/en/Localization_and_Plurals which mentions 15 rules having a maximum of 6 forms.
  17. * The mozilla code is also interesting to check for.
  18. *
  19. * As mentioned by chx http://drupal.org/node/1273968 we can cover all by testing number from 0 to 199
  20. *
  21. * The goal to cover all languages is to far fetched so this test case is smaller.
  22. *
  23. * @author Clemens Tolboom clemens@build2be.nl
  24. */
  25. class PluralizationRulesTest extends \PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. * We test failed langcode here.
  29. *
  30. * TODO: The languages mentioned in the data provide need to get fixed somehow within PluralizationRules.
  31. *
  32. * @dataProvider failingLangcodes
  33. */
  34. public function testFailedLangcodes($nplural, $langCodes)
  35. {
  36. $matrix = $this->generateTestData($nplural, $langCodes);
  37. $this->validateMatrix($nplural, $matrix, false);
  38. }
  39. /**
  40. * @dataProvider successLangcodes
  41. */
  42. public function testLangcodes($nplural, $langCodes)
  43. {
  44. $matrix = $this->generateTestData($nplural, $langCodes);
  45. $this->validateMatrix($nplural, $matrix);
  46. }
  47. /**
  48. * This array should contain all currently known langcodes.
  49. *
  50. * As it is impossible to have this ever complete we should try as hard as possible to have it almost complete.
  51. *
  52. * @return type
  53. */
  54. public function successLangcodes()
  55. {
  56. return array(
  57. array('1' , array('ay','bo', 'cgg','dz','id', 'ja', 'jbo', 'ka','kk','km','ko','ky')),
  58. array('2' , array('nl', 'fr', 'en', 'de', 'de_GE')),
  59. array('3' , array('be','bs','cs','hr')),
  60. array('4' , array('cy','mt', 'sl')),
  61. array('5' , array()),
  62. array('6' , array('ar')),
  63. );
  64. }
  65. /**
  66. * This array should be at least empty within the near future.
  67. *
  68. * This both depends on a complete list trying to add above as understanding
  69. * the plural rules of the current failing languages.
  70. *
  71. * @return array with nplural together with langcodes
  72. */
  73. public function failingLangcodes()
  74. {
  75. return array(
  76. array('1' , array('fa')),
  77. array('2' , array('jbo')),
  78. array('3' , array('cbs')),
  79. array('4' , array('gd','kw')),
  80. array('5' , array('ga')),
  81. array('6' , array()),
  82. );
  83. }
  84. /**
  85. * We validate only on the plural coverage. Thus the real rules is not tested.
  86. *
  87. * @param string $nplural plural expected
  88. * @param array $matrix containing langcodes and their plural index values.
  89. * @param boolean $expectSuccess
  90. */
  91. protected function validateMatrix($nplural, $matrix, $expectSuccess = true)
  92. {
  93. foreach ($matrix as $langCode => $data) {
  94. $indexes = array_flip($data);
  95. if ($expectSuccess) {
  96. $this->assertEquals($nplural, count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
  97. } else {
  98. $this->assertNotEquals((int) $nplural, count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
  99. }
  100. }
  101. }
  102. protected function generateTestData($plural, $langCodes)
  103. {
  104. $matrix = array();
  105. foreach ($langCodes as $langCode) {
  106. for ($count=0; $count<200; $count++) {
  107. $plural = PluralizationRules::get($count, $langCode);
  108. $matrix[$langCode][$count] = $plural;
  109. }
  110. }
  111. return $matrix;
  112. }
  113. }