ComponentRegistryTest.php 6.5 KB

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