PluginTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. public function setUp(): void
  30. {
  31. parent::setUp();
  32. $this->clearPlugins();
  33. }
  34. /**
  35. * Reverts the changes done to the environment while testing
  36. */
  37. public function tearDown(): void
  38. {
  39. parent::tearDown();
  40. $this->clearPlugins();
  41. }
  42. /**
  43. * Tests loading a plugin with a class
  44. */
  45. public function testLoadConcreteClass(): void
  46. {
  47. $this->loadPlugins(['TestPlugin']);
  48. $instance = Plugin::getCollection()->get('TestPlugin');
  49. $this->assertSame(TestPlugin::class, $instance::class);
  50. }
  51. /**
  52. * Tests loading a plugin without a class
  53. */
  54. public function testLoadDynamicClass(): void
  55. {
  56. $this->loadPlugins(['TestPluginTwo']);
  57. $instance = Plugin::getCollection()->get('TestPluginTwo');
  58. $this->assertSame(BasePlugin::class, $instance::class);
  59. }
  60. /**
  61. * Tests that Plugin::path() returns the correct path for the loaded plugins
  62. */
  63. public function testPath(): void
  64. {
  65. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  66. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  67. $this->assertPathEquals(Plugin::path('TestPlugin'), $expected);
  68. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  69. $this->assertPathEquals(Plugin::path('TestPluginTwo'), $expected);
  70. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS;
  71. $this->assertPathEquals(Plugin::path('Company/TestPluginThree'), $expected);
  72. }
  73. /**
  74. * Tests that Plugin::path() throws an exception on unknown plugin
  75. */
  76. public function testPathNotFound(): void
  77. {
  78. $this->expectException(MissingPluginException::class);
  79. Plugin::path('NonExistentPlugin');
  80. }
  81. /**
  82. * Tests that Plugin::classPath() returns the correct path for the loaded plugins
  83. */
  84. public function testClassPath(): void
  85. {
  86. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  87. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
  88. $this->assertPathEquals(Plugin::classPath('TestPlugin'), $expected);
  89. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'src' . DS;
  90. $this->assertPathEquals(Plugin::classPath('TestPluginTwo'), $expected);
  91. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS;
  92. $this->assertPathEquals(Plugin::classPath('Company/TestPluginThree'), $expected);
  93. }
  94. /**
  95. * Tests that Plugin::templatePath() returns the correct path for the loaded plugins
  96. */
  97. public function testTemplatePath(): void
  98. {
  99. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  100. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'templates' . DS;
  101. $this->assertPathEquals(Plugin::templatePath('TestPlugin'), $expected);
  102. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'templates' . DS;
  103. $this->assertPathEquals(Plugin::templatePath('TestPluginTwo'), $expected);
  104. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'templates' . DS;
  105. $this->assertPathEquals(Plugin::templatePath('Company/TestPluginThree'), $expected);
  106. }
  107. /**
  108. * Tests that Plugin::classPath() throws an exception on unknown plugin
  109. */
  110. public function testClassPathNotFound(): void
  111. {
  112. $this->expectException(MissingPluginException::class);
  113. Plugin::classPath('NonExistentPlugin');
  114. }
  115. }