PluginCollectionTest.php 7.1 KB

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