PluginCollectionTest.php 7.2 KB

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