UnloadTaskTest.php 6.7 KB

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