UnloadTaskTest.php 6.5 KB

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