PluginCollectionTest.php 6.1 KB

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