ComponentRegistryTest.php 9.4 KB

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