PluginCollectionTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 testAddOperations(): void
  46. {
  47. $plugins = new PluginCollection();
  48. $plugins->add(new TestPlugin());
  49. $this->assertFalse($plugins->has('Nope'));
  50. $this->assertSame($plugins, $plugins->remove('Nope'));
  51. $this->assertTrue($plugins->has('TestPlugin'));
  52. $this->assertSame($plugins, $plugins->remove('TestPlugin'));
  53. $this->assertCount(0, $plugins);
  54. $this->assertFalse($plugins->has('TestPlugin'));
  55. }
  56. public function testAddVendoredPlugin(): void
  57. {
  58. $plugins = new PluginCollection();
  59. $plugins->add(new TestPluginThreePlugin());
  60. $this->assertTrue($plugins->has('Company/TestPluginThree'));
  61. $this->assertFalse($plugins->has('TestPluginThree'));
  62. $this->assertFalse($plugins->has('Company'));
  63. $this->assertFalse($plugins->has('TestPlugin'));
  64. }
  65. public function testHas(): void
  66. {
  67. $plugins = new PluginCollection();
  68. $this->assertFalse($plugins->has('TestPlugin'));
  69. $plugins->add(new TestPlugin());
  70. $this->assertTrue($plugins->has('TestPlugin'));
  71. $this->assertFalse($plugins->has('Plugin'));
  72. }
  73. public function testGet(): void
  74. {
  75. $plugins = new PluginCollection();
  76. $plugin = new TestPlugin();
  77. $plugins->add($plugin);
  78. $this->assertSame($plugin, $plugins->get('TestPlugin'));
  79. }
  80. public function testGetAutoload(): void
  81. {
  82. $plugins = new PluginCollection();
  83. $plugin = $plugins->get('Named');
  84. $this->assertInstanceOf(NamedPlugin::class, $plugin);
  85. }
  86. public function testGetInvalid(): void
  87. {
  88. $this->expectException(MissingPluginException::class);
  89. $plugins = new PluginCollection();
  90. $plugins->get('Invalid');
  91. }
  92. public function testCreate(): void
  93. {
  94. $plugins = new PluginCollection();
  95. $plugin = $plugins->create('Named');
  96. $this->assertInstanceOf(NamedPlugin::class, $plugin);
  97. $plugin = $plugins->create('Named', ['name' => 'Granpa']);
  98. $this->assertInstanceOf(NamedPlugin::class, $plugin);
  99. $this->assertSame('Granpa', $plugin->getName());
  100. $plugin = $plugins->create(NamedPlugin::class);
  101. $this->assertInstanceOf(NamedPlugin::class, $plugin);
  102. $plugin = $plugins->create('Company/TestPluginThree');
  103. $this->assertInstanceOf(TestPluginThreePlugin::class, $plugin);
  104. $plugin = $plugins->create('TestTheme');
  105. $this->assertInstanceOf(BasePlugin::class, $plugin);
  106. $this->assertSame('TestTheme', $plugin->getName());
  107. }
  108. public function testCreateException(): void
  109. {
  110. $this->expectException(CakeException::class);
  111. $this->expectExceptionMessage('Cannot create a plugin with empty name');
  112. $plugins = new PluginCollection();
  113. $plugins->create('');
  114. }
  115. public function testIterator(): void
  116. {
  117. $data = [
  118. new TestPlugin(),
  119. new TestPluginThreePlugin(),
  120. ];
  121. $plugins = new PluginCollection($data);
  122. $out = [];
  123. foreach ($plugins as $key => $plugin) {
  124. $this->assertInstanceOf(PluginInterface::class, $plugin);
  125. $out[] = $plugin;
  126. }
  127. $this->assertSame($data, $out);
  128. }
  129. public function testWith(): void
  130. {
  131. $plugins = new PluginCollection();
  132. $plugin = new TestPlugin();
  133. $plugin->disable('routes');
  134. $pluginThree = new TestPluginThreePlugin();
  135. $plugins->add($plugin);
  136. $plugins->add($pluginThree);
  137. $out = [];
  138. foreach ($plugins->with('routes') as $p) {
  139. $out[] = $p;
  140. }
  141. $this->assertCount(1, $out);
  142. $this->assertSame($pluginThree, $out[0]);
  143. }
  144. /**
  145. * Test that looping over the plugin collection during
  146. * a with loop doesn't lose iteration state.
  147. *
  148. * This situation can happen when a plugin like bake
  149. * needs to discover things inside other plugins.
  150. */
  151. public function testWithInnerIteration(): void
  152. {
  153. $plugins = new PluginCollection();
  154. $plugin = new TestPlugin();
  155. $pluginThree = new TestPluginThreePlugin();
  156. $plugins->add($plugin);
  157. $plugins->add($pluginThree);
  158. $out = [];
  159. foreach ($plugins->with('routes') as $p) {
  160. foreach ($plugins as $i) {
  161. // Do nothing, we just need to enumerate the collection
  162. }
  163. $out[] = $p;
  164. }
  165. $this->assertCount(2, $out);
  166. $this->assertSame($plugin, $out[0]);
  167. $this->assertSame($pluginThree, $out[1]);
  168. }
  169. public function testWithInvalidHook(): void
  170. {
  171. $this->expectException(InvalidArgumentException::class);
  172. $plugins = new PluginCollection();
  173. foreach ($plugins->with('bad') as $p) {
  174. }
  175. }
  176. public function testFindPathNoConfigureData(): void
  177. {
  178. Configure::write('plugins', []);
  179. $plugins = new PluginCollection();
  180. $path = $plugins->findPath('TestPlugin');
  181. $this->assertSame(TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS, $path);
  182. }
  183. public function testFindPathLoadsConfigureData(): void
  184. {
  185. $configPath = ROOT . DS . 'cakephp-plugins.php';
  186. $this->skipIf(file_exists($configPath), 'cakephp-plugins.php exists, skipping overwrite');
  187. $file = <<<PHP
  188. <?php
  189. declare(strict_types=1);
  190. return [
  191. 'plugins' => [
  192. 'TestPlugin' => '/config/path'
  193. ]
  194. ];
  195. PHP;
  196. file_put_contents($configPath, $file);
  197. $plugins = new PluginCollection();
  198. Configure::delete('plugins');
  199. $path = $plugins->findPath('TestPlugin');
  200. unlink($configPath);
  201. $this->assertSame('/config/path', $path);
  202. }
  203. public function testFindPathConfigureData(): void
  204. {
  205. Configure::write('plugins', ['TestPlugin' => '/some/path']);
  206. $plugins = new PluginCollection();
  207. $path = $plugins->findPath('TestPlugin');
  208. $this->assertSame('/some/path', $path);
  209. }
  210. public function testFindPathMissingPlugin(): void
  211. {
  212. Configure::write('plugins', []);
  213. $plugins = new PluginCollection();
  214. $this->expectException(MissingPluginException::class);
  215. $plugins->findPath('InvalidPlugin');
  216. }
  217. }