PluginUnloadCommandTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\Command;
  16. use Cake\Command\Command;
  17. use Cake\Console\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. public function setUp(): void
  37. {
  38. parent::setUp();
  39. $this->app = APP . DS . 'Application.php';
  40. $this->originalAppContent = file_get_contents($this->app);
  41. $this->useCommandRunner();
  42. $this->setAppNamespace();
  43. }
  44. /**
  45. * tearDown method
  46. */
  47. public function tearDown(): void
  48. {
  49. parent::tearDown();
  50. $this->clearPlugins();
  51. file_put_contents($this->app, $this->originalAppContent);
  52. }
  53. /**
  54. * testUnload
  55. */
  56. public function testUnload(): void
  57. {
  58. $plugin1 = "\$this->addPlugin('TestPlugin', ['bootstrap' => false, 'routes' => false]);";
  59. $plugin2 = "\$this->addPlugin('TestPluginTwo', ['bootstrap' => false, 'routes' => false]);";
  60. $this->addPluginToApp($plugin1);
  61. $this->addPluginToApp($plugin2);
  62. $this->exec('plugin unload TestPlugin');
  63. $this->assertExitCode(Command::CODE_SUCCESS);
  64. $contents = file_get_contents($this->app);
  65. $this->assertStringNotContainsString($plugin1, $contents);
  66. $this->assertStringContainsString($plugin2, $contents);
  67. }
  68. /**
  69. * test removing the first plugin leaves the second behind.
  70. */
  71. public function testUnloadFirstPlugin(): void
  72. {
  73. $plugin1 = "\$this->addPlugin('TestPlugin');";
  74. $plugin2 = "\$this->addPlugin('Vendor/TestPluginTwo');";
  75. $this->addPluginToApp($plugin1);
  76. $this->addPluginToApp($plugin2);
  77. $this->exec('plugin unload Vendor/TestPluginTwo');
  78. $this->assertExitCode(Command::CODE_SUCCESS);
  79. $contents = file_get_contents($this->app);
  80. $this->assertStringNotContainsString($plugin2, $contents);
  81. $this->assertStringContainsString($plugin1, $contents);
  82. }
  83. /**
  84. * Data provider for various forms.
  85. *
  86. * @return array
  87. */
  88. public function variantProvider(): array
  89. {
  90. return [
  91. // $this->addPlugin('TestPlugin', [
  92. // 'bootstrap' => false
  93. // ]);
  94. [" \$this->addPlugin('TestPlugin', [\n\t'bootstrap' => false\n]);\n"],
  95. // $this->addPlugin(
  96. // 'TestPlugin',
  97. // [ 'bootstrap' => false]
  98. // );
  99. [" \$this->addPlugin(\n\t'TestPlugin',\n\t[ 'bootstrap' => false]\n);\n"],
  100. // $this->addPlugin(
  101. // 'Foo',
  102. // [
  103. // 'bootstrap' => false
  104. // ]
  105. // );
  106. [" \$this->addPlugin(\n\t'TestPlugin',\n\t[\n\t\t'bootstrap' => false\n\t]\n);\n"],
  107. // $this->addPlugin('Test', [
  108. // 'bootstrap' => true,
  109. // 'routes' => true
  110. // ]);
  111. [" \$this->addPlugin('TestPlugin', [\n\t'bootstrap' => true,\n\t'routes' => true\n]);\n"],
  112. // $this->addPlugin('Test',
  113. // [
  114. // 'bootstrap' => true,
  115. // 'routes' => true
  116. // ]
  117. // );
  118. [" \$this->addPlugin('TestPlugin',\n\t[\n\t\t'bootstrap' => true,\n\t\t'routes' => true\n\t]\n);\n"],
  119. // $this->addPlugin('Test',
  120. // [
  121. //
  122. // ]
  123. // );
  124. [" \$this->addPlugin('TestPlugin',\n\t[\n\t\n\t]\n);\n"],
  125. // $this->addPlugin('Test');
  126. [" \$this->addPlugin('TestPlugin');\n"],
  127. // $this->addPlugin('Test', ['bootstrap' => true, 'route' => false]);
  128. [" \$this->addPlugin('TestPlugin', ['bootstrap' => true, 'route' => false]);\n"],
  129. ];
  130. }
  131. /**
  132. * This method will tests multiple notations of plugin loading in the application class
  133. *
  134. * @dataProvider variantProvider
  135. */
  136. public function testRegularExpressionsApplication(string $content): void
  137. {
  138. $this->addPluginToApp($content);
  139. $this->exec('plugin unload TestPlugin');
  140. $this->assertExitCode(Command::CODE_SUCCESS);
  141. $result = file_get_contents($this->app);
  142. $this->assertStringNotContainsString("addPlugin('TestPlugin'", $result);
  143. $this->assertDoesNotMatchRegularExpression("/this\-\>addPlugin\([\'\"]TestPlugin'[\'\"][^\)]*\)\;/mi", $result);
  144. }
  145. /**
  146. * _addPluginToApp
  147. *
  148. * Quick method to add a plugin to the Application file.
  149. * This is useful for the tests
  150. *
  151. * @param string $insert The addPlugin line to add.
  152. */
  153. protected function addPluginToApp($insert): void
  154. {
  155. $contents = file_get_contents($this->app);
  156. $contents = preg_replace('/(function bootstrap\(\)(?:\s*)\:(?:\s*)void(?:\s+)\{)/m', "\$1\n " . $insert, $contents);
  157. file_put_contents($this->app, $contents);
  158. }
  159. }