PluginCollectionTest.php 7.9 KB

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