BasePluginTest.php 5.6 KB

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