PluginListCommandTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP : 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 Project
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Command;
  16. use Cake\Console\CommandInterface;
  17. use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
  18. use Cake\Core\Exception\MissingPluginException;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * PluginListCommandTest class.
  22. */
  23. class PluginListCommandTest extends TestCase
  24. {
  25. use ConsoleIntegrationTestTrait;
  26. protected string $pluginsListPath;
  27. protected string $pluginsConfigPath;
  28. protected string $originalPluginsConfigContent = '';
  29. /**
  30. * setUp method
  31. */
  32. public function setUp(): void
  33. {
  34. parent::setUp();
  35. $this->setAppNamespace();
  36. $this->pluginsListPath = ROOT . DS . 'cakephp-plugins.php';
  37. if (file_exists($this->pluginsListPath)) {
  38. unlink($this->pluginsListPath);
  39. }
  40. $this->pluginsConfigPath = CONFIG . DS . 'plugins.php';
  41. if (file_exists($this->pluginsConfigPath)) {
  42. $this->originalPluginsConfigContent = file_get_contents($this->pluginsConfigPath);
  43. }
  44. }
  45. protected function tearDown(): void
  46. {
  47. parent::tearDown();
  48. if (file_exists($this->pluginsListPath)) {
  49. unlink($this->pluginsListPath);
  50. }
  51. if (file_exists($this->pluginsConfigPath)) {
  52. file_put_contents($this->pluginsConfigPath, $this->originalPluginsConfigContent);
  53. }
  54. }
  55. /**
  56. * Test generating help succeeds
  57. */
  58. public function testHelp(): void
  59. {
  60. $this->exec('plugin list --help');
  61. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  62. $this->assertOutputContains('plugin list');
  63. }
  64. /**
  65. * Test plugin names are being displayed correctly
  66. */
  67. public function testList(): void
  68. {
  69. $file = <<<PHP
  70. <?php
  71. declare(strict_types=1);
  72. return [
  73. 'plugins' => [
  74. 'TestPlugin' => '/config/path',
  75. 'OtherPlugin' => '/config/path'
  76. ]
  77. ];
  78. PHP;
  79. file_put_contents($this->pluginsListPath, $file);
  80. $this->exec('plugin list');
  81. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  82. $this->assertOutputContains('TestPlugin');
  83. $this->assertOutputContains('OtherPlugin');
  84. }
  85. /**
  86. * Test empty plugins array
  87. */
  88. public function testListEmpty(): void
  89. {
  90. $file = <<<PHP
  91. <?php
  92. declare(strict_types=1);
  93. return [];
  94. PHP;
  95. file_put_contents($this->pluginsListPath, $file);
  96. $this->exec('plugin list');
  97. $this->assertExitCode(CommandInterface::CODE_ERROR);
  98. $this->assertErrorContains('No plugins have been found.');
  99. }
  100. /**
  101. * Test enabled plugins are being flagged as enabled
  102. */
  103. public function testListEnabled(): void
  104. {
  105. $file = <<<PHP
  106. <?php
  107. declare(strict_types=1);
  108. return [
  109. 'plugins' => [
  110. 'TestPlugin' => '/config/path',
  111. 'OtherPlugin' => '/config/path'
  112. ]
  113. ];
  114. PHP;
  115. file_put_contents($this->pluginsListPath, $file);
  116. $config = <<<PHP
  117. <?php
  118. declare(strict_types=1);
  119. return [
  120. 'TestPlugin',
  121. 'OtherPlugin' => ['onlyDebug' => true, 'onlyCli' => true, 'optional' => true]
  122. ];
  123. PHP;
  124. file_put_contents($this->pluginsConfigPath, $config);
  125. $this->exec('plugin list');
  126. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  127. $this->assertOutputContains('TestPlugin');
  128. $this->assertOutputContains('OtherPlugin');
  129. }
  130. /**
  131. * Test listing unknown plugins throws an exception
  132. */
  133. public function testListUnknown(): void
  134. {
  135. $file = <<<PHP
  136. <?php
  137. declare(strict_types=1);
  138. return [
  139. 'plugins' => [
  140. 'TestPlugin' => '/config/path',
  141. 'OtherPlugin' => '/config/path'
  142. ]
  143. ];
  144. PHP;
  145. file_put_contents($this->pluginsListPath, $file);
  146. $config = <<<PHP
  147. <?php
  148. declare(strict_types=1);
  149. return [
  150. 'Unknown'
  151. ];
  152. PHP;
  153. file_put_contents($this->pluginsConfigPath, $config);
  154. $this->expectException(MissingPluginException::class);
  155. $this->expectExceptionMessage('Plugin `Unknown` could not be found.');
  156. $this->exec('plugin list');
  157. }
  158. /**
  159. * Test listing vendor plugins with versions
  160. */
  161. public function testListWithVersions(): void
  162. {
  163. $file = <<<PHP
  164. <?php
  165. declare(strict_types=1);
  166. return [
  167. 'plugins' => [
  168. 'Chronos' => ROOT . '/vendor/cakephp/chronos',
  169. 'CodeSniffer' => ROOT . '/vendor/cakephp/cakephp-codesniffer'
  170. ]
  171. ];
  172. PHP;
  173. file_put_contents($this->pluginsListPath, $file);
  174. $config = <<<PHP
  175. <?php
  176. declare(strict_types=1);
  177. return [
  178. 'Chronos',
  179. 'CodeSniffer'
  180. ];
  181. PHP;
  182. file_put_contents($this->pluginsConfigPath, $config);
  183. $path = ROOT . DS . 'tests' . DS . 'composer.lock';
  184. $this->exec(sprintf('plugin list --composer-path="%s"', $path));
  185. $this->assertOutputContains('| Chronos | X | | | | 3.0.4 |');
  186. $this->assertOutputContains('| CodeSniffer | X | | | | 5.1.1 |');
  187. }
  188. }