UnloadTaskTest.php 5.2 KB

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