I18nCommandTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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->useCommandRunner();
  38. $this->setAppNamespace();
  39. }
  40. /**
  41. * Teardown
  42. */
  43. public function tearDown(): void
  44. {
  45. parent::tearDown();
  46. $deDir = $this->localeDir . 'de_DE' . DS;
  47. if (file_exists($this->localeDir . 'default.pot')) {
  48. unlink($this->localeDir . 'default.pot');
  49. unlink($this->localeDir . 'cake.pot');
  50. }
  51. if (file_exists($deDir . 'default.po')) {
  52. unlink($deDir . 'default.po');
  53. unlink($deDir . 'cake.po');
  54. }
  55. }
  56. /**
  57. * Tests that init() creates the PO files from POT files.
  58. */
  59. public function testInit(): void
  60. {
  61. $deDir = $this->localeDir . 'de_DE' . DS;
  62. if (!is_dir($deDir)) {
  63. mkdir($deDir, 0770, true);
  64. }
  65. file_put_contents($this->localeDir . 'default.pot', 'Testing POT file.');
  66. file_put_contents($this->localeDir . 'cake.pot', 'Testing POT file.');
  67. if (file_exists($deDir . 'default.po')) {
  68. unlink($deDir . 'default.po');
  69. }
  70. if (file_exists($deDir . 'cake.po')) {
  71. unlink($deDir . 'cake.po');
  72. }
  73. $this->exec('i18n init --verbose', [
  74. 'de_DE',
  75. $this->localeDir,
  76. ]);
  77. $this->assertExitSuccess();
  78. $this->assertOutputContains('Generated 2 PO files');
  79. $this->assertFileExists($deDir . 'default.po');
  80. $this->assertFileExists($deDir . 'cake.po');
  81. }
  82. /**
  83. * Tests that init() creates the PO files from POT files when App.path.locales contains an associative array
  84. */
  85. public function testInitWithAssociativePaths(): void
  86. {
  87. $deDir = $this->localeDir . 'de_DE' . DS;
  88. if (!is_dir($deDir)) {
  89. mkdir($deDir, 0770, true);
  90. }
  91. file_put_contents($this->localeDir . 'default.pot', 'Testing POT file.');
  92. file_put_contents($this->localeDir . 'cake.pot', 'Testing POT file.');
  93. if (file_exists($deDir . 'default.po')) {
  94. unlink($deDir . 'default.po');
  95. }
  96. if (file_exists($deDir . 'cake.po')) {
  97. unlink($deDir . 'cake.po');
  98. }
  99. Configure::write('App.paths.locales', ['customKey' => TEST_APP . 'resources' . DS . 'locales' . DS]);
  100. $this->exec('i18n init --verbose', [
  101. 'de_DE',
  102. $this->localeDir,
  103. ]);
  104. $this->assertExitSuccess();
  105. $this->assertOutputContains('Generated 2 PO files');
  106. $this->assertFileExists($deDir . 'default.po');
  107. $this->assertFileExists($deDir . 'cake.po');
  108. }
  109. /**
  110. * Test that the option parser is shaped right.
  111. */
  112. public function testGetOptionParser(): void
  113. {
  114. $this->exec('i18n -h');
  115. $this->assertExitSuccess();
  116. $this->assertOutputContains('cake i18n');
  117. }
  118. /**
  119. * Tests main interactive mode
  120. */
  121. public function testInteractiveQuit(): void
  122. {
  123. $this->exec('i18n', ['q']);
  124. $this->assertExitSuccess();
  125. }
  126. /**
  127. * Tests main interactive mode
  128. */
  129. public function testInteractiveHelp(): void
  130. {
  131. $this->exec('i18n', ['h', 'q']);
  132. $this->assertExitSuccess();
  133. $this->assertOutputContains('cake i18n');
  134. }
  135. /**
  136. * Tests main interactive mode
  137. */
  138. public function testInteractiveInit(): void
  139. {
  140. $this->exec('i18n', [
  141. 'i',
  142. 'x',
  143. ]);
  144. $this->assertExitError();
  145. $this->assertErrorContains('Invalid language code');
  146. }
  147. }