BasePluginTest.php 4.8 KB

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