PluginUnloadCommandTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\TestSuite\ConsoleIntegrationTestTrait;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * PluginUnloadCommandTest class
  21. */
  22. class PluginUnloadCommandTest extends TestCase
  23. {
  24. use ConsoleIntegrationTestTrait;
  25. /**
  26. * @var string
  27. */
  28. protected $app;
  29. /**
  30. * @var string
  31. */
  32. protected $originalAppContent;
  33. /**
  34. * setUp method
  35. */
  36. public function setUp(): void
  37. {
  38. parent::setUp();
  39. $this->app = APP . DS . 'Application.php';
  40. $this->originalAppContent = file_get_contents($this->app);
  41. $this->setAppNamespace();
  42. }
  43. /**
  44. * tearDown method
  45. */
  46. public function tearDown(): void
  47. {
  48. parent::tearDown();
  49. $this->clearPlugins();
  50. file_put_contents($this->app, $this->originalAppContent);
  51. }
  52. /**
  53. * testUnload
  54. */
  55. public function testUnload(): void
  56. {
  57. $plugin1 = "\$this->addPlugin('TestPlugin', ['bootstrap' => false, 'routes' => false]);";
  58. $plugin2 = "\$this->addPlugin('TestPluginTwo', ['bootstrap' => false, 'routes' => false]);";
  59. $this->addPluginToApp($plugin1);
  60. $this->addPluginToApp($plugin2);
  61. $this->exec('plugin unload TestPlugin');
  62. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  63. $contents = file_get_contents($this->app);
  64. $this->assertStringNotContainsString($plugin1, $contents);
  65. $this->assertStringContainsString($plugin2, $contents);
  66. }
  67. /**
  68. * test removing the first plugin leaves the second behind.
  69. */
  70. public function testUnloadFirstPlugin(): void
  71. {
  72. $plugin1 = "\$this->addPlugin('TestPlugin');";
  73. $plugin2 = "\$this->addPlugin('Vendor/TestPluginTwo');";
  74. $this->addPluginToApp($plugin1);
  75. $this->addPluginToApp($plugin2);
  76. $this->exec('plugin unload Vendor/TestPluginTwo');
  77. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  78. $contents = file_get_contents($this->app);
  79. $this->assertStringNotContainsString($plugin2, $contents);
  80. $this->assertStringContainsString($plugin1, $contents);
  81. }
  82. /**
  83. * Data provider for various forms.
  84. *
  85. * @return array
  86. */
  87. public function variantProvider(): array
  88. {
  89. return [
  90. // $this->addPlugin('TestPlugin', [
  91. // 'bootstrap' => false
  92. // ]);
  93. [" \$this->addPlugin('TestPlugin', [\n\t'bootstrap' => false\n]);\n"],
  94. // $this->addPlugin(
  95. // 'TestPlugin',
  96. // [ 'bootstrap' => false]
  97. // );
  98. [" \$this->addPlugin(\n\t'TestPlugin',\n\t[ 'bootstrap' => false]\n);\n"],
  99. // $this->addPlugin(
  100. // 'Foo',
  101. // [
  102. // 'bootstrap' => false
  103. // ]
  104. // );
  105. [" \$this->addPlugin(\n\t'TestPlugin',\n\t[\n\t\t'bootstrap' => false\n\t]\n);\n"],
  106. // $this->addPlugin('Test', [
  107. // 'bootstrap' => true,
  108. // 'routes' => true
  109. // ]);
  110. [" \$this->addPlugin('TestPlugin', [\n\t'bootstrap' => true,\n\t'routes' => true\n]);\n"],
  111. // $this->addPlugin('Test',
  112. // [
  113. // 'bootstrap' => true,
  114. // 'routes' => true
  115. // ]
  116. // );
  117. [" \$this->addPlugin('TestPlugin',\n\t[\n\t\t'bootstrap' => true,\n\t\t'routes' => true\n\t]\n);\n"],
  118. // $this->addPlugin('Test',
  119. // [
  120. //
  121. // ]
  122. // );
  123. [" \$this->addPlugin('TestPlugin',\n\t[\n\t\n\t]\n);\n"],
  124. // $this->addPlugin('Test');
  125. [" \$this->addPlugin('TestPlugin');\n"],
  126. // $this->addPlugin('Test', ['bootstrap' => true, 'route' => false]);
  127. [" \$this->addPlugin('TestPlugin', ['bootstrap' => true, 'route' => false]);\n"],
  128. ];
  129. }
  130. /**
  131. * This method will tests multiple notations of plugin loading in the application class
  132. *
  133. * @dataProvider variantProvider
  134. */
  135. public function testRegularExpressionsApplication(string $content): void
  136. {
  137. $this->addPluginToApp($content);
  138. $this->exec('plugin unload TestPlugin');
  139. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  140. $result = file_get_contents($this->app);
  141. $this->assertStringNotContainsString("addPlugin('TestPlugin'", $result);
  142. $this->assertDoesNotMatchRegularExpression("/this\-\>addPlugin\([\'\"]TestPlugin'[\'\"][^\)]*\)\;/mi", $result);
  143. }
  144. /**
  145. * _addPluginToApp
  146. *
  147. * Quick method to add a plugin to the Application file.
  148. * This is useful for the tests
  149. *
  150. * @param string $insert The addPlugin line to add.
  151. */
  152. protected function addPluginToApp($insert): void
  153. {
  154. $contents = file_get_contents($this->app);
  155. $contents = preg_replace('/(function bootstrap\(\)(?:\s*)\:(?:\s*)void(?:\s+)\{)/m', "\$1\n " . $insert, $contents);
  156. file_put_contents($this->app, $contents);
  157. }
  158. }