BasePluginTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\Core\BasePlugin;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Container;
  20. use Cake\Core\Plugin;
  21. use Cake\Core\PluginApplicationInterface;
  22. use Cake\Http\MiddlewareQueue;
  23. use Cake\Routing\RouteBuilder;
  24. use Cake\Routing\RouteCollection;
  25. use Cake\TestSuite\TestCase;
  26. use Company\TestPluginThree\TestPluginThreePlugin;
  27. use TestPlugin\Plugin as TestPlugin;
  28. /**
  29. * BasePluginTest class
  30. */
  31. class BasePluginTest extends TestCase
  32. {
  33. /**
  34. * tearDown method
  35. */
  36. public function tearDown(): void
  37. {
  38. parent::tearDown();
  39. $this->clearPlugins();
  40. }
  41. /**
  42. * testConfigForRoutesAndBootstrap
  43. */
  44. public function testConfigForRoutesAndBootstrap(): void
  45. {
  46. $plugin = new BasePlugin([
  47. 'bootstrap' => false,
  48. 'routes' => false,
  49. ]);
  50. $this->assertFalse($plugin->isEnabled('routes'));
  51. $this->assertFalse($plugin->isEnabled('bootstrap'));
  52. $this->assertTrue($plugin->isEnabled('console'));
  53. $this->assertTrue($plugin->isEnabled('middleware'));
  54. $this->assertTrue($plugin->isEnabled('services'));
  55. }
  56. public function testGetName(): void
  57. {
  58. $plugin = new TestPlugin();
  59. $this->assertSame('TestPlugin', $plugin->getName());
  60. $plugin = new TestPluginThreePlugin();
  61. $this->assertSame('Company/TestPluginThree', $plugin->getName());
  62. }
  63. public function testGetNameOption(): void
  64. {
  65. $plugin = new TestPlugin(['name' => 'Elephants']);
  66. $this->assertSame('Elephants', $plugin->getName());
  67. }
  68. public function testMiddleware(): void
  69. {
  70. $plugin = new BasePlugin();
  71. $middleware = new MiddlewareQueue();
  72. $this->assertSame($middleware, $plugin->middleware($middleware));
  73. }
  74. public function testConsole(): void
  75. {
  76. $plugin = new BasePlugin();
  77. $commands = new CommandCollection();
  78. $this->assertSame($commands, $plugin->console($commands));
  79. }
  80. public function testServices(): void
  81. {
  82. $plugin = new BasePlugin();
  83. $container = new Container();
  84. $this->assertNull($plugin->services($container));
  85. }
  86. public function testConsoleFind(): void
  87. {
  88. $plugin = new TestPlugin();
  89. Plugin::getCollection()->add($plugin);
  90. $result = $plugin->console(new CommandCollection());
  91. $this->assertTrue($result->has('widget'), 'Should have plugin command added');
  92. $this->assertTrue($result->has('test_plugin.widget'), 'Should have long plugin name');
  93. $this->assertTrue($result->has('example'), 'Should have plugin shell added');
  94. $this->assertTrue($result->has('test_plugin.example'), 'Should have long plugin name');
  95. }
  96. public function testBootstrap(): void
  97. {
  98. $app = $this->createMock(PluginApplicationInterface::class);
  99. $plugin = new TestPlugin();
  100. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  101. $plugin->bootstrap($app);
  102. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  103. }
  104. /**
  105. * No errors should be emitted when a plugin doesn't have a bootstrap file.
  106. */
  107. public function testBootstrapSkipMissingFile(): void
  108. {
  109. $app = $this->createMock(PluginApplicationInterface::class);
  110. $plugin = new BasePlugin();
  111. $plugin->bootstrap($app);
  112. $this->assertTrue(true);
  113. }
  114. /**
  115. * No errors should be emitted when a plugin doesn't have a routes file.
  116. */
  117. public function testRoutesSkipMissingFile(): void
  118. {
  119. $plugin = new BasePlugin();
  120. $routeBuilder = new RouteBuilder(new RouteCollection(), '/');
  121. $plugin->routes($routeBuilder);
  122. $this->assertTrue(true);
  123. }
  124. public function testConstructorArguments(): void
  125. {
  126. $plugin = new BasePlugin([
  127. 'routes' => false,
  128. 'bootstrap' => false,
  129. 'console' => false,
  130. 'middleware' => false,
  131. 'templatePath' => '/plates/',
  132. ]);
  133. $this->assertFalse($plugin->isEnabled('routes'));
  134. $this->assertFalse($plugin->isEnabled('bootstrap'));
  135. $this->assertFalse($plugin->isEnabled('console'));
  136. $this->assertFalse($plugin->isEnabled('middleware'));
  137. $this->assertSame('/plates/', $plugin->getTemplatePath());
  138. }
  139. public function testGetPathBaseClass(): void
  140. {
  141. $plugin = new BasePlugin();
  142. $expected = CAKE . 'Core' . DS;
  143. $this->assertSame($expected, $plugin->getPath());
  144. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  145. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  146. $this->assertSame($expected . 'templates' . DS, $plugin->getTemplatePath());
  147. }
  148. public function testGetPathOptionValue(): void
  149. {
  150. $plugin = new BasePlugin(['path' => '/some/path']);
  151. $expected = '/some/path';
  152. $this->assertSame($expected, $plugin->getPath());
  153. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  154. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  155. $this->assertSame($expected . 'templates' . DS, $plugin->getTemplatePath());
  156. }
  157. public function testGetPathSubclass(): void
  158. {
  159. $plugin = new TestPlugin();
  160. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  161. $this->assertSame($expected, $plugin->getPath());
  162. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  163. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  164. $this->assertSame($expected . 'templates' . DS, $plugin->getTemplatePath());
  165. }
  166. }