ComponentRegistryTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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\App;
  21. use Cake\Core\Plugin;
  22. use Cake\Network\Request;
  23. use Cake\Network\Response;
  24. use Cake\TestSuite\TestCase;
  25. /**
  26. * Extended CookieComponent
  27. */
  28. class CookieAliasComponent extends CookieComponent
  29. {
  30. }
  31. class ComponentRegistryTest extends TestCase
  32. {
  33. /**
  34. * setUp
  35. *
  36. * @return void
  37. */
  38. public function setUp()
  39. {
  40. parent::setUp();
  41. $controller = new Controller(new Request(), new Response());
  42. $this->Components = new ComponentRegistry($controller);
  43. }
  44. /**
  45. * tearDown
  46. *
  47. * @return void
  48. */
  49. public function tearDown()
  50. {
  51. parent::tearDown();
  52. unset($this->Components);
  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->config('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. Plugin::load('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->getMock('Cake\Event\EventManager');
  99. $mock->expects($this->never())
  100. ->method('attach');
  101. $this->Components->getController()->eventManager($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. * @expectedException \Cake\Controller\Exception\MissingComponentException
  110. * @return void
  111. */
  112. public function testLoadMissingComponent()
  113. {
  114. $this->Components->load('ThisComponentShouldAlwaysBeMissing');
  115. }
  116. /**
  117. * test loading a plugin component.
  118. *
  119. * @return void
  120. */
  121. public function testLoadPluginComponent()
  122. {
  123. Plugin::load('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. Plugin::load('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()->eventManager();
  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->assertNull($this->Components->reset(), 'No return expected');
  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()->eventManager();
  179. $result = $this->Components->load('Auth');
  180. $this->Components->unload('Auth');
  181. $this->assertFalse(isset($this->Components->Auth), 'Should be gone');
  182. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  183. }
  184. /**
  185. * Test set.
  186. *
  187. * @return void
  188. */
  189. public function testSet()
  190. {
  191. $eventManager = $this->Components->getController()->eventManager();
  192. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  193. $auth = new AuthComponent($this->Components);
  194. $this->Components->set('Auth', $auth);
  195. $this->assertTrue(isset($this->Components->Auth), 'Should be gone');
  196. $this->assertCount(1, $eventManager->listeners('Controller.startup'));
  197. }
  198. }