BasePluginTest.php 6.2 KB

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