IcuFormatterTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\I18n;
  16. use Cake\I18n\Formatter\IcuFormatter;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * IcuFormatter tests
  20. */
  21. class IcuFormatterTest extends TestCase
  22. {
  23. /**
  24. * Tests that empty values can be used as formatting strings
  25. *
  26. * @return void
  27. */
  28. public function testFormatEmptyValues()
  29. {
  30. $formatter = new IcuFormatter();
  31. $this->assertEquals('', $formatter->format('en_US', '', []));
  32. }
  33. /**
  34. * Tests that variables are interpolated correctly
  35. *
  36. * @return void
  37. */
  38. public function testFormatSimple()
  39. {
  40. $formatter = new IcuFormatter();
  41. $this->assertEquals('Hello José', $formatter->format('en_US', 'Hello {0}', ['José']));
  42. $result = $formatter->format(
  43. '1 Orange',
  44. '{0, number} {1}',
  45. [1.0, 'Orange']
  46. );
  47. $this->assertEquals('1 Orange', $result);
  48. }
  49. /**
  50. * Tests that plurals can instead be selected using ICU's native selector
  51. *
  52. * @return void
  53. */
  54. public function testNativePluralSelection()
  55. {
  56. $formatter = new IcuFormatter();
  57. $locale = 'en_US';
  58. $string = '{0,plural,' .
  59. '=0{No fruits.}' .
  60. '=1{We have one fruit}' .
  61. 'other{We have {1} fruits}' .
  62. '}';
  63. $params = [0, 0];
  64. $expect = 'No fruits.';
  65. $actual = $formatter->format($locale, $string, $params);
  66. $this->assertSame($expect, $actual);
  67. $params = [1, 0];
  68. $expect = 'We have one fruit';
  69. $actual = $formatter->format($locale, $string, $params);
  70. $this->assertSame($expect, $actual);
  71. $params = [10, 10];
  72. $expect = 'We have 10 fruits';
  73. $actual = $formatter->format($locale, $string, $params);
  74. $this->assertSame($expect, $actual);
  75. }
  76. /**
  77. * Tests that passing a message in the wrong format will throw an exception
  78. *
  79. * @return void
  80. */
  81. public function testBadMessageFormat()
  82. {
  83. $this->expectException(\Exception::class);
  84. $this->expectExceptionMessage('msgfmt_create: message formatter');
  85. $this->skipIf(version_compare(PHP_VERSION, '7', '>='));
  86. $formatter = new IcuFormatter();
  87. $formatter->format('en_US', '{crazy format', ['some', 'vars']);
  88. }
  89. /**
  90. * Tests that passing a message in the wrong format will throw an exception
  91. *
  92. * @return void
  93. */
  94. public function testBadMessageFormatPHP7()
  95. {
  96. $this->expectException(\Exception::class);
  97. $this->skipIf(version_compare(PHP_VERSION, '7', '<'));
  98. $formatter = new IcuFormatter();
  99. $formatter->format('en_US', '{crazy format', ['some', 'vars']);
  100. }
  101. }