PluginUnloadCommandTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell\Task;
  16. use Cake\Console\Command;
  17. use Cake\TestSuite\ConsoleIntegrationTestTrait;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * PluginUnloadCommandTest class
  21. */
  22. class PluginUnloadCommandTest extends TestCase
  23. {
  24. use ConsoleIntegrationTestTrait;
  25. /**
  26. * @var string
  27. */
  28. protected $app;
  29. /**
  30. * @var string
  31. */
  32. protected $originalAppContent;
  33. /**
  34. * setUp method
  35. *
  36. * @return void
  37. */
  38. public function setUp(): void
  39. {
  40. parent::setUp();
  41. $this->app = APP . DS . 'Application.php';
  42. $this->originalAppContent = file_get_contents($this->app);
  43. $this->useCommandRunner();
  44. $this->setAppNamespace();
  45. }
  46. /**
  47. * tearDown method
  48. *
  49. * @return void
  50. */
  51. public function tearDown(): void
  52. {
  53. parent::tearDown();
  54. $this->clearPlugins();
  55. file_put_contents($this->app, $this->originalAppContent);
  56. }
  57. /**
  58. * testUnload
  59. *
  60. * @return void
  61. */
  62. public function testUnload()
  63. {
  64. $plugin1 = "\$this->addPlugin('TestPlugin', ['bootstrap' => false, 'routes' => false]);";
  65. $plugin2 = "\$this->addPlugin('TestPluginTwo', ['bootstrap' => false, 'routes' => false]);";
  66. $this->addPluginToApp($plugin1);
  67. $this->addPluginToApp($plugin2);
  68. $contents = file_get_contents($this->app);
  69. $this->assertStringContainsString($plugin1, $contents);
  70. $this->exec('plugin unload TestPlugin');
  71. $this->assertExitCode(Command::CODE_SUCCESS);
  72. $contents = file_get_contents($this->app);
  73. $this->assertStringNotContainsString($plugin1, $contents);
  74. $this->assertStringContainsString($plugin2, $contents);
  75. }
  76. /**
  77. * Data provider for various forms.
  78. *
  79. * @return array
  80. */
  81. public function variantProvider()
  82. {
  83. return [
  84. // $this->addPlugin('TestPlugin', [
  85. // 'bootstrap' => false
  86. // ]);
  87. [" \$this->addPlugin('TestPlugin', [\n\t'bootstrap' => false\n]);\n"],
  88. // $this->addPlugin(
  89. // 'TestPlugin',
  90. // [ 'bootstrap' => false]
  91. // );
  92. [" \$this->addPlugin(\n\t'TestPlugin',\n\t[ 'bootstrap' => false]\n);\n"],
  93. // $this->addPlugin(
  94. // 'Foo',
  95. // [
  96. // 'bootstrap' => false
  97. // ]
  98. // );
  99. [" \$this->addPlugin(\n\t'TestPlugin',\n\t[\n\t\t'bootstrap' => false\n\t]\n);\n"],
  100. // $this->addPlugin('Test', [
  101. // 'bootstrap' => true,
  102. // 'routes' => true
  103. // ]);
  104. [" \$this->addPlugin('TestPlugin', [\n\t'bootstrap' => true,\n\t'routes' => true\n]);\n"],
  105. // $this->addPlugin('Test',
  106. // [
  107. // 'bootstrap' => true,
  108. // 'routes' => true
  109. // ]
  110. // );
  111. [" \$this->addPlugin('TestPlugin',\n\t[\n\t\t'bootstrap' => true,\n\t\t'routes' => true\n\t]\n);\n"],
  112. // $this->addPlugin('Test',
  113. // [
  114. //
  115. // ]
  116. // );
  117. [" \$this->addPlugin('TestPlugin',\n\t[\n\t\n\t]\n);\n"],
  118. // $this->addPlugin('Test');
  119. [" \$this->addPlugin('TestPlugin');\n"],
  120. // $this->addPlugin('Test', ['bootstrap' => true, 'route' => false]);
  121. [" \$this->addPlugin('TestPlugin', ['bootstrap' => true, 'route' => false]);\n"],
  122. ];
  123. }
  124. /**
  125. * This method will tests multiple notations of plugin loading in the application class
  126. *
  127. * @dataProvider variantProvider
  128. * @return void
  129. */
  130. public function testRegularExpressionsApplication($content)
  131. {
  132. $this->addPluginToApp($content);
  133. $this->exec('plugin unload TestPlugin');
  134. $this->assertExitCode(Command::CODE_SUCCESS);
  135. $result = file_get_contents($this->app);
  136. $this->assertStringNotContainsString("addPlugin('TestPlugin'", $result);
  137. $this->assertNotRegexp("/this\-\>addPlugin\([\'\"]TestPlugin'[\'\"][^\)]*\)\;/mi", $result);
  138. }
  139. /**
  140. * _addPluginToApp
  141. *
  142. * Quick method to add a plugin to the Application file.
  143. * This is useful for the tests
  144. *
  145. * @param string $insert The addPlugin line to add.
  146. * @return void
  147. */
  148. protected function addPluginToApp($insert)
  149. {
  150. $contents = file_get_contents($this->app);
  151. $contents = preg_replace('/(function bootstrap\(\)(?:\s*)\:(?:\s*)void(?:\s+)\{)/m', '$1' . $insert, $contents);
  152. file_put_contents($this->app, $contents);
  153. }
  154. }