ComponentRegistryTest.php 5.5 KB

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