PluginLoadCommandTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Plugin;
  19. use Cake\Routing\Router;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * PluginLoadCommandTest class.
  23. */
  24. class PluginLoadCommandTest extends TestCase
  25. {
  26. use ConsoleIntegrationTestTrait;
  27. /**
  28. * @var string
  29. */
  30. protected $configFile;
  31. /**
  32. * @var string
  33. */
  34. protected $originalContent;
  35. /**
  36. * setUp method
  37. */
  38. public function setUp(): void
  39. {
  40. parent::setUp();
  41. $this->configFile = CONFIG . 'plugins.php';
  42. $this->originalContent = file_get_contents($this->configFile);
  43. $this->setAppNamespace();
  44. }
  45. /**
  46. * tearDown method
  47. */
  48. public function tearDown(): void
  49. {
  50. parent::tearDown();
  51. file_put_contents($this->configFile, $this->originalContent);
  52. }
  53. /**
  54. * Test generating help succeeds
  55. */
  56. public function testHelp(): void
  57. {
  58. $this->exec('plugin load --help');
  59. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  60. $this->assertOutputContains('plugin load');
  61. }
  62. /**
  63. * Test loading a plugin modifies the config file
  64. */
  65. public function testLoad(): void
  66. {
  67. $this->exec('plugin load TestPlugin');
  68. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  69. Plugin::getCollection()->remove('TestPlugin');
  70. // Needed to not have duplicate named routes
  71. Router::reload();
  72. $this->exec('plugin load TestPluginTwo --no-bootstrap --no-console --no-middleware --no-routes --no-services');
  73. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  74. Plugin::getCollection()->remove('TestPlugin');
  75. // Needed to not have duplicate named routes
  76. Router::reload();
  77. $this->exec('plugin load Company/TestPluginThree --only-debug --only-cli');
  78. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  79. $config = include $this->configFile;
  80. $this->assertTrue(isset($config['TestPlugin']));
  81. $this->assertTrue(isset($config['TestPluginTwo']));
  82. $this->assertTrue(isset($config['Company/TestPluginThree']));
  83. $this->assertSame(['onlyDebug' => true, 'onlyCli' => true], $config['Company/TestPluginThree']);
  84. $this->assertSame(
  85. ['bootstrap' => false, 'console' => false, 'middleware' => false, 'routes' => false, 'services' => false],
  86. $config['TestPluginTwo']
  87. );
  88. }
  89. /**
  90. * Test loading an unknown plugin
  91. */
  92. public function testLoadUnknownPlugin(): void
  93. {
  94. $this->exec('plugin load NopeNotThere');
  95. $this->assertExitCode(CommandInterface::CODE_ERROR);
  96. $this->assertErrorContains('Plugin `NopeNotThere` could not be found');
  97. $config = include $this->configFile;
  98. $this->assertFalse(isset($config['NopeNotThere']));
  99. }
  100. /**
  101. * Test loading optional plugin
  102. */
  103. public function testLoadOptionalPlugin(): void
  104. {
  105. $this->exec('plugin load NopeNotThere --optional');
  106. $config = include $this->configFile;
  107. $this->assertTrue(isset($config['NopeNotThere']));
  108. $this->assertSame(['optional' => true], $config['NopeNotThere']);
  109. }
  110. }