UnloadTaskTest.php 6.6 KB

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