PluginCollectionTest.php 6.4 KB

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