BasePluginTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.6.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Core;
  15. use Cake\Console\CommandCollection;
  16. use Cake\Core\BasePlugin;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\Core\PluginApplicationInterface;
  20. use Cake\Http\MiddlewareQueue;
  21. use Cake\Routing\RouteBuilder;
  22. use Cake\Routing\RouteCollection;
  23. use Cake\TestSuite\TestCase;
  24. use Company\TestPluginThree\Plugin as TestPluginThree;
  25. use TestPlugin\Plugin as TestPlugin;
  26. /**
  27. * BasePluginTest class
  28. */
  29. class BasePluginTest extends TestCase
  30. {
  31. /**
  32. * tearDown method
  33. *
  34. * @return void
  35. */
  36. public function tearDown()
  37. {
  38. parent::tearDown();
  39. $this->clearPlugins();
  40. }
  41. /**
  42. * testConfigForRoutesAndBootstrap
  43. *
  44. * @return void
  45. */
  46. public function testConfigForRoutesAndBootstrap()
  47. {
  48. $plugin = new BasePlugin([
  49. 'bootstrap' => false,
  50. 'routes' => false,
  51. ]);
  52. $this->assertFalse($plugin->isEnabled('routes'));
  53. $this->assertFalse($plugin->isEnabled('bootstrap'));
  54. $this->assertTrue($plugin->isEnabled('console'));
  55. $this->assertTrue($plugin->isEnabled('middleware'));
  56. }
  57. public function testGetName()
  58. {
  59. $plugin = new TestPlugin();
  60. $this->assertSame('TestPlugin', $plugin->getName());
  61. $plugin = new TestPluginThree();
  62. $this->assertSame('Company/TestPluginThree', $plugin->getName());
  63. }
  64. public function testGetNameOption()
  65. {
  66. $plugin = new TestPlugin(['name' => 'Elephants']);
  67. $this->assertSame('Elephants', $plugin->getName());
  68. }
  69. public function testMiddleware()
  70. {
  71. $plugin = new BasePlugin();
  72. $middleware = new MiddlewareQueue();
  73. $this->assertSame($middleware, $plugin->middleware($middleware));
  74. }
  75. public function testConsoleNoop()
  76. {
  77. $plugin = new BasePlugin();
  78. $commands = new CommandCollection();
  79. $this->assertSame($commands, $plugin->console($commands));
  80. }
  81. public function testConsoleFind()
  82. {
  83. $plugin = new TestPlugin();
  84. Plugin::getCollection()->add($plugin);
  85. $result = $plugin->console(new CommandCollection());
  86. $this->assertTrue($result->has('widget'), 'Should have plugin command added');
  87. $this->assertTrue($result->has('test_plugin.widget'), 'Should have long plugin name');
  88. $this->assertTrue($result->has('example'), 'Should have plugin shell added');
  89. $this->assertTrue($result->has('test_plugin.example'), 'Should have long plugin name');
  90. }
  91. public function testBootstrap()
  92. {
  93. $app = $this->createMock(PluginApplicationInterface::class);
  94. $plugin = new TestPlugin();
  95. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  96. $this->assertNull($plugin->bootstrap($app));
  97. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  98. }
  99. /**
  100. * No errors should be emitted when a plugin doesn't have a bootstrap file.
  101. */
  102. public function testBootstrapSkipMissingFile()
  103. {
  104. $app = $this->createMock(PluginApplicationInterface::class);
  105. $plugin = new BasePlugin();
  106. $this->assertNull($plugin->bootstrap($app));
  107. }
  108. /**
  109. * No errors should be emitted when a plugin doesn't have a routes file.
  110. */
  111. public function testRoutesSkipMissingFile()
  112. {
  113. $app = $this->createMock(PluginApplicationInterface::class);
  114. $plugin = new BasePlugin();
  115. $routeBuilder = new RouteBuilder(new RouteCollection(), '/');
  116. $this->assertNull($plugin->routes($routeBuilder));
  117. }
  118. public function testConstructorArguments()
  119. {
  120. $plugin = new BasePlugin([
  121. 'routes' => false,
  122. 'bootstrap' => false,
  123. 'console' => false,
  124. 'middleware' => false,
  125. ]);
  126. $this->assertFalse($plugin->isEnabled('routes'));
  127. $this->assertFalse($plugin->isEnabled('bootstrap'));
  128. $this->assertFalse($plugin->isEnabled('console'));
  129. $this->assertFalse($plugin->isEnabled('middleware'));
  130. }
  131. public function testGetPathBaseClass()
  132. {
  133. $plugin = new BasePlugin();
  134. $expected = CAKE . 'Core' . DS;
  135. $this->assertSame($expected, $plugin->getPath());
  136. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  137. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  138. }
  139. public function testGetPathOptionValue()
  140. {
  141. $plugin = new BasePlugin(['path' => '/some/path']);
  142. $expected = '/some/path';
  143. $this->assertSame($expected, $plugin->getPath());
  144. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  145. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  146. }
  147. public function testGetPathSubclass()
  148. {
  149. $plugin = new TestPlugin();
  150. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  151. $this->assertSame($expected, $plugin->getPath());
  152. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  153. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  154. }
  155. }