ComponentRegistryTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.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. }
  53. /**
  54. * test triggering callbacks on loaded helpers
  55. *
  56. * @return void
  57. */
  58. public function testLoad()
  59. {
  60. $result = $this->Components->load('Cookie');
  61. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $result);
  62. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $this->Components->Cookie);
  63. $result = $this->Components->loaded();
  64. $this->assertEquals(['Cookie'], $result, 'loaded() results are wrong.');
  65. $result = $this->Components->load('Cookie');
  66. $this->assertSame($result, $this->Components->Cookie);
  67. }
  68. /**
  69. * Tests loading as an alias
  70. *
  71. * @return void
  72. */
  73. public function testLoadWithAlias()
  74. {
  75. $result = $this->Components->load('Cookie', ['className' => __NAMESPACE__ . '\CookieAliasComponent', 'somesetting' => true]);
  76. $this->assertInstanceOf(__NAMESPACE__ . '\CookieAliasComponent', $result);
  77. $this->assertInstanceOf(__NAMESPACE__ . '\CookieAliasComponent', $this->Components->Cookie);
  78. $this->assertTrue($this->Components->Cookie->config('somesetting'));
  79. $result = $this->Components->loaded();
  80. $this->assertEquals(['Cookie'], $result, 'loaded() results are wrong.');
  81. $result = $this->Components->load('Cookie');
  82. $this->assertInstanceOf(__NAMESPACE__ . '\CookieAliasComponent', $result);
  83. Plugin::load('TestPlugin');
  84. $result = $this->Components->load('SomeOther', ['className' => 'TestPlugin.Other']);
  85. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $result);
  86. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $this->Components->SomeOther);
  87. $result = $this->Components->loaded();
  88. $this->assertEquals(['Cookie', 'SomeOther'], $result, 'loaded() results are wrong.');
  89. }
  90. /**
  91. * test load and enable = false
  92. *
  93. * @return void
  94. */
  95. public function testLoadWithEnableFalse()
  96. {
  97. $mock = $this->getMockBuilder('Cake\Event\EventManager')->getMock();
  98. $mock->expects($this->never())
  99. ->method('attach');
  100. $this->Components->getController()->setEventManager($mock);
  101. $result = $this->Components->load('Cookie', ['enabled' => false]);
  102. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $result);
  103. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $this->Components->Cookie);
  104. }
  105. /**
  106. * test MissingComponent exception
  107. *
  108. * @expectedException \Cake\Controller\Exception\MissingComponentException
  109. * @return void
  110. */
  111. public function testLoadMissingComponent()
  112. {
  113. $this->Components->load('ThisComponentShouldAlwaysBeMissing');
  114. }
  115. /**
  116. * test loading a plugin component.
  117. *
  118. * @return void
  119. */
  120. public function testLoadPluginComponent()
  121. {
  122. Plugin::load('TestPlugin');
  123. $result = $this->Components->load('TestPlugin.Other');
  124. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $result, 'Component class is wrong.');
  125. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $this->Components->Other, 'Class is wrong');
  126. }
  127. /**
  128. * Test loading components with aliases and plugins.
  129. *
  130. * @return void
  131. */
  132. public function testLoadWithAliasAndPlugin()
  133. {
  134. Plugin::load('TestPlugin');
  135. $result = $this->Components->load('AliasedOther', ['className' => 'TestPlugin.Other']);
  136. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $result);
  137. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $this->Components->AliasedOther);
  138. $result = $this->Components->loaded();
  139. $this->assertEquals(['AliasedOther'], $result, 'loaded() results are wrong.');
  140. }
  141. /**
  142. * test getting the controller out of the collection
  143. *
  144. * @return void
  145. */
  146. public function testGetController()
  147. {
  148. $result = $this->Components->getController();
  149. $this->assertInstanceOf('Cake\Controller\Controller', $result);
  150. }
  151. /**
  152. * Test reset.
  153. *
  154. * @return void
  155. */
  156. public function testReset()
  157. {
  158. $eventManager = $this->Components->getController()->getEventManager();
  159. $instance = $this->Components->load('Auth');
  160. $this->assertSame(
  161. $instance,
  162. $this->Components->Auth,
  163. 'Instance in registry should be the same as previously loaded'
  164. );
  165. $this->assertCount(1, $eventManager->listeners('Controller.startup'));
  166. $this->assertNull($this->Components->reset(), 'No return expected');
  167. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  168. $this->assertNotSame($instance, $this->Components->load('Auth'));
  169. }
  170. /**
  171. * Test unloading.
  172. *
  173. * @return void
  174. */
  175. public function testUnload()
  176. {
  177. $eventManager = $this->Components->getController()->getEventManager();
  178. $result = $this->Components->load('Auth');
  179. $this->Components->unload('Auth');
  180. $this->assertFalse(isset($this->Components->Auth), 'Should be gone');
  181. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  182. }
  183. /**
  184. * Test that unloading a none existing component triggers an error.
  185. *
  186. * @expectedException \Cake\Controller\Exception\MissingComponentException
  187. * @expectedExceptionMessage Component class FooComponent could not be found.
  188. * @return void
  189. */
  190. public function testUnloadUnknown()
  191. {
  192. $this->Components->unload('Foo');
  193. }
  194. /**
  195. * Test set.
  196. *
  197. * @return void
  198. */
  199. public function testSet()
  200. {
  201. $eventManager = $this->Components->getController()->getEventManager();
  202. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  203. $auth = new AuthComponent($this->Components);
  204. $this->Components->set('Auth', $auth);
  205. $this->assertTrue(isset($this->Components->Auth), 'Should be gone');
  206. $this->assertCount(1, $eventManager->listeners('Controller.startup'));
  207. }
  208. }