ComponentRegistryTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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\TestSuite\TestCase;
  23. /**
  24. * Extended CookieComponent
  25. */
  26. class CookieAliasComponent extends CookieComponent {
  27. }
  28. class ComponentRegistryTest extends TestCase {
  29. /**
  30. * setUp
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. $controller = new Controller();
  37. $this->Components = new ComponentRegistry($controller);
  38. }
  39. /**
  40. * tearDown
  41. *
  42. * @return void
  43. */
  44. public function tearDown() {
  45. parent::tearDown();
  46. unset($this->Components);
  47. }
  48. /**
  49. * test triggering callbacks on loaded helpers
  50. *
  51. * @return void
  52. */
  53. public function testLoad() {
  54. $result = $this->Components->load('Cookie');
  55. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $result);
  56. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $this->Components->Cookie);
  57. $result = $this->Components->loaded();
  58. $this->assertEquals(array('Cookie'), $result, 'loaded() results are wrong.');
  59. $result = $this->Components->load('Cookie');
  60. $this->assertSame($result, $this->Components->Cookie);
  61. }
  62. /**
  63. * Tests loading as an alias
  64. *
  65. * @return void
  66. */
  67. public function testLoadWithAlias() {
  68. $result = $this->Components->load('Cookie', array('className' => __NAMESPACE__ . '\CookieAliasComponent', 'somesetting' => true));
  69. $this->assertInstanceOf(__NAMESPACE__ . '\CookieAliasComponent', $result);
  70. $this->assertInstanceOf(__NAMESPACE__ . '\CookieAliasComponent', $this->Components->Cookie);
  71. $this->assertTrue($this->Components->Cookie->config('somesetting'));
  72. $result = $this->Components->loaded();
  73. $this->assertEquals(array('Cookie'), $result, 'loaded() results are wrong.');
  74. $result = $this->Components->load('Cookie');
  75. $this->assertInstanceOf(__NAMESPACE__ . '\CookieAliasComponent', $result);
  76. Plugin::load('TestPlugin');
  77. $result = $this->Components->load('SomeOther', array('className' => 'TestPlugin.Other'));
  78. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $result);
  79. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $this->Components->SomeOther);
  80. $result = $this->Components->loaded();
  81. $this->assertEquals(array('Cookie', 'SomeOther'), $result, 'loaded() results are wrong.');
  82. }
  83. /**
  84. * test load and enable = false
  85. *
  86. * @return void
  87. */
  88. public function testLoadWithEnableFalse() {
  89. $mock = $this->getMock('Cake\Event\EventManager');
  90. $mock->expects($this->never())
  91. ->method('attach');
  92. $this->Components->getController()->setEventManager($mock);
  93. $result = $this->Components->load('Cookie', array('enabled' => false));
  94. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $result);
  95. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $this->Components->Cookie);
  96. }
  97. /**
  98. * test missingcomponent exception
  99. *
  100. * @expectedException \Cake\Controller\Error\MissingComponentException
  101. * @return void
  102. */
  103. public function testLoadMissingComponent() {
  104. $this->Components->load('ThisComponentShouldAlwaysBeMissing');
  105. }
  106. /**
  107. * test loading a plugin component.
  108. *
  109. * @return void
  110. */
  111. public function testLoadPluginComponent() {
  112. Plugin::load('TestPlugin');
  113. $result = $this->Components->load('TestPlugin.Other');
  114. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $result, 'Component class is wrong.');
  115. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $this->Components->Other, 'Class is wrong');
  116. }
  117. /**
  118. * Test loading components with aliases and plugins.
  119. *
  120. * @return void
  121. */
  122. public function testLoadWithAliasAndPlugin() {
  123. Plugin::load('TestPlugin');
  124. $result = $this->Components->load('AliasedOther', ['className' => 'TestPlugin.Other']);
  125. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $result);
  126. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $this->Components->AliasedOther);
  127. $result = $this->Components->loaded();
  128. $this->assertEquals(['AliasedOther'], $result, 'loaded() results are wrong.');
  129. }
  130. /**
  131. * test getting the controller out of the collection
  132. *
  133. * @return void
  134. */
  135. public function testGetController() {
  136. $result = $this->Components->getController();
  137. $this->assertInstanceOf('Cake\Controller\Controller', $result);
  138. }
  139. /**
  140. * Test reset.
  141. *
  142. * @return void
  143. */
  144. public function testReset() {
  145. $eventManager = $this->Components->getController()->getEventManager();
  146. $instance = $this->Components->load('Auth');
  147. $this->assertSame(
  148. $instance,
  149. $this->Components->Auth,
  150. 'Instance in registry should be the same as previously loaded'
  151. );
  152. $this->assertCount(1, $eventManager->listeners('Controller.startup'));
  153. $this->assertNull($this->Components->reset(), 'No return expected');
  154. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  155. $this->assertNotSame($instance, $this->Components->load('Auth'));
  156. }
  157. /**
  158. * Test unloading.
  159. *
  160. * @return void
  161. */
  162. public function testUnload() {
  163. $eventManager = $this->Components->getController()->getEventManager();
  164. $result = $this->Components->load('Auth');
  165. $this->Components->unload('Auth');
  166. $this->assertFalse(isset($this->Components->Auth), 'Should be gone');
  167. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  168. }
  169. /**
  170. * Test set.
  171. *
  172. * @return void
  173. */
  174. public function testSet() {
  175. $eventManager = $this->Components->getController()->getEventManager();
  176. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  177. $auth = new AuthComponent($this->Components);
  178. $this->Components->set('Auth', $auth);
  179. $this->assertTrue(isset($this->Components->Auth), 'Should be gone');
  180. $this->assertCount(1, $eventManager->listeners('Controller.startup'));
  181. }
  182. }