PluginTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 2.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Core;
  16. use Cake\Core\BasePlugin;
  17. use Cake\Core\Exception\MissingPluginException;
  18. use Cake\Core\Plugin;
  19. use Cake\TestSuite\TestCase;
  20. use TestPlugin\Plugin as TestPlugin;
  21. /**
  22. * PluginTest class
  23. */
  24. class PluginTest extends TestCase
  25. {
  26. /**
  27. * Setup
  28. *
  29. * @return void
  30. */
  31. public function setUp(): void
  32. {
  33. parent::setUp();
  34. $this->clearPlugins();
  35. }
  36. /**
  37. * Reverts the changes done to the environment while testing
  38. *
  39. * @return void
  40. */
  41. public function tearDown(): void
  42. {
  43. parent::tearDown();
  44. $this->clearPlugins();
  45. }
  46. /**
  47. * Tests loading a plugin with a class
  48. *
  49. * @return void
  50. */
  51. public function testLoadConcreteClass()
  52. {
  53. $this->loadPlugins(['TestPlugin']);
  54. $instance = Plugin::getCollection()->get('TestPlugin');
  55. $this->assertSame(TestPlugin::class, get_class($instance));
  56. }
  57. /**
  58. * Tests loading a plugin without a class
  59. *
  60. * @return void
  61. */
  62. public function testLoadDynamicClass()
  63. {
  64. $this->loadPlugins(['TestPluginTwo']);
  65. $instance = Plugin::getCollection()->get('TestPluginTwo');
  66. $this->assertSame(BasePlugin::class, get_class($instance));
  67. }
  68. /**
  69. * Tests that Plugin::path() returns the correct path for the loaded plugins
  70. *
  71. * @return void
  72. */
  73. public function testPath()
  74. {
  75. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  76. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  77. $this->assertPathEquals(Plugin::path('TestPlugin'), $expected);
  78. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  79. $this->assertPathEquals(Plugin::path('TestPluginTwo'), $expected);
  80. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS;
  81. $this->assertPathEquals(Plugin::path('Company/TestPluginThree'), $expected);
  82. }
  83. /**
  84. * Tests that Plugin::path() throws an exception on unknown plugin
  85. *
  86. * @return void
  87. */
  88. public function testPathNotFound()
  89. {
  90. $this->expectException(MissingPluginException::class);
  91. Plugin::path('NonExistentPlugin');
  92. }
  93. /**
  94. * Tests that Plugin::classPath() returns the correct path for the loaded plugins
  95. *
  96. * @return void
  97. */
  98. public function testClassPath()
  99. {
  100. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  101. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
  102. $this->assertPathEquals(Plugin::classPath('TestPlugin'), $expected);
  103. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'src' . DS;
  104. $this->assertPathEquals(Plugin::classPath('TestPluginTwo'), $expected);
  105. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS;
  106. $this->assertPathEquals(Plugin::classPath('Company/TestPluginThree'), $expected);
  107. }
  108. /**
  109. * Tests that Plugin::templatePath() returns the correct path for the loaded plugins
  110. *
  111. * @return void
  112. */
  113. public function testTemplatePath()
  114. {
  115. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  116. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'templates' . DS;
  117. $this->assertPathEquals(Plugin::templatePath('TestPlugin'), $expected);
  118. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'templates' . DS;
  119. $this->assertPathEquals(Plugin::templatePath('TestPluginTwo'), $expected);
  120. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'templates' . DS;
  121. $this->assertPathEquals(Plugin::templatePath('Company/TestPluginThree'), $expected);
  122. }
  123. /**
  124. * Tests that Plugin::classPath() throws an exception on unknown plugin
  125. *
  126. * @return void
  127. */
  128. public function testClassPathNotFound()
  129. {
  130. $this->expectException(MissingPluginException::class);
  131. Plugin::classPath('NonExistentPlugin');
  132. }
  133. }