IntlFormatterTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Formatter;
  11. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  12. use Symfony\Component\Translation\Formatter\IntlFormatter;
  13. use Symfony\Component\Translation\Formatter\IntlFormatterInterface;
  14. /**
  15. * @requires extension intl
  16. */
  17. class IntlFormatterTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @dataProvider provideDataForFormat
  21. */
  22. public function testFormat($expected, $message, $arguments)
  23. {
  24. $this->assertEquals($expected, trim((new IntlFormatter())->formatIntl($message, 'en', $arguments)));
  25. }
  26. public function testInvalidFormat()
  27. {
  28. $this->expectException(InvalidArgumentException::class);
  29. (new IntlFormatter())->formatIntl('{foo', 'en', [2]);
  30. }
  31. public function testFormatWithNamedArguments()
  32. {
  33. if (version_compare(INTL_ICU_VERSION, '4.8', '<')) {
  34. $this->markTestSkipped('Format with named arguments can only be run with ICU 4.8 or higher and PHP >= 5.5');
  35. }
  36. $chooseMessage = <<<'_MSG_'
  37. {gender_of_host, select,
  38. female {{num_guests, plural, offset:1
  39. =0 {{host} does not give a party.}
  40. =1 {{host} invites {guest} to her party.}
  41. =2 {{host} invites {guest} and one other person to her party.}
  42. other {{host} invites {guest} as one of the # people invited to her party.}}}
  43. male {{num_guests, plural, offset:1
  44. =0 {{host} does not give a party.}
  45. =1 {{host} invites {guest} to his party.}
  46. =2 {{host} invites {guest} and one other person to his party.}
  47. other {{host} invites {guest} as one of the # people invited to his party.}}}
  48. other {{num_guests, plural, offset:1
  49. =0 {{host} does not give a party.}
  50. =1 {{host} invites {guest} to their party.}
  51. =2 {{host} invites {guest} and one other person to their party.}
  52. other {{host} invites {guest} as one of the # people invited to their party.}}}}
  53. _MSG_;
  54. $message = (new IntlFormatter())->formatIntl($chooseMessage, 'en', [
  55. 'gender_of_host' => 'male',
  56. 'num_guests' => 10,
  57. 'host' => 'Fabien',
  58. 'guest' => 'Guilherme',
  59. ]);
  60. $this->assertEquals('Fabien invites Guilherme as one of the 9 people invited to his party.', $message);
  61. }
  62. public function provideDataForFormat()
  63. {
  64. return [
  65. [
  66. 'There is one apple',
  67. 'There is one apple',
  68. [],
  69. ],
  70. [
  71. '4,560 monkeys on 123 trees make 37.073 monkeys per tree',
  72. '{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree',
  73. [4560, 123, 4560 / 123],
  74. ],
  75. ];
  76. }
  77. public function testPercentsAndBracketsAreTrimmed()
  78. {
  79. $formatter = new IntlFormatter();
  80. $this->assertInstanceof(IntlFormatterInterface::class, $formatter);
  81. $this->assertSame('Hello Fab', $formatter->formatIntl('Hello {name}', 'en', ['name' => 'Fab']));
  82. $this->assertSame('Hello Fab', $formatter->formatIntl('Hello {name}', 'en', ['%name%' => 'Fab']));
  83. $this->assertSame('Hello Fab', $formatter->formatIntl('Hello {name}', 'en', ['{{ name }}' => 'Fab']));
  84. }
  85. }