ComponentRegistryTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Controller;
  17. use Cake\Controller\Component\AuthComponent;
  18. use Cake\Controller\Component\FlashComponent;
  19. use Cake\Controller\ComponentRegistry;
  20. use Cake\Controller\Controller;
  21. use Cake\Controller\Exception\MissingComponentException;
  22. use Cake\Http\Response;
  23. use Cake\Http\ServerRequest;
  24. use Cake\TestSuite\TestCase;
  25. use Countable;
  26. use TestApp\Controller\Component\FlashAliasComponent;
  27. use TestPlugin\Controller\Component\OtherComponent;
  28. use Traversable;
  29. class ComponentRegistryTest extends TestCase
  30. {
  31. /**
  32. * @var \Cake\Controller\ComponentRegistry
  33. */
  34. protected $Components;
  35. /**
  36. * setUp
  37. */
  38. public function setUp(): void
  39. {
  40. parent::setUp();
  41. $controller = new Controller(new ServerRequest(), new Response());
  42. $this->Components = new ComponentRegistry($controller);
  43. }
  44. /**
  45. * tearDown
  46. */
  47. public function tearDown(): void
  48. {
  49. parent::tearDown();
  50. unset($this->Components);
  51. $this->clearPlugins();
  52. }
  53. /**
  54. * test triggering callbacks on loaded helpers
  55. */
  56. public function testLoad(): void
  57. {
  58. $result = $this->Components->load('Flash');
  59. $this->assertInstanceOf(FlashComponent::class, $result);
  60. $this->assertInstanceOf(FlashComponent::class, $this->Components->Flash);
  61. $result = $this->Components->loaded();
  62. $this->assertEquals(['Flash'], $result, 'loaded() results are wrong.');
  63. $result = $this->Components->load('Flash');
  64. $this->assertSame($result, $this->Components->Flash);
  65. }
  66. /**
  67. * Tests loading as an alias
  68. */
  69. public function testLoadWithAlias(): void
  70. {
  71. $result = $this->Components->load('Flash', ['className' => FlashAliasComponent::class, 'somesetting' => true]);
  72. $this->assertInstanceOf(FlashAliasComponent::class, $result);
  73. $this->assertInstanceOf(FlashAliasComponent::class, $this->Components->Flash);
  74. $this->assertTrue($this->Components->Flash->getConfig('somesetting'));
  75. $result = $this->Components->loaded();
  76. $this->assertEquals(['Flash'], $result, 'loaded() results are wrong.');
  77. $result = $this->Components->load('Flash');
  78. $this->assertInstanceOf(FlashAliasComponent::class, $result);
  79. $this->loadPlugins(['TestPlugin']);
  80. $result = $this->Components->load('SomeOther', ['className' => 'TestPlugin.Other']);
  81. $this->assertInstanceOf(OtherComponent::class, $result);
  82. $this->assertInstanceOf(OtherComponent::class, $this->Components->SomeOther);
  83. $result = $this->Components->loaded();
  84. $this->assertEquals(['Flash', 'SomeOther'], $result, 'loaded() results are wrong.');
  85. }
  86. /**
  87. * test load and enable = false
  88. */
  89. public function testLoadWithEnableFalse(): void
  90. {
  91. $mock = $this->getMockBuilder('Cake\Event\EventManager')->getMock();
  92. $mock->expects($this->never())
  93. ->method('on');
  94. $this->Components->getController()->setEventManager($mock);
  95. $result = $this->Components->load('Flash', ['enabled' => false]);
  96. $this->assertInstanceOf(FlashComponent::class, $result);
  97. $this->assertInstanceOf(FlashComponent::class, $this->Components->Flash);
  98. }
  99. /**
  100. * test MissingComponent exception
  101. */
  102. public function testLoadMissingComponent(): void
  103. {
  104. $this->expectException(MissingComponentException::class);
  105. $this->Components->load('ThisComponentShouldAlwaysBeMissing');
  106. }
  107. /**
  108. * test loading a plugin component.
  109. */
  110. public function testLoadPluginComponent(): void
  111. {
  112. $this->loadPlugins(['TestPlugin']);
  113. $result = $this->Components->load('TestPlugin.Other');
  114. $this->assertInstanceOf(OtherComponent::class, $result, 'Component class is wrong.');
  115. $this->assertInstanceOf(OtherComponent::class, $this->Components->Other, 'Class is wrong');
  116. }
  117. /**
  118. * Test loading components with aliases and plugins.
  119. */
  120. public function testLoadWithAliasAndPlugin(): void
  121. {
  122. $this->loadPlugins(['TestPlugin']);
  123. $result = $this->Components->load('AliasedOther', ['className' => 'TestPlugin.Other']);
  124. $this->assertInstanceOf(OtherComponent::class, $result);
  125. $this->assertInstanceOf(OtherComponent::class, $this->Components->AliasedOther);
  126. $result = $this->Components->loaded();
  127. $this->assertEquals(['AliasedOther'], $result, 'loaded() results are wrong.');
  128. }
  129. /**
  130. * test getting the controller out of the collection
  131. */
  132. public function testGetController(): void
  133. {
  134. $result = $this->Components->getController();
  135. $this->assertInstanceOf('Cake\Controller\Controller', $result);
  136. }
  137. /**
  138. * Test reset.
  139. */
  140. public function testReset(): void
  141. {
  142. $eventManager = $this->Components->getController()->getEventManager();
  143. $instance = $this->Components->load('Auth');
  144. $this->assertSame(
  145. $instance,
  146. $this->Components->Auth,
  147. 'Instance in registry should be the same as previously loaded'
  148. );
  149. $this->assertCount(1, $eventManager->listeners('Controller.startup'));
  150. $this->assertSame($this->Components, $this->Components->reset());
  151. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  152. $this->assertNotSame($instance, $this->Components->load('Auth'));
  153. }
  154. /**
  155. * Test unloading.
  156. */
  157. public function testUnload(): void
  158. {
  159. $eventManager = $this->Components->getController()->getEventManager();
  160. $this->Components->load('Auth');
  161. $result = $this->Components->unload('Auth');
  162. $this->assertSame($this->Components, $result);
  163. $this->assertFalse(isset($this->Components->Auth), 'Should be gone');
  164. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  165. }
  166. /**
  167. * Test __unset.
  168. */
  169. public function testUnset(): void
  170. {
  171. $eventManager = $this->Components->getController()->getEventManager();
  172. $this->Components->load('Auth');
  173. unset($this->Components->Auth);
  174. $this->assertFalse(isset($this->Components->Auth), 'Should be gone');
  175. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  176. }
  177. /**
  178. * Test that unloading a none existing component triggers an error.
  179. */
  180. public function testUnloadUnknown(): void
  181. {
  182. $this->expectException(MissingComponentException::class);
  183. $this->expectExceptionMessage('Component class FooComponent could not be found.');
  184. $this->Components->unload('Foo');
  185. }
  186. /**
  187. * Test set.
  188. */
  189. public function testSet(): void
  190. {
  191. $eventManager = $this->Components->getController()->getEventManager();
  192. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  193. $auth = new AuthComponent($this->Components);
  194. $result = $this->Components->set('Auth', $auth);
  195. $this->assertSame($this->Components, $result);
  196. $this->assertTrue(isset($this->Components->Auth), 'Should be present');
  197. $this->assertCount(1, $eventManager->listeners('Controller.startup'));
  198. }
  199. /**
  200. * Test __set.
  201. */
  202. public function testMagicSet(): void
  203. {
  204. $eventManager = $this->Components->getController()->getEventManager();
  205. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  206. $auth = new AuthComponent($this->Components);
  207. $this->Components->Auth = $auth;
  208. $this->assertTrue(isset($this->Components->Auth), 'Should be present');
  209. $this->assertCount(1, $eventManager->listeners('Controller.startup'));
  210. }
  211. /**
  212. * Test Countable.
  213. */
  214. public function testCountable(): void
  215. {
  216. $this->Components->load('Auth');
  217. $this->assertInstanceOf(Countable::class, $this->Components);
  218. $count = count($this->Components);
  219. $this->assertSame(1, $count);
  220. }
  221. /**
  222. * Test Traversable.
  223. */
  224. public function testTraversable(): void
  225. {
  226. $this->Components->load('Auth');
  227. $this->assertInstanceOf(Traversable::class, $this->Components);
  228. $result = null;
  229. foreach ($this->Components as $component) {
  230. $result = $component;
  231. }
  232. $this->assertNotNull($result);
  233. }
  234. }