PluginLoadCommandTest.php 3.7 KB

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