PluginCollectionTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\Exception\MissingPluginException;
  16. use Cake\Core\PluginCollection;
  17. use Cake\Core\PluginInterface;
  18. use Cake\TestSuite\TestCase;
  19. use Company\TestPluginThree\Plugin as TestPluginThree;
  20. use InvalidArgumentException;
  21. use TestPlugin\Plugin as TestPlugin;
  22. /**
  23. * PluginCollection Test
  24. */
  25. class PluginCollectionTest extends TestCase
  26. {
  27. public function testConstructor()
  28. {
  29. $plugins = new PluginCollection([new TestPlugin()]);
  30. $this->assertCount(1, $plugins);
  31. $this->assertTrue($plugins->has('TestPlugin'));
  32. }
  33. public function testAdd()
  34. {
  35. $plugins = new PluginCollection();
  36. $this->assertCount(0, $plugins);
  37. $plugins->add(new TestPlugin());
  38. $this->assertCount(1, $plugins);
  39. }
  40. public function testAddOperations()
  41. {
  42. $plugins = new PluginCollection();
  43. $plugins->add(new TestPlugin());
  44. $this->assertFalse($plugins->has('Nope'));
  45. $this->assertSame($plugins, $plugins->remove('Nope'));
  46. $this->assertTrue($plugins->has('TestPlugin'));
  47. $this->assertSame($plugins, $plugins->remove('TestPlugin'));
  48. $this->assertCount(0, $plugins);
  49. $this->assertFalse($plugins->has('TestPlugin'));
  50. }
  51. public function testAddVendoredPlugin()
  52. {
  53. $plugins = new PluginCollection();
  54. $plugins->add(new TestPluginThree());
  55. $this->assertTrue($plugins->has('Company/TestPluginThree'));
  56. $this->assertFalse($plugins->has('TestPluginThree'));
  57. $this->assertFalse($plugins->has('Company'));
  58. $this->assertFalse($plugins->has('TestPlugin'));
  59. }
  60. public function testHas()
  61. {
  62. $plugins = new PluginCollection();
  63. $this->assertFalse($plugins->has('TestPlugin'));
  64. $plugins->add(new TestPlugin());
  65. $this->assertTrue($plugins->has('TestPlugin'));
  66. $this->assertFalse($plugins->has('Plugin'));
  67. }
  68. public function testGet()
  69. {
  70. $plugins = new PluginCollection();
  71. $plugin = new TestPlugin();
  72. $plugins->add($plugin);
  73. $this->assertSame($plugin, $plugins->get('TestPlugin'));
  74. }
  75. public function testGetInvalid()
  76. {
  77. $this->expectException(MissingPluginException::class);
  78. $plugins = new PluginCollection();
  79. $plugins->get('Invalid');
  80. }
  81. public function testIterator()
  82. {
  83. $data = [
  84. new TestPlugin(),
  85. new TestPluginThree()
  86. ];
  87. $plugins = new PluginCollection($data);
  88. $out = [];
  89. foreach ($plugins as $key => $plugin) {
  90. $this->assertInstanceOf(PluginInterface::class, $plugin);
  91. $out[] = $plugin;
  92. }
  93. $this->assertSame($data, $out);
  94. }
  95. public function testWith()
  96. {
  97. $plugins = new PluginCollection();
  98. $plugin = new TestPlugin();
  99. $plugin->disable('routes');
  100. $pluginThree = new TestPluginThree();
  101. $plugins->add($plugin);
  102. $plugins->add($pluginThree);
  103. $out = [];
  104. foreach ($plugins->with('routes') as $p) {
  105. $out[] = $p;
  106. }
  107. $this->assertCount(1, $out);
  108. $this->assertSame($pluginThree, $out[0]);
  109. }
  110. public function testWithInvalidHook()
  111. {
  112. $this->expectException(InvalidArgumentException::class);
  113. $plugins = new PluginCollection();
  114. foreach ($plugins->with('bad') as $p) {
  115. }
  116. }
  117. }