ComponentTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  11. * @since 1.2.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Controller;
  15. use Cake\Controller\ComponentRegistry;
  16. use Cake\Controller\Component\CookieComponent;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\Configure;
  19. use Cake\Event\EventManager;
  20. use Cake\TestSuite\TestCase;
  21. use TestApp\Controller\ComponentTestController;
  22. use TestApp\Controller\Component\AppleComponent;
  23. use TestApp\Controller\Component\BananaComponent;
  24. use TestApp\Controller\Component\ConfiguredComponent;
  25. use TestApp\Controller\Component\OrangeComponent;
  26. use TestApp\Controller\Component\SomethingWithCookieComponent;
  27. /**
  28. * ComponentTest class
  29. */
  30. class ComponentTest extends TestCase
  31. {
  32. /**
  33. * setUp method
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. parent::setUp();
  40. Configure::write('App.namespace', 'TestApp');
  41. }
  42. /**
  43. * test accessing inner components.
  44. *
  45. * @return void
  46. */
  47. public function testInnerComponentConstruction()
  48. {
  49. $Collection = new ComponentRegistry();
  50. $Component = new AppleComponent($Collection);
  51. $this->assertInstanceOf(OrangeComponent::class, $Component->Orange, 'class is wrong');
  52. }
  53. /**
  54. * test component loading
  55. *
  56. * @return void
  57. */
  58. public function testNestedComponentLoading()
  59. {
  60. $Collection = new ComponentRegistry();
  61. $Apple = new AppleComponent($Collection);
  62. $this->assertInstanceOf(OrangeComponent::class, $Apple->Orange, 'class is wrong');
  63. $this->assertInstanceOf(BananaComponent::class, $Apple->Orange->Banana, 'class is wrong');
  64. $this->assertEmpty($Apple->Session);
  65. $this->assertEmpty($Apple->Orange->Session);
  66. }
  67. /**
  68. * test that component components are not enabled in the collection.
  69. *
  70. * @return void
  71. */
  72. public function testInnerComponentsAreNotEnabled()
  73. {
  74. $mock = $this->getMockBuilder(EventManager::class)->getMock();
  75. $controller = new Controller();
  76. $controller->setEventManager($mock);
  77. $mock->expects($this->once())
  78. ->method('on')
  79. ->with($this->isInstanceOf(AppleComponent::class));
  80. $Collection = new ComponentRegistry($controller);
  81. $Apple = $Collection->load('Apple');
  82. $this->assertInstanceOf(OrangeComponent::class, $Apple->Orange, 'class is wrong');
  83. }
  84. /**
  85. * test a component being used more than once.
  86. *
  87. * @return void
  88. */
  89. public function testMultipleComponentInitialize()
  90. {
  91. $Collection = new ComponentRegistry();
  92. $Banana = $Collection->load('Banana');
  93. $Orange = $Collection->load('Orange');
  94. $this->assertSame($Banana, $Orange->Banana, 'Should be references');
  95. $Banana->testField = 'OrangeField';
  96. $this->assertSame($Banana->testField, $Orange->Banana->testField, 'References are broken');
  97. }
  98. /**
  99. * Test a duplicate component being loaded more than once with same and differing configurations.
  100. *
  101. * @expectedException \RuntimeException
  102. * @expectedExceptionMessage The "Banana" alias has already been loaded with the following config:
  103. * @return void
  104. */
  105. public function testDuplicateComponentInitialize()
  106. {
  107. $Collection = new ComponentRegistry();
  108. $Collection->load('Banana', ['property' => ['closure' => function () {
  109. }]]);
  110. $Collection->load('Banana', ['property' => ['closure' => function () {
  111. }]]);
  112. $this->assertInstanceOf(BananaComponent::class, $Collection->Banana, 'class is wrong');
  113. $Collection->load('Banana', ['property' => ['differs']]);
  114. }
  115. /**
  116. * Test mutually referencing components.
  117. *
  118. * @return void
  119. */
  120. public function testSomethingReferencingCookieComponent()
  121. {
  122. $Controller = new ComponentTestController();
  123. $Controller->loadComponent('SomethingWithCookie');
  124. $Controller->startupProcess();
  125. $this->assertInstanceOf(SomethingWithCookieComponent::class, $Controller->SomethingWithCookie);
  126. $this->assertInstanceOf(CookieComponent::class, $Controller->SomethingWithCookie->Cookie);
  127. }
  128. /**
  129. * Tests __debugInfo
  130. *
  131. * @return void
  132. */
  133. public function testDebugInfo()
  134. {
  135. $Collection = new ComponentRegistry();
  136. $Component = new AppleComponent($Collection);
  137. $expected = [
  138. 'components' => [
  139. 'Orange'
  140. ],
  141. 'implementedEvents' => [
  142. 'Controller.startup' => 'startup'
  143. ],
  144. '_config' => []
  145. ];
  146. $result = $Component->__debugInfo();
  147. $this->assertEquals($expected, $result);
  148. }
  149. /**
  150. * Tests null return for unknown magic properties.
  151. *
  152. * @return void
  153. */
  154. public function testMagicReturnsNull()
  155. {
  156. $Component = new AppleComponent(new ComponentRegistry());
  157. $this->assertNull($Component->ShouldBeNull);
  158. }
  159. /**
  160. * Tests config via constructor
  161. *
  162. * @return void
  163. */
  164. public function testConfigViaConstructor()
  165. {
  166. $Component = new ConfiguredComponent(new ComponentRegistry(), ['chicken' => 'soup']);
  167. $this->assertEquals(['chicken' => 'soup'], $Component->configCopy);
  168. $this->assertEquals(['chicken' => 'soup'], $Component->config());
  169. }
  170. /**
  171. * Lazy load a component without events.
  172. *
  173. * @return void
  174. */
  175. public function testLazyLoading()
  176. {
  177. $Component = new ConfiguredComponent(new ComponentRegistry(), [], ['Apple', 'Banana', 'Orange']);
  178. $this->assertInstanceOf(AppleComponent::class, $Component->Apple, 'class is wrong');
  179. $this->assertInstanceOf(OrangeComponent::class, $Component->Orange, 'class is wrong');
  180. $this->assertInstanceOf(BananaComponent::class, $Component->Banana, 'class is wrong');
  181. }
  182. /**
  183. * Lazy load a component that does not exist.
  184. *
  185. * @expectedException \Cake\Controller\Exception\MissingComponentException
  186. * @expectedExceptionMessage Component class YouHaveNoBananasComponent could not be found.
  187. * @return void
  188. */
  189. public function testLazyLoadingDoesNotExists()
  190. {
  191. $Component = new ConfiguredComponent(new ComponentRegistry(), [], ['YouHaveNoBananas']);
  192. $bananas = $Component->YouHaveNoBananas;
  193. }
  194. /**
  195. * Lazy loaded components can have config options
  196. *
  197. * @return void
  198. */
  199. public function testConfiguringInnerComponent()
  200. {
  201. $Component = new ConfiguredComponent(new ComponentRegistry(), [], ['Configured' => ['foo' => 'bar']]);
  202. $this->assertInstanceOf(ConfiguredComponent::class, $Component->Configured, 'class is wrong');
  203. $this->assertNotSame($Component, $Component->Configured, 'Component instance was reused');
  204. $this->assertEquals(['foo' => 'bar', 'enabled' => false], $Component->Configured->config());
  205. }
  206. /**
  207. * Test enabling events for lazy loaded components
  208. *
  209. * @return void
  210. */
  211. public function testEventsInnerComponent()
  212. {
  213. $eventManager = $this->getMockBuilder(EventManager::class)->getMock();
  214. $eventManager->expects($this->once())
  215. ->method('on')
  216. ->with($this->isInstanceOf(AppleComponent::class));
  217. $controller = new Controller();
  218. $controller->setEventManager($eventManager);
  219. $Collection = new ComponentRegistry($controller);
  220. $Component = new ConfiguredComponent($Collection, [], ['Apple' => ['enabled' => true]]);
  221. $this->assertInstanceOf(AppleComponent::class, $Component->Apple, 'class is wrong');
  222. }
  223. /**
  224. * Disabled events do not register for event listeners.
  225. *
  226. * @return void
  227. */
  228. public function testNoEventsInnerComponent()
  229. {
  230. $eventManager = $this->getMockBuilder(EventManager::class)->getMock();
  231. $eventManager->expects($this->never())->method('on');
  232. $controller = new Controller();
  233. $controller->setEventManager($eventManager);
  234. $Collection = new ComponentRegistry($controller);
  235. $Component = new ConfiguredComponent($Collection, [], ['Apple' => ['enabled' => false]]);
  236. $this->assertInstanceOf(AppleComponent::class, $Component->Apple, 'class is wrong');
  237. }
  238. }