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