ComponentRegistryTest.php 9.3 KB

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