ComponentTest.php 9.9 KB

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