BasePluginTest.php 4.8 KB

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