PluginCollectionTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 3.6.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\Configure;
  18. use Cake\Core\Exception\CakeException;
  19. use Cake\Core\Exception\MissingPluginException;
  20. use Cake\Core\PluginCollection;
  21. use Cake\Core\PluginInterface;
  22. use Cake\TestSuite\TestCase;
  23. use Company\TestPluginThree\TestPluginThreePlugin;
  24. use InvalidArgumentException;
  25. use Named\NamedPlugin;
  26. use TestPlugin\Plugin as TestPlugin;
  27. /**
  28. * PluginCollection Test
  29. */
  30. class PluginCollectionTest extends TestCase
  31. {
  32. public function testConstructor(): void
  33. {
  34. $plugins = new PluginCollection([new TestPlugin()]);
  35. $this->assertCount(1, $plugins);
  36. $this->assertTrue($plugins->has('TestPlugin'));
  37. }
  38. public function testAdd(): void
  39. {
  40. $plugins = new PluginCollection();
  41. $this->assertCount(0, $plugins);
  42. $plugins->add(new TestPlugin());
  43. $this->assertCount(1, $plugins);
  44. }
  45. public function testAddDuplicate(): void
  46. {
  47. $this->expectException(CakeException::class);
  48. $this->expectExceptionMessage('Plugin named `TestPlugin` is already loaded');
  49. $plugins = new PluginCollection();
  50. $plugins->add(new TestPlugin());
  51. $plugins->add(new TestPlugin());
  52. }
  53. public function testAddFromConfig(): void
  54. {
  55. Configure::write('debug', false);
  56. $config = [
  57. 'Company/TestPluginThree',
  58. 'TestPlugin' => ['onlyDebug' => true],
  59. 'Nope' => ['optional' => true],
  60. 'Named' => ['routes' => false],
  61. ];
  62. $plugins = new PluginCollection();
  63. $plugins->addFromConfig($config);
  64. $this->assertCount(2, $plugins);
  65. $this->assertTrue($plugins->has('Company/TestPluginThree'));
  66. $this->assertFalse($plugins->has('TestPlugin'));
  67. $this->assertFalse($plugins->get('Named')->isEnabled('routes'));
  68. }
  69. public function testAddOperations(): void
  70. {
  71. $plugins = new PluginCollection();
  72. $plugins->add(new TestPlugin());
  73. $this->assertFalse($plugins->has('Nope'));
  74. $this->assertSame($plugins, $plugins->remove('Nope'));
  75. $this->assertTrue($plugins->has('TestPlugin'));
  76. $this->assertSame($plugins, $plugins->remove('TestPlugin'));
  77. $this->assertCount(0, $plugins);
  78. $this->assertFalse($plugins->has('TestPlugin'));
  79. }
  80. public function testAddVendoredPlugin(): void
  81. {
  82. $plugins = new PluginCollection();
  83. $plugins->add(new TestPluginThreePlugin());
  84. $this->assertTrue($plugins->has('Company/TestPluginThree'));
  85. $this->assertFalse($plugins->has('TestPluginThree'));
  86. $this->assertFalse($plugins->has('Company'));
  87. $this->assertFalse($plugins->has('TestPlugin'));
  88. }
  89. public function testHas(): void
  90. {
  91. $plugins = new PluginCollection();
  92. $this->assertFalse($plugins->has('TestPlugin'));
  93. $plugins->add(new TestPlugin());
  94. $this->assertTrue($plugins->has('TestPlugin'));
  95. $this->assertFalse($plugins->has('Plugin'));
  96. }
  97. public function testGet(): void
  98. {
  99. $plugins = new PluginCollection();
  100. $plugin = new TestPlugin();
  101. $plugins->add($plugin);
  102. $this->assertSame($plugin, $plugins->get('TestPlugin'));
  103. }
  104. public function testGetAutoload(): void
  105. {
  106. $plugins = new PluginCollection();
  107. $plugin = $plugins->get('Named');
  108. $this->assertInstanceOf(NamedPlugin::class, $plugin);
  109. }
  110. public function testGetInvalid(): void
  111. {
  112. $this->expectException(MissingPluginException::class);
  113. $plugins = new PluginCollection();
  114. $plugins->get('Invalid');
  115. }
  116. public function testCreate(): void
  117. {
  118. $plugins = new PluginCollection();
  119. $plugin = $plugins->create('Named');
  120. $this->assertInstanceOf(NamedPlugin::class, $plugin);
  121. $plugin = $plugins->create('Named', ['name' => 'Granpa']);
  122. $this->assertInstanceOf(NamedPlugin::class, $plugin);
  123. $this->assertSame('Granpa', $plugin->getName());
  124. $plugin = $plugins->create(NamedPlugin::class);
  125. $this->assertInstanceOf(NamedPlugin::class, $plugin);
  126. $plugin = $plugins->create('Company/TestPluginThree');
  127. $this->assertInstanceOf(TestPluginThreePlugin::class, $plugin);
  128. $plugin = $plugins->create('TestTheme');
  129. $this->assertInstanceOf(BasePlugin::class, $plugin);
  130. $this->assertSame('TestTheme', $plugin->getName());
  131. }
  132. public function testCreateException(): void
  133. {
  134. $this->expectException(CakeException::class);
  135. $this->expectExceptionMessage('Cannot create a plugin with empty name');
  136. $plugins = new PluginCollection();
  137. $plugins->create('');
  138. }
  139. public function testIterator(): void
  140. {
  141. $data = [
  142. new TestPlugin(),
  143. new TestPluginThreePlugin(),
  144. ];
  145. $plugins = new PluginCollection($data);
  146. $out = [];
  147. foreach ($plugins as $plugin) {
  148. $this->assertInstanceOf(PluginInterface::class, $plugin);
  149. $out[] = $plugin;
  150. }
  151. $this->assertSame($data, $out);
  152. }
  153. public function testWith(): void
  154. {
  155. $plugins = new PluginCollection();
  156. $plugin = new TestPlugin();
  157. $plugin->disable('routes');
  158. $pluginThree = new TestPluginThreePlugin();
  159. $plugins->add($plugin);
  160. $plugins->add($pluginThree);
  161. $out = [];
  162. foreach ($plugins->with('routes') as $p) {
  163. $out[] = $p;
  164. }
  165. $this->assertCount(1, $out);
  166. $this->assertSame($pluginThree, $out[0]);
  167. }
  168. /**
  169. * Test that looping over the plugin collection during
  170. * a with loop doesn't lose iteration state.
  171. *
  172. * This situation can happen when a plugin like bake
  173. * needs to discover things inside other plugins.
  174. */
  175. public function testWithInnerIteration(): void
  176. {
  177. $plugins = new PluginCollection();
  178. $plugin = new TestPlugin();
  179. $pluginThree = new TestPluginThreePlugin();
  180. $plugins->add($plugin);
  181. $plugins->add($pluginThree);
  182. $out = [];
  183. foreach ($plugins->with('routes') as $p) {
  184. // phpcs:ignore SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable
  185. foreach ($plugins as $i) {
  186. // Do nothing, we just need to enumerate the collection
  187. }
  188. $out[] = $p;
  189. }
  190. $this->assertCount(2, $out);
  191. $this->assertSame($plugin, $out[0]);
  192. $this->assertSame($pluginThree, $out[1]);
  193. }
  194. public function testWithInvalidHook(): void
  195. {
  196. $this->expectException(InvalidArgumentException::class);
  197. $plugins = new PluginCollection();
  198. // phpcs:ignore SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable
  199. foreach ($plugins->with('bad') as $p) {
  200. }
  201. }
  202. public function testFindPathNoConfigureData(): void
  203. {
  204. Configure::write('plugins', []);
  205. $plugins = new PluginCollection();
  206. $path = $plugins->findPath('TestPlugin');
  207. $this->assertSame(TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS, $path);
  208. }
  209. public function testFindPathLoadsConfigureData(): void
  210. {
  211. $configPath = ROOT . DS . 'cakephp-plugins.php';
  212. $this->skipIf(file_exists($configPath), 'cakephp-plugins.php exists, skipping overwrite');
  213. $file = <<<PHP
  214. <?php
  215. declare(strict_types=1);
  216. return [
  217. 'plugins' => [
  218. 'TestPlugin' => '/config/path'
  219. ]
  220. ];
  221. PHP;
  222. file_put_contents($configPath, $file);
  223. $plugins = new PluginCollection();
  224. Configure::delete('plugins');
  225. $path = $plugins->findPath('TestPlugin');
  226. unlink($configPath);
  227. $this->assertSame('/config/path', $path);
  228. }
  229. public function testFindPathConfigureData(): void
  230. {
  231. Configure::write('plugins', ['TestPlugin' => '/some/path']);
  232. $plugins = new PluginCollection();
  233. $path = $plugins->findPath('TestPlugin');
  234. $this->assertSame('/some/path', $path);
  235. }
  236. public function testFindPathMissingPlugin(): void
  237. {
  238. Configure::write('plugins', []);
  239. $plugins = new PluginCollection();
  240. $this->expectException(MissingPluginException::class);
  241. $plugins->findPath('InvalidPlugin');
  242. }
  243. }