UnloadTaskTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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\Shell\Task;
  16. use Cake\Console\Shell;
  17. use Cake\Core\Plugin;
  18. use Cake\Filesystem\File;
  19. use Cake\TestSuite\ConsoleIntegrationTestCase;
  20. /**
  21. * UnloadTaskTest class
  22. */
  23. class UnloadTaskTest extends ConsoleIntegrationTestCase
  24. {
  25. /**
  26. * @var string
  27. */
  28. protected $bootstrap;
  29. /**
  30. * @var string
  31. */
  32. protected $app;
  33. /**
  34. * @var string
  35. */
  36. protected $originalBootstrapContent;
  37. /**
  38. * @var string
  39. */
  40. protected $originalAppContent;
  41. /**
  42. * setUp method
  43. *
  44. * @return void
  45. */
  46. public function setUp()
  47. {
  48. parent::setUp();
  49. $this->bootstrap = ROOT . DS . 'config' . DS . 'bootstrap.php';
  50. $this->app = APP . DS . 'Application.php';
  51. $this->originalBootstrapContent = file_get_contents($this->bootstrap);
  52. $this->originalAppContent = file_get_contents($this->app);
  53. }
  54. /**
  55. * tearDown method
  56. *
  57. * @return void
  58. */
  59. public function tearDown()
  60. {
  61. parent::tearDown();
  62. unset($this->shell);
  63. Plugin::unload();
  64. file_put_contents($this->bootstrap, $this->originalBootstrapContent);
  65. file_put_contents($this->app, $this->originalAppContent);
  66. }
  67. /**
  68. * testUnload
  69. *
  70. * @return void
  71. */
  72. public function testUnload()
  73. {
  74. $this->_addPluginToBootstrap('TestPlugin');
  75. $this->_addPluginToBootstrap('TestPluginSecond');
  76. $contents = file_get_contents($this->bootstrap);
  77. $expected = "Plugin::load('TestPlugin', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);";
  78. $this->assertContains($expected, $contents);
  79. $this->exec('plugin unload --no_app TestPlugin');
  80. $this->assertExitCode(Shell::CODE_SUCCESS);
  81. $contents = file_get_contents($this->bootstrap);
  82. $expected = "Plugin::load('TestPlugin', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);";
  83. $this->assertNotContains($expected, $contents);
  84. $expected = "Plugin::load('TestPluginSecond', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);";
  85. $this->assertContains($expected, $contents);
  86. }
  87. /**
  88. * Data provider for various forms.
  89. *
  90. * @return array
  91. */
  92. public function variantProvider()
  93. {
  94. return [
  95. // Plugin::load('TestPlugin', [
  96. // 'bootstrap' => false
  97. // ]);
  98. ["\nPlugin::load('TestPlugin', [\n\t'bootstrap' => false\n]);\n"],
  99. // Plugin::load(
  100. // 'TestPlugin',
  101. // [ 'bootstrap' => false]
  102. // );
  103. ["\nPlugin::load(\n\t'TestPlugin',\n\t[ 'bootstrap' => false]\n);\n"],
  104. // Plugin::load(
  105. // 'Foo',
  106. // [
  107. // 'bootstrap' => false
  108. // ]
  109. // );
  110. ["\nPlugin::load(\n\t'TestPlugin',\n\t[\n\t\t'bootstrap' => false\n\t]\n);\n"],
  111. // Plugin::load('Test', [
  112. // 'autoload' => false,
  113. // 'bootstrap' => true,
  114. // 'routes' => true
  115. // ]);
  116. ["\nPlugin::load('TestPlugin', [\n\t'autoload' => false,\n\t'bootstrap' => true,\n\t'routes' => true\n]);\n"],
  117. // Plugin::load('Test',
  118. // [
  119. // 'bootstrap' => true,
  120. // 'routes' => true
  121. // ]
  122. // );
  123. ["\nPlugin::load('TestPlugin',\n\t[\n\t\t'bootstrap' => true,\n\t\t'routes' => true\n\t]\n);\n"],
  124. // Plugin::load('Test',
  125. // [
  126. //
  127. // ]
  128. // );
  129. ["\nPlugin::load('TestPlugin',\n\t[\n\t\n\t]\n);\n"],
  130. // Plugin::load('Test');
  131. ["\nPlugin::load('TestPlugin');\n"],
  132. // Plugin::load('Test', ['bootstrap' => true, 'route' => false]);
  133. ["\nPlugin::load('TestPlugin', ['bootstrap' => true, 'route' => false]);\n"],
  134. ];
  135. }
  136. /**
  137. * testRegularExpressions
  138. *
  139. * This method will tests multiple notations of plugin loading.
  140. *
  141. * @dataProvider variantProvider
  142. * @return void
  143. */
  144. public function testRegularExpressions($content)
  145. {
  146. $bootstrap = new File($this->bootstrap, false);
  147. $bootstrap->append($content);
  148. $this->exec('plugin unload --no_app TestPlugin');
  149. $this->assertExitCode(Shell::CODE_SUCCESS);
  150. $result = $bootstrap->read();
  151. $this->assertNotRegexp("/Plugin\:\:load\([\'\"]TestPlugin'[\'\"][^\)]*\)\;/mi", $result);
  152. }
  153. /**
  154. * This method will tests multiple notations of plugin loading in the application class
  155. *
  156. * @dataProvider variantProvider
  157. * @return void
  158. */
  159. public function testRegularExpressionsApplication($content)
  160. {
  161. $content = str_replace('Plugin::load', " \$this->addPlugin", $content);
  162. $this->addPluginToApp($content);
  163. $this->exec('plugin unload TestPlugin');
  164. $this->assertExitCode(Shell::CODE_SUCCESS);
  165. $result = file_get_contents($this->app);
  166. $this->assertNotContains("addPlugin('TestPlugin'", $result);
  167. $this->assertNotRegexp("/this\-\>addPlugin\([\'\"]TestPlugin'[\'\"][^\)]*\)\;/mi", $result);
  168. }
  169. /**
  170. * _addPluginToBootstrap
  171. *
  172. * Quick method to add a plugin to the bootstrap file.
  173. * This is useful for the tests
  174. *
  175. * @param string $name
  176. */
  177. protected function _addPluginToBootstrap($name)
  178. {
  179. $bootstrap = new File($this->bootstrap, false);
  180. $bootstrap->append("\n\nPlugin::load('$name', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);\n");
  181. }
  182. /**
  183. * _addPluginToApp
  184. *
  185. * Quick method to add a plugin to the bootstrap file.
  186. * This is useful for the tests
  187. *
  188. * @param string $insert The addPlugin line to add.
  189. */
  190. protected function addPluginToApp($insert)
  191. {
  192. $contents = file_get_contents($this->app);
  193. $contents = preg_replace('/(function bootstrap\(\)(?:\s+)\{)/m', '$1' . $insert, $contents);
  194. file_put_contents($this->app, $contents);
  195. }
  196. }