UnloadTaskTest.php 6.6 KB

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