I18nTest.php 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace App\Tests\Core\I18n;
  19. // require_once '/home/hugo/software/social/config/bootstrap.php';
  20. // require_once '/home/hugo/software/social/src/Core/I18n/I18n.php';
  21. use function App\Core\I18n\_m;
  22. use App\Core\I18n\I18n;
  23. use App\Core\I18n\I18nHelper;
  24. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  25. use Symfony\Contracts\Translation\TranslatorInterface;
  26. // use Jchook\AssertThrows\AssertThrows;
  27. class I18nTest extends WebTestCase
  28. {
  29. // use AssertThrows;
  30. public function testM()
  31. {
  32. static::bootKernel();
  33. $translator = static::$container->get(TranslatorInterface::class);
  34. I18nHelper::setTranslator($translator);
  35. static::assertSame('test string', _m('test string'));
  36. $apples = [1 => '1 apple', '# apples'];
  37. static::assertSame('-42 apples', _m($apples, ['count' => -42]));
  38. static::assertSame('0 apples', _m($apples, ['count' => 0]));
  39. static::assertSame('1 apple', _m($apples, ['count' => 1]));
  40. static::assertSame('2 apples', _m($apples, ['count' => 2]));
  41. static::assertSame('42 apples', _m($apples, ['count' => 42]));
  42. $apples = [0 => 'no apples', 1 => '1 apple', '# apples'];
  43. static::assertSame('no apples', _m($apples, ['count' => 0]));
  44. static::assertSame('1 apple', _m($apples, ['count' => 1]));
  45. static::assertSame('2 apples', _m($apples, ['count' => 2]));
  46. static::assertSame('42 apples', _m($apples, ['count' => 42]));
  47. $pronouns = ['she' => 'her apple', 'he' => 'his apple', 'they' => 'their apple'];
  48. static::assertSame('her apple', _m($pronouns, ['pronoun' => 'she']));
  49. static::assertSame('his apple', _m($pronouns, ['pronoun' => 'he']));
  50. static::assertSame('their apple', _m($pronouns, ['pronoun' => 'they']));
  51. // $this->assertThrows(\Exception::class,
  52. // function () use ($pronouns) { _m($pronouns, ['pronoun' => 'unknown']); });
  53. $pronouns = ['she' => 'her apple', 'he' => 'his apple', 'they' => 'their apple', 'someone\'s apple'];
  54. static::assertSame('someone\'s apple', _m($pronouns, ['pronoun' => 'unknown']));
  55. $complex = [
  56. 'she' => [1 => 'her apple', 'her # apples'],
  57. 'he' => [1 => 'his apple', 'his # apples'],
  58. ];
  59. static::assertSame('her apple', _m($complex, ['pronoun' => 'she', 'count' => 1]));
  60. static::assertSame('his apple', _m($complex, ['pronoun' => 'he', 'count' => 1]));
  61. static::assertSame('her 2 apples', _m($complex, ['pronoun' => 'she', 'count' => 2]));
  62. static::assertSame('his 2 apples', _m($complex, ['pronoun' => 'he', 'count' => 2]));
  63. static::assertSame('her 42 apples', _m($complex, ['pronoun' => 'she', 'count' => 42]));
  64. $complex = [
  65. 'she' => [1 => 'her apple', 'her # apples'],
  66. 'he' => [1 => 'his apple', 'his # apples'],
  67. 'their' => [1 => 'their apple', 'their # apples'],
  68. ];
  69. static::assertSame('her apple', _m($complex, ['pronoun' => 'she', 'count' => 1]));
  70. static::assertSame('his apple', _m($complex, ['pronoun' => 'he', 'count' => 1]));
  71. static::assertSame('her 2 apples', _m($complex, ['pronoun' => 'she', 'count' => 2]));
  72. static::assertSame('his 2 apples', _m($complex, ['pronoun' => 'he', 'count' => 2]));
  73. static::assertSame('her 42 apples', _m($complex, ['pronoun' => 'she', 'count' => 42]));
  74. static::assertSame('their apple', _m($complex, ['pronoun' => 'they', 'count' => 1]));
  75. static::assertSame('their 3 apples', _m($complex, ['pronoun' => 'they', 'count' => 3]));
  76. }
  77. }