I18nCommandTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.8
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Command;
  17. use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
  18. use Cake\Core\Configure;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * I18nCommand test.
  22. */
  23. class I18nCommandTest extends TestCase
  24. {
  25. use ConsoleIntegrationTestTrait;
  26. /**
  27. * @var string
  28. */
  29. protected $localeDir;
  30. /**
  31. * setup method
  32. */
  33. public function setUp(): void
  34. {
  35. parent::setUp();
  36. $this->localeDir = TMP . 'Locale' . DS;
  37. $this->setAppNamespace();
  38. }
  39. /**
  40. * Teardown
  41. */
  42. public function tearDown(): void
  43. {
  44. parent::tearDown();
  45. $deDir = $this->localeDir . 'de_DE' . DS;
  46. if (file_exists($this->localeDir . 'default.pot')) {
  47. unlink($this->localeDir . 'default.pot');
  48. unlink($this->localeDir . 'cake.pot');
  49. }
  50. if (file_exists($deDir . 'default.po')) {
  51. unlink($deDir . 'default.po');
  52. unlink($deDir . 'cake.po');
  53. }
  54. }
  55. /**
  56. * Tests that init() creates the PO files from POT files.
  57. */
  58. public function testInit(): void
  59. {
  60. $deDir = $this->localeDir . 'de_DE' . DS;
  61. if (!is_dir($deDir)) {
  62. mkdir($deDir, 0770, true);
  63. }
  64. file_put_contents($this->localeDir . 'default.pot', 'Testing POT file.');
  65. file_put_contents($this->localeDir . 'cake.pot', 'Testing POT file.');
  66. if (file_exists($deDir . 'default.po')) {
  67. unlink($deDir . 'default.po');
  68. }
  69. if (file_exists($deDir . 'cake.po')) {
  70. unlink($deDir . 'cake.po');
  71. }
  72. $this->exec('i18n init --verbose', [
  73. 'de_DE',
  74. $this->localeDir,
  75. ]);
  76. $this->assertExitSuccess();
  77. $this->assertOutputContains('Generated 2 PO files');
  78. $this->assertFileExists($deDir . 'default.po');
  79. $this->assertFileExists($deDir . 'cake.po');
  80. }
  81. /**
  82. * Tests that init() creates the PO files from POT files when App.path.locales contains an associative array
  83. */
  84. public function testInitWithAssociativePaths(): void
  85. {
  86. $deDir = $this->localeDir . 'de_DE' . DS;
  87. if (!is_dir($deDir)) {
  88. mkdir($deDir, 0770, true);
  89. }
  90. file_put_contents($this->localeDir . 'default.pot', 'Testing POT file.');
  91. file_put_contents($this->localeDir . 'cake.pot', 'Testing POT file.');
  92. if (file_exists($deDir . 'default.po')) {
  93. unlink($deDir . 'default.po');
  94. }
  95. if (file_exists($deDir . 'cake.po')) {
  96. unlink($deDir . 'cake.po');
  97. }
  98. Configure::write('App.paths.locales', ['customKey' => TEST_APP . 'resources' . DS . 'locales' . DS]);
  99. $this->exec('i18n init --verbose', [
  100. 'de_DE',
  101. $this->localeDir,
  102. ]);
  103. $this->assertExitSuccess();
  104. $this->assertOutputContains('Generated 2 PO files');
  105. $this->assertFileExists($deDir . 'default.po');
  106. $this->assertFileExists($deDir . 'cake.po');
  107. }
  108. /**
  109. * Test that the option parser is shaped right.
  110. */
  111. public function testGetOptionParser(): void
  112. {
  113. $this->exec('i18n -h');
  114. $this->assertExitSuccess();
  115. $this->assertOutputContains('cake i18n');
  116. }
  117. /**
  118. * Tests main interactive mode
  119. */
  120. public function testInteractiveQuit(): void
  121. {
  122. $this->exec('i18n', ['q']);
  123. $this->assertExitSuccess();
  124. }
  125. /**
  126. * Tests main interactive mode
  127. */
  128. public function testInteractiveHelp(): void
  129. {
  130. $this->exec('i18n', ['h', 'q']);
  131. $this->assertExitSuccess();
  132. $this->assertOutputContains('cake i18n');
  133. }
  134. /**
  135. * Tests main interactive mode
  136. */
  137. public function testInteractiveInit(): void
  138. {
  139. $this->exec('i18n', [
  140. 'i',
  141. 'x',
  142. ]);
  143. $this->assertExitError();
  144. $this->assertErrorContains('Invalid language code');
  145. }
  146. }