BasePluginTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 testConsole()
  75. {
  76. $plugin = new BasePlugin();
  77. $commands = new CommandCollection();
  78. $this->assertSame($commands, $plugin->console($commands));
  79. }
  80. public function testBootstrap()
  81. {
  82. $app = $this->createMock(PluginApplicationInterface::class);
  83. $plugin = new TestPlugin();
  84. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  85. $this->assertNull($plugin->bootstrap($app));
  86. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  87. }
  88. public function testConstructorArguments()
  89. {
  90. $plugin = new BasePlugin([
  91. 'routes' => false,
  92. 'bootstrap' => false,
  93. 'console' => false,
  94. 'middleware' => false
  95. ]);
  96. $this->assertFalse($plugin->isEnabled('routes'));
  97. $this->assertFalse($plugin->isEnabled('bootstrap'));
  98. $this->assertFalse($plugin->isEnabled('console'));
  99. $this->assertFalse($plugin->isEnabled('middleware'));
  100. }
  101. public function testGetPathBaseClass()
  102. {
  103. $plugin = new BasePlugin();
  104. $expected = CAKE . 'Core' . DS;
  105. $this->assertSame($expected, $plugin->getPath());
  106. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  107. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  108. }
  109. public function testGetPathOptionValue()
  110. {
  111. $plugin = new BasePlugin(['path' => '/some/path']);
  112. $expected = '/some/path';
  113. $this->assertSame($expected, $plugin->getPath());
  114. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  115. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  116. }
  117. public function testGetPathSubclass()
  118. {
  119. $plugin = new TestPlugin();
  120. $expected = TEST_APP . 'Plugin/TestPlugin' . DS;
  121. $this->assertSame($expected, $plugin->getPath());
  122. $this->assertSame($expected . 'config' . DS, $plugin->getConfigPath());
  123. $this->assertSame($expected . 'src' . DS, $plugin->getClassPath());
  124. }
  125. }