BasePluginTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  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(tm) Project
  12. * @since 3.6.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Core;
  16. use Cake\Console\CommandCollection;
  17. use Cake\Console\CommandRunner;
  18. use Cake\Console\ConsoleIo;
  19. use Cake\Console\TestSuite\StubConsoleOutput;
  20. use Cake\Core\BasePlugin;
  21. use Cake\Core\Configure;
  22. use Cake\Core\Container;
  23. use Cake\Core\Plugin;
  24. use Cake\Core\PluginApplicationInterface;
  25. use Cake\Event\EventManagerInterface;
  26. use Cake\Http\BaseApplication;
  27. use Cake\Http\MiddlewareQueue;
  28. use Cake\Http\ServerRequestFactory;
  29. use Cake\Routing\RouteBuilder;
  30. use Cake\Routing\RouteCollection;
  31. use Cake\TestSuite\TestCase;
  32. use Company\TestPluginThree\TestPluginThreePlugin;
  33. use Mockery;
  34. use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
  35. use TestPlugin\Plugin as TestPlugin;
  36. /**
  37. * BasePluginTest class
  38. */
  39. class BasePluginTest extends TestCase
  40. {
  41. /**
  42. * tearDown method
  43. */
  44. public function tearDown(): void
  45. {
  46. parent::tearDown();
  47. $this->clearPlugins();
  48. }
  49. /**
  50. * testConfigForRoutesAndBootstrap
  51. */
  52. public function testConfigForRoutesAndBootstrap(): void
  53. {
  54. $plugin = new BasePlugin([
  55. 'bootstrap' => false,
  56. 'routes' => false,
  57. ]);
  58. $this->assertFalse($plugin->isEnabled('routes'));
  59. $this->assertFalse($plugin->isEnabled('bootstrap'));
  60. $this->assertTrue($plugin->isEnabled('console'));
  61. $this->assertTrue($plugin->isEnabled('middleware'));
  62. $this->assertTrue($plugin->isEnabled('services'));
  63. }
  64. public function testGetName(): void
  65. {
  66. $plugin = new TestPlugin();
  67. $this->assertSame('TestPlugin', $plugin->getName());
  68. $plugin = new TestPluginThreePlugin();
  69. $this->assertSame('Company/TestPluginThree', $plugin->getName());
  70. }
  71. public function testGetNameOption(): void
  72. {
  73. $plugin = new TestPlugin(['name' => 'Elephants']);
  74. $this->assertSame('Elephants', $plugin->getName());
  75. }
  76. public function testMiddleware(): void
  77. {
  78. $plugin = new BasePlugin();
  79. $middleware = new MiddlewareQueue();
  80. $this->assertSame($middleware, $plugin->middleware($middleware));
  81. }
  82. public function testConsole(): void
  83. {
  84. $plugin = new BasePlugin();
  85. $commands = new CommandCollection();
  86. $this->assertSame($commands, $plugin->console($commands));
  87. }
  88. #[DoesNotPerformAssertions]
  89. public function testServices(): void
  90. {
  91. $plugin = new BasePlugin();
  92. $container = new Container();
  93. $plugin->services($container);
  94. }
  95. public function testConsoleFind(): void
  96. {
  97. $plugin = new TestPlugin();
  98. Plugin::getCollection()->add($plugin);
  99. $result = $plugin->console(new CommandCollection());
  100. $this->assertTrue($result->has('sample'), 'Should have plugin command added');
  101. $this->assertTrue($result->has('test_plugin.sample'), 'Should have long plugin name');
  102. $this->assertTrue($result->has('example'), 'Should have plugin shell added');
  103. $this->assertTrue($result->has('test_plugin.example'), 'Should have long plugin name');
  104. }
  105. public function testBootstrap(): void
  106. {
  107. $app = new class implements PluginApplicationInterface {
  108. use BasePluginApplicationTrait;
  109. };
  110. $plugin = new TestPlugin();
  111. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  112. $plugin->bootstrap($app);
  113. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  114. }
  115. /**
  116. * No errors should be emitted when a plugin doesn't have a bootstrap file.
  117. */
  118. public function testBootstrapSkipMissingFile(): void
  119. {
  120. $app = new class implements PluginApplicationInterface {
  121. use BasePluginApplicationTrait;
  122. };
  123. $plugin = new BasePlugin();
  124. $plugin->bootstrap($app);
  125. $this->assertTrue(true);
  126. }
  127. /**
  128. * No errors should be emitted when a plugin doesn't have a routes file.
  129. */
  130. public function testRoutesSkipMissingFile(): void
  131. {
  132. $plugin = new BasePlugin();
  133. $routeBuilder = new RouteBuilder(new RouteCollection(), '/');
  134. $plugin->routes($routeBuilder);
  135. $this->assertTrue(true);
  136. }
  137. public function testConstructorArguments(): void
  138. {
  139. $plugin = new BasePlugin([
  140. 'routes' => false,
  141. 'bootstrap' => false,
  142. 'console' => false,
  143. 'middleware' => false,
  144. 'templatePath' => '/plates/',
  145. ]);
  146. $this->assertFalse($plugin->isEnabled('routes'));
  147. $this->assertFalse($plugin->isEnabled('bootstrap'));
  148. $this->assertFalse($plugin->isEnabled('console'));
  149. $this->assertFalse($plugin->isEnabled('middleware'));
  150. $this->assertSame('/plates/', $plugin->getTemplatePath());
  151. }
  152. public function testGetPathBaseClass(): void
  153. {
  154. $plugin = new BasePlugin();
  155. $expected = CAKE . 'Core' . DS;
  156. $this->assertSame($expected, $plugin->getPath());
  157. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  158. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  159. $this->assertSame($expected . 'templates' . DS, $plugin->getTemplatePath());
  160. }
  161. public function testGetPathOptionValue(): void
  162. {
  163. $plugin = new BasePlugin(['path' => '/some/path']);
  164. $expected = '/some/path';
  165. $this->assertSame($expected, $plugin->getPath());
  166. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  167. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  168. $this->assertSame($expected . 'templates' . DS, $plugin->getTemplatePath());
  169. }
  170. public function testGetPathSubclass(): void
  171. {
  172. $plugin = new TestPlugin();
  173. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  174. $this->assertSame($expected, $plugin->getPath());
  175. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  176. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  177. $this->assertSame($expected . 'templates' . DS, $plugin->getTemplatePath());
  178. }
  179. public function testEventsAreRegistered(): void
  180. {
  181. static::setAppNamespace();
  182. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/cakes']);
  183. $request = $request->withAttribute('params', [
  184. 'controller' => 'Cakes',
  185. 'action' => 'index',
  186. 'plugin' => null,
  187. 'pass' => [],
  188. ]);
  189. $basePlugin = new class extends BasePlugin
  190. {
  191. public function events(EventManagerInterface $eventManager): EventManagerInterface
  192. {
  193. $eventManager->on('testTrue', function ($event) {
  194. return true;
  195. });
  196. return $eventManager;
  197. }
  198. };
  199. $app = new class (dirname(__DIR__, 2)) extends BaseApplication
  200. {
  201. public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
  202. {
  203. return $middlewareQueue;
  204. }
  205. };
  206. $app = $app->addPlugin($basePlugin);
  207. $app->handle($request);
  208. $this->assertNotEmpty($app->getEventManager()->listeners('testTrue'));
  209. }
  210. public function testConsoleEventsAreRegistered(): void
  211. {
  212. static::setAppNamespace();
  213. $basePlugin = new class extends BasePlugin
  214. {
  215. public function events(EventManagerInterface $eventManager): EventManagerInterface
  216. {
  217. $eventManager->on('testTrue', function ($event) {
  218. return true;
  219. });
  220. return $eventManager;
  221. }
  222. };
  223. $app = new class (dirname(__DIR__, 2)) extends BaseApplication
  224. {
  225. public function routes(RouteBuilder $routes): void
  226. {
  227. }
  228. public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
  229. {
  230. return $middlewareQueue;
  231. }
  232. };
  233. $app = $app->addPlugin($basePlugin);
  234. $output = new StubConsoleOutput();
  235. $consoleIo = Mockery::mock(ConsoleIo::class, [$output, $output, null, null])
  236. ->shouldAllowMockingMethod('in')
  237. ->makePartial();
  238. $runner = new CommandRunner($app);
  239. $runner->run(['cake', 'version'], $consoleIo);
  240. $this->assertNotEmpty($app->getEventManager()->listeners('testTrue'));
  241. }
  242. }