PluginUnloadCommandTest.php 5.8 KB

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