UnloadTaskTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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\Core\Plugin;
  16. use Cake\Filesystem\File;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * UnloadTaskTest class
  20. */
  21. class UnloadTaskTest extends TestCase
  22. {
  23. /**
  24. * @var \Cake\Shell\Task\UnloadTask|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $Task;
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->Task = $this->getMockBuilder('Cake\Shell\Task\UnloadTask')
  39. ->setMethods(['in', 'out', 'err', '_stop'])
  40. ->setConstructorArgs([$this->io])
  41. ->getMock();
  42. $this->bootstrap = ROOT . DS . 'config' . DS . 'bootstrap.php';
  43. $bootstrap = new File($this->bootstrap, false);
  44. $this->originalBootstrapContent = $bootstrap->read();
  45. }
  46. /**
  47. * tearDown method
  48. *
  49. * @return void
  50. */
  51. public function tearDown()
  52. {
  53. parent::tearDown();
  54. unset($this->shell);
  55. Plugin::unload();
  56. $bootstrap = new File($this->bootstrap, false);
  57. $bootstrap->write($this->originalBootstrapContent);
  58. }
  59. /**
  60. * testUnload
  61. *
  62. * @return void
  63. */
  64. public function testUnload()
  65. {
  66. $bootstrap = new File($this->bootstrap, false);
  67. $this->_addPluginToBootstrap('TestPlugin');
  68. $this->_addPluginToBootstrap('TestPluginSecond');
  69. $expected = "Plugin::load('TestPlugin', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);";
  70. $this->assertContains($expected, $bootstrap->read());
  71. $this->Task->params = [
  72. 'cli' => false
  73. ];
  74. $action = $this->Task->main('TestPlugin');
  75. $this->assertTrue($action);
  76. $expected = "Plugin::load('TestPlugin', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);";
  77. $this->assertNotContains($expected, $bootstrap->read());
  78. $expected = "Plugin::load('TestPluginSecond', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);";
  79. $this->assertContains($expected, $bootstrap->read());
  80. }
  81. /**
  82. * testRegularExpressions
  83. *
  84. * This method will tests multiple notations of plugin loading.
  85. */
  86. public function testRegularExpressions()
  87. {
  88. $bootstrap = new File($this->bootstrap, false);
  89. $this->Task->params = [
  90. 'cli' => false
  91. ];
  92. // Plugin::load('TestPlugin', [
  93. // 'bootstrap' => false
  94. // ]);
  95. $bootstrap->append("\nPlugin::load('TestPlugin', [\n\t'bootstrap' => false\n]);\n");
  96. $this->Task->main('TestPlugin');
  97. $this->assertNotContains("Plugin::load('TestPlugin', [\n\t'bootstrap' => false\n]);", $bootstrap->read());
  98. $this->_clearBootstrap();
  99. // Plugin::load(
  100. // 'TestPlugin',
  101. // [ 'bootstrap' => false]
  102. // );
  103. $bootstrap->append("\nPlugin::load(\n\t'TestPlugin',\n\t[ 'bootstrap' => false]\n);\n");
  104. $this->Task->main('TestPlugin');
  105. $this->assertNotContains("Plugin::load(\n\t'TestPlugin',\n\t[ 'bootstrap' => false]\n);", $bootstrap->read());
  106. $this->_clearBootstrap();
  107. // Plugin::load(
  108. // 'Foo',
  109. // [
  110. // 'bootstrap' => false
  111. // ]
  112. // );
  113. $bootstrap->append("\nPlugin::load(\n\t'TestPlugin',\n\t[\n\t\t'bootstrap' => false\n\t]\n);\n");
  114. $this->Task->main('TestPlugin');
  115. $this->assertNotContains("Plugin::load(\n\t'TestPlugin',\n\t[\n\t\t'bootstrap' => false\n\t]\n);", $bootstrap->read());
  116. $this->_clearBootstrap();
  117. // Plugin::load('Test', [
  118. // 'autoload' => false,
  119. // 'bootstrap' => true,
  120. // 'routes' => true
  121. // ]);
  122. $bootstrap->append("\nPlugin::load('TestPlugin', [\n\t'autoload' => false,\n\t'bootstrap' => true,\n\t'routes' => true\n]);\n");
  123. $this->Task->main('TestPlugin');
  124. $this->assertNotContains("Plugin::load('TestPlugin', [\n\t'autoload' => false,\n\t'bootstrap' => true,\n\t'routes' => true\n]);", $bootstrap->read());
  125. $this->_clearBootstrap();
  126. // Plugin::load('Test',
  127. // [
  128. // 'bootstrap' => true,
  129. // 'routes' => true
  130. // ]
  131. // );
  132. $bootstrap->append("\nPlugin::load('TestPlugin',\n\t[\n\t\t'bootstrap' => true,\n\t\t'routes' => true\n\t]\n);\n");
  133. $this->Task->main('TestPlugin');
  134. $this->assertNotContains("Plugin::load('TestPlugin',\n\t[\n\t\t'bootstrap' => true,\n\t\t'routes' => true\n\t]\n);", $bootstrap->read());
  135. $this->_clearBootstrap();
  136. // Plugin::load('Test',
  137. // [
  138. //
  139. // ]
  140. // );
  141. $bootstrap->append("\nPlugin::load('TestPlugin',\n\t[\n\t\n\t]\n);\n");
  142. $this->Task->main('TestPlugin');
  143. $this->assertNotContains("Plugin::load('TestPlugin',\n\t[\n\t\n\t]\n);", $bootstrap->read());
  144. $this->_clearBootstrap();
  145. // Plugin::load('Test');
  146. $bootstrap->append("\nPlugin::load('TestPlugin');\n");
  147. $this->Task->main('TestPlugin');
  148. $this->assertNotContains("Plugin::load('TestPlugin');", $bootstrap->read());
  149. $this->_clearBootstrap();
  150. // Plugin::load('Test', ['bootstrap' => true, 'route' => false]);
  151. $bootstrap->append("\nPlugin::load('TestPlugin', ['bootstrap' => true, 'route' => false]);\n");
  152. $this->Task->main('TestPlugin');
  153. $this->assertNotContains("Plugin::load('TestPlugin', ['bootstrap' => true, 'route' => false]);", $bootstrap->read());
  154. }
  155. /**
  156. * _addPluginToBootstrap
  157. *
  158. * Quick method to add a plugin to the bootstrap file.
  159. * This is useful for the tests
  160. *
  161. * @param string $name
  162. */
  163. protected function _addPluginToBootstrap($name)
  164. {
  165. $bootstrap = new File($this->bootstrap, false);
  166. $bootstrap->append("\n\nPlugin::load('$name', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);\n");
  167. }
  168. /**
  169. * clearBootstrap
  170. *
  171. * Helper to clear the bootstrap file.
  172. *
  173. * @return void
  174. */
  175. protected function _clearBootstrap()
  176. {
  177. $bootstrap = new File($this->bootstrap, false);
  178. $bootstrap->write($this->originalBootstrapContent);
  179. }
  180. }