ComponentTest.php 8.6 KB

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