ComponentTest.php 9.0 KB

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