PluginCollectionTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 testGetInvalid()
  78. {
  79. $this->expectException(MissingPluginException::class);
  80. $plugins = new PluginCollection();
  81. $plugins->get('Invalid');
  82. }
  83. public function testIterator()
  84. {
  85. $data = [
  86. new TestPlugin(),
  87. new TestPluginThree()
  88. ];
  89. $plugins = new PluginCollection($data);
  90. $out = [];
  91. foreach ($plugins as $key => $plugin) {
  92. $this->assertInstanceOf(PluginInterface::class, $plugin);
  93. $out[] = $plugin;
  94. }
  95. $this->assertSame($data, $out);
  96. }
  97. public function testWith()
  98. {
  99. $plugins = new PluginCollection();
  100. $plugin = new TestPlugin();
  101. $plugin->disable('routes');
  102. $pluginThree = new TestPluginThree();
  103. $plugins->add($plugin);
  104. $plugins->add($pluginThree);
  105. $out = [];
  106. foreach ($plugins->with('routes') as $p) {
  107. $out[] = $p;
  108. }
  109. $this->assertCount(1, $out);
  110. $this->assertSame($pluginThree, $out[0]);
  111. }
  112. public function testWithInvalidHook()
  113. {
  114. $this->expectException(InvalidArgumentException::class);
  115. $plugins = new PluginCollection();
  116. foreach ($plugins->with('bad') as $p) {
  117. }
  118. }
  119. public function testFindPathNoConfigureData()
  120. {
  121. Configure::write('plugins', []);
  122. $plugins = new PluginCollection();
  123. $path = $plugins->findPath('TestPlugin');
  124. $this->assertEquals(TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS, $path);
  125. }
  126. public function testFindPathLoadsConfigureData()
  127. {
  128. $configPath = ROOT . DS . 'cakephp-plugins.php';
  129. $this->skipIf(file_exists($configPath), 'cakephp-plugins.php exists, skipping overwrite');
  130. $file = <<<PHP
  131. <?php
  132. declare(strict_types=1);
  133. return [
  134. 'plugins' => [
  135. 'TestPlugin' => '/config/path'
  136. ]
  137. ];
  138. PHP;
  139. file_put_contents($configPath, $file);
  140. Configure::delete('plugins');
  141. $plugins = new PluginCollection();
  142. $path = $plugins->findPath('TestPlugin');
  143. unlink($configPath);
  144. $this->assertEquals('/config/path', $path);
  145. }
  146. public function testFindPathConfigureData()
  147. {
  148. Configure::write('plugins', ['TestPlugin' => '/some/path']);
  149. $plugins = new PluginCollection();
  150. $path = $plugins->findPath('TestPlugin');
  151. $this->assertEquals('/some/path', $path);
  152. }
  153. public function testFindPathMissingPlugin()
  154. {
  155. Configure::write('plugins', []);
  156. $plugins = new PluginCollection();
  157. $this->expectException(MissingPluginException::class);
  158. $plugins->findPath('InvalidPlugin');
  159. }
  160. }