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 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. class PluralizationRulesTest extends TestCase
  27. {
  28. /**
  29. * We test failed langcode here.
  30. *
  31. * TODO: The languages mentioned in the data provide need to get fixed somehow within PluralizationRules.
  32. *
  33. * @dataProvider failingLangcodes
  34. */
  35. public function testFailedLangcodes($nplural, $langCodes)
  36. {
  37. $matrix = $this->generateTestData($langCodes);
  38. $this->validateMatrix($nplural, $matrix, false);
  39. }
  40. /**
  41. * @dataProvider successLangcodes
  42. */
  43. public function testLangcodes($nplural, $langCodes)
  44. {
  45. $matrix = $this->generateTestData($langCodes);
  46. $this->validateMatrix($nplural, $matrix);
  47. }
  48. /**
  49. * This array should contain all currently known langcodes.
  50. *
  51. * As it is impossible to have this ever complete we should try as hard as possible to have it almost complete.
  52. *
  53. * @return array
  54. */
  55. public function successLangcodes()
  56. {
  57. return array(
  58. array('1', array('ay', 'bo', 'cgg', 'dz', 'id', 'ja', 'jbo', 'ka', 'kk', 'km', 'ko', 'ky')),
  59. array('2', array('nl', 'fr', 'en', 'de', 'de_GE', 'hy', 'hy_AM')),
  60. array('3', array('be', 'bs', 'cs', 'hr')),
  61. array('4', array('cy', 'mt', 'sl')),
  62. array('5', array()),
  63. array('6', array('ar')),
  64. );
  65. }
  66. /**
  67. * This array should be at least empty within the near future.
  68. *
  69. * This both depends on a complete list trying to add above as understanding
  70. * the plural rules of the current failing languages.
  71. *
  72. * @return array with nplural together with langcodes
  73. */
  74. public function failingLangcodes()
  75. {
  76. return array(
  77. array('1', array('fa')),
  78. array('2', array('jbo')),
  79. array('3', array('cbs')),
  80. array('4', array('gd', 'kw')),
  81. array('5', array('ga')),
  82. array('6', array()),
  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 = array();
  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. }