I18nTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Tests KO7 i18n class
  4. *
  5. * @group ko7
  6. * @group ko7.core
  7. * @group ko7.core.i18n
  8. *
  9. * @package KO7
  10. * @category Tests
  11. *
  12. * , Jeremy Bush <contractfrombelow@gmail.com>
  13. * @copyright (c) 2008´- 2016 Kohana Team
  14. * @copyright (c) since 2018 Koseven Team
  15. * @license https://koseven.dev/LICENSE
  16. */
  17. class KO7_I18nTest extends Unittest_TestCase {
  18. /**
  19. * Default values for the environment, see setEnvironment
  20. * @var array
  21. */
  22. protected $environmentDefault = [
  23. 'I18n::$lang' => 'en-us'
  24. ];
  25. /**
  26. * Provides test data for test_lang()
  27. *
  28. * @return array
  29. */
  30. public function provider_lang() : array
  31. {
  32. return [
  33. // $input, $expected_result
  34. [
  35. NULL, 'en-us'
  36. ],
  37. [
  38. 'es-es', 'es-es'
  39. ],
  40. ];
  41. }
  42. /**
  43. * Tests I18n::lang()
  44. *
  45. * @dataProvider provider_lang
  46. *
  47. * @param boolean $input Input for I18n::lang
  48. * @param boolean $expected Output for I18n::lang
  49. */
  50. public function test_lang($input, $expected)
  51. {
  52. self::assertSame($expected, I18n::lang($input));
  53. self::assertSame($expected, I18n::lang());
  54. }
  55. /**
  56. * Provides test data for test_get()
  57. *
  58. * @return array
  59. */
  60. public function provider_get() : array
  61. {
  62. return [
  63. // $lang, $input, $expected
  64. ['en-us', 'Hello, world!', 'Hello, world!'],
  65. ['es-es', 'Hello, world!', '¡Hola, mundo!'],
  66. ['fr-fr', 'Hello, world!', 'Bonjour, monde!'],
  67. ];
  68. }
  69. /**
  70. * Tests i18n::get()
  71. *
  72. * @dataProvider provider_get
  73. *
  74. * @param string $lang Target Language to translate to
  75. * @param string $input Input Translation String
  76. * @param string $expected Expected Result
  77. */
  78. public function test_get(string $lang, string $input, string $expected)
  79. {
  80. // Set Language
  81. I18n::lang($lang);
  82. // Test I18n::get function
  83. self::assertSame($expected, I18n::get($input));
  84. // Test I18n::get function with source language same as target language (we don't expect to be translated)
  85. self::assertSame($input, I18n::get($input, $lang, $lang));
  86. // Test __() function
  87. self::assertSame($expected, __($input));
  88. // Test __() function with source language (Note: we don't expect the string to be translated)
  89. self::assertSame($input, __($input, NULL, $lang));
  90. // Test I18n::get function with target language passed as variable
  91. I18n::lang('en-us');
  92. self::assertSame($expected, I18n::get($input, $lang));
  93. }
  94. /**
  95. * @return array
  96. */
  97. public function provider_get_values() :array
  98. {
  99. return [
  100. // $lang, $input, $values, $expected
  101. ['en-us', 'Good Morning :name!', [':name' => 'Koseven'], 'Good Morning Koseven!'],
  102. ['es-es', 'Good Morning :name!', [':name' => 'Koseven'], 'Buenos dias Koseven!'],
  103. ['fr-fr', 'Good Morning :name!', [':name' => 'Koseven'], 'Bonjour Koseven!'],
  104. ];
  105. }
  106. /**
  107. * Tests i18n::get with values passed to replace
  108. *
  109. * @dataProvider provider_get_values
  110. *
  111. * @param string $lang Target Language to translate to
  112. * @param string $input Input Translation String
  113. * @param array $values Values to replace in translated string
  114. * @param string $expected Expected Result
  115. */
  116. public function test_get_values(string $lang, string $input, array $values, string $expected)
  117. {
  118. // Set Language
  119. I18n::lang($lang);
  120. // Test I18n::get function with values to replace
  121. self::assertSame($expected, I18n::get([$input, $values]));
  122. // Test __() function with values to replace
  123. self::assertSame($expected, __($input, $values));
  124. // Don't expect string to be translated but expect values to be re-placed
  125. self::assertSame('Good Morning Koseven!', I18n::get([$input, $values], $lang, $lang));
  126. }
  127. }