SprintfFormatterTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\I18n;
  16. use Cake\I18n\Formatter\SprintfFormatter;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * SprintfFormatter tests
  20. *
  21. */
  22. class SprintfFormatterTest extends TestCase {
  23. /**
  24. * Tests that variables are interpolated correctly
  25. *
  26. * @return void
  27. */
  28. public function testFormatSimple() {
  29. $formatter = new SprintfFormatter();
  30. $this->assertEquals('Hello José', $formatter->format('en_US', 'Hello %s', ['José']));
  31. $this->assertEquals('1 Orange', $formatter->format('en_US', '%d %s', [1, 'Orange']));
  32. }
  33. /**
  34. * Tests that plural forms are selected for the passed locale
  35. *
  36. * @return void
  37. */
  38. public function testFormatPlural() {
  39. $formatter = new SprintfFormatter();
  40. $messages = ['%d is 0', '%d is 1', '%d is 2', '%d is 3', '%d > 11'];
  41. $this->assertEquals('1 is 1', $formatter->format('ar', $messages, ['_count' => 1]));
  42. $this->assertEquals('2 is 2', $formatter->format('ar', $messages, ['_count' => 2]));
  43. $this->assertEquals('20 > 11', $formatter->format('ar', $messages, ['_count' => 20]));
  44. }
  45. /**
  46. * Tests that strings stored inside context namespaces can also be formatted
  47. *
  48. * @return void
  49. */
  50. public function testFormatWithContext() {
  51. $messages = [
  52. 'simple' => [
  53. '_context' => [
  54. 'context a' => 'Text "a" %s',
  55. 'context b' => 'Text "b" %s'
  56. ]
  57. ],
  58. 'complex' => [
  59. '_context' => [
  60. 'context b' => [
  61. 0 => 'Only one',
  62. 1 => 'there are %d'
  63. ]
  64. ]
  65. ]
  66. ];
  67. $formatter = new SprintfFormatter();
  68. $this->assertEquals(
  69. 'Text "a" is good',
  70. $formatter->format('en', $messages['simple'], ['_context' => 'context a', 'is good'])
  71. );
  72. $this->assertEquals(
  73. 'Text "b" is good',
  74. $formatter->format('en', $messages['simple'], ['_context' => 'context b', 'is good'])
  75. );
  76. $this->assertEquals(
  77. 'Text "a" is good',
  78. $formatter->format('en', $messages['simple'], ['is good'])
  79. );
  80. $this->assertEquals(
  81. 'Only one',
  82. $formatter->format('en', $messages['complex'], ['_context' => 'context b', '_count' => 1])
  83. );
  84. $this->assertEquals(
  85. 'there are 2',
  86. $formatter->format('en', $messages['complex'], ['_context' => 'context b', '_count' => 2])
  87. );
  88. }
  89. /**
  90. * Tests that it is possible to provide a singular fallback when passing a string message.
  91. * This is useful for getting quick feedback on the code during development instead of
  92. * having to provide all plural forms even for the default language
  93. *
  94. * @return void
  95. */
  96. public function testSingularFallback() {
  97. $formatter = new SprintfFormatter();
  98. $singular = 'one thing';
  99. $plural = 'many things';
  100. $this->assertEquals($singular, $formatter->format('en_US', $plural, ['_count' => 1, '_singular' => $singular]));
  101. $this->assertEquals($plural, $formatter->format('en_US', $plural, ['_count' => 2, '_singular' => $singular]));
  102. }
  103. }