PluginCollectionTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. public function testWithInvalidHook()
  112. {
  113. $this->expectException(InvalidArgumentException::class);
  114. $plugins = new PluginCollection();
  115. foreach ($plugins->with('bad') as $p) {
  116. }
  117. }
  118. public function testFindPathNoConfigureData()
  119. {
  120. Configure::write('plugins', []);
  121. $plugins = new PluginCollection();
  122. $path = $plugins->findPath('TestPlugin');
  123. $this->assertEquals(TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS, $path);
  124. }
  125. public function testFindPathLoadsConfigureData()
  126. {
  127. $configPath = ROOT . DS . 'cakephp-plugins.php';
  128. $this->skipIf(file_exists($configPath), 'cakephp-plugins.php exists, skipping overwrite');
  129. $file = <<<PHP
  130. <?php
  131. return [
  132. 'plugins' => [
  133. 'TestPlugin' => '/config/path'
  134. ]
  135. ];
  136. PHP;
  137. file_put_contents($configPath, $file);
  138. Configure::delete('plugins');
  139. $plugins = new PluginCollection();
  140. $path = $plugins->findPath('TestPlugin');
  141. unlink($configPath);
  142. $this->assertEquals('/config/path', $path);
  143. }
  144. public function testFindPathConfigureData()
  145. {
  146. Configure::write('plugins', ['TestPlugin' => '/some/path']);
  147. $plugins = new PluginCollection();
  148. $path = $plugins->findPath('TestPlugin');
  149. $this->assertEquals('/some/path', $path);
  150. }
  151. public function testFindPathMissingPlugin()
  152. {
  153. Configure::write('plugins', []);
  154. $plugins = new PluginCollection();
  155. $this->expectException(MissingPluginException::class);
  156. $plugins->findPath('InvalidPlugin');
  157. }
  158. }