TranslatorTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\Language;
  12. use Carbon\AbstractTranslator;
  13. use Carbon\Carbon;
  14. use Carbon\Exceptions\ImmutableException;
  15. use Carbon\Exceptions\NotLocaleAwareException;
  16. use Carbon\Translator;
  17. use Carbon\TranslatorImmutable;
  18. use ReflectionMethod;
  19. use Tests\AbstractTestCase;
  20. class TranslatorTest extends AbstractTestCase
  21. {
  22. public function testSetLocale()
  23. {
  24. $currentLocale = setlocale(LC_TIME, '0');
  25. $currentLocaleAll = setlocale(LC_ALL, '0');
  26. $translator = new Translator('en_FooBar');
  27. $translator->setLocale('auto');
  28. $this->assertSame('en', $translator->getLocale());
  29. $translator = new Translator('en');
  30. $translator->setLocale('en_iso');
  31. $this->assertSame('en_ISO', $translator->getLocale());
  32. $translator = new Translator('fr');
  33. if (setlocale(LC_ALL, 'en_US.UTF-8', 'en_US.utf8', 'en_US', 'en_GB', 'en') === false ||
  34. setlocale(LC_TIME, 'en_US.UTF-8', 'en_US.utf8', 'en_US', 'en_GB', 'en') === false) {
  35. $this->markTestSkipped('testSetLocale test need en_US.UTF-8.');
  36. }
  37. $translator->setLocale('auto');
  38. $this->assertStringStartsWith('en', $translator->getLocale());
  39. setlocale(LC_ALL, $currentLocaleAll);
  40. setlocale(LC_TIME, $currentLocale);
  41. }
  42. public function testMethodsPriorities()
  43. {
  44. Carbon::setLocale('nl');
  45. $text = Carbon::parse('2019-08-06')->locale('en')->isoFormat('dddd D MMMM');
  46. $this->assertSame('Tuesday 6 August', $text);
  47. }
  48. public function testCompareChunkLists()
  49. {
  50. $method = new ReflectionMethod(AbstractTranslator::class, 'compareChunkLists');
  51. $method->setAccessible(true);
  52. $this->assertSame(20, $method->invoke(null, ['a', 'b'], ['a', 'b']));
  53. $this->assertSame(10, $method->invoke(null, ['a', 'b'], ['a', 'c']));
  54. $this->assertSame(10, $method->invoke(null, ['a'], ['a', 'c']));
  55. $this->assertSame(11, $method->invoke(null, ['a', 'b'], ['a']));
  56. $this->assertSame(10, $method->invoke(null, ['a'], ['a']));
  57. }
  58. public function testNotLocaleAwareException()
  59. {
  60. $exception = new NotLocaleAwareException('foobar');
  61. $this->assertSame(
  62. 'string does neither implements Symfony\Contracts\Translation\LocaleAwareInterface nor getLocale() method.',
  63. $exception->getMessage()
  64. );
  65. }
  66. public function testTranslatorImmutable()
  67. {
  68. $this->expectExceptionObject(
  69. new ImmutableException('setTranslations not allowed on '.TranslatorImmutable::class)
  70. );
  71. TranslatorImmutable::get('en')->setTranslations([]);
  72. }
  73. }