ComponentRegistryTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Controller;
  17. use Cake\Controller\Component\FlashComponent;
  18. use Cake\Controller\Component\FormProtectionComponent;
  19. use Cake\Controller\ComponentRegistry;
  20. use Cake\Controller\Controller;
  21. use Cake\Controller\Exception\MissingComponentException;
  22. use Cake\Core\Container;
  23. use Cake\Event\EventManager;
  24. use Cake\Http\ServerRequest;
  25. use Cake\TestSuite\TestCase;
  26. use Countable;
  27. use Exception;
  28. use League\Container\ReflectionContainer;
  29. use TestApp\Controller\Component\ConfiguredComponent;
  30. use TestApp\Controller\Component\FlashAliasComponent;
  31. use TestPlugin\Controller\Component\OtherComponent;
  32. use Traversable;
  33. class ComponentRegistryTest extends TestCase
  34. {
  35. /**
  36. * @var \Cake\Controller\ComponentRegistry
  37. */
  38. protected $Components;
  39. private bool $created = false;
  40. /**
  41. * setUp
  42. */
  43. public function setUp(): void
  44. {
  45. parent::setUp();
  46. $controller = new Controller(new ServerRequest());
  47. $this->Components = new ComponentRegistry($controller);
  48. }
  49. /**
  50. * tearDown
  51. */
  52. public function tearDown(): void
  53. {
  54. parent::tearDown();
  55. unset($this->Components);
  56. $this->clearPlugins();
  57. }
  58. /**
  59. * test triggering callbacks on loaded helpers
  60. */
  61. public function testLoad(): void
  62. {
  63. $result = $this->Components->load('Flash');
  64. $this->assertInstanceOf(FlashComponent::class, $result);
  65. $this->assertInstanceOf(FlashComponent::class, $this->Components->Flash);
  66. $result = $this->Components->loaded();
  67. $this->assertEquals(['Flash'], $result, 'loaded() results are wrong.');
  68. $result = $this->Components->load('Flash');
  69. $this->assertSame($result, $this->Components->Flash);
  70. }
  71. /**
  72. * test load() with the container set
  73. */
  74. public function testLoadWithContainer(): void
  75. {
  76. $controller = new Controller(new ServerRequest());
  77. $container = new Container();
  78. $components = new ComponentRegistry($controller, $container);
  79. $this->assertEquals([], $components->loaded());
  80. $container->add(ComponentRegistry::class, $components);
  81. $container->add(FlashComponent::class, function (ComponentRegistry $registry, array $config) {
  82. $this->created = true;
  83. return new FlashComponent($registry, $config);
  84. })
  85. ->addArgument(ComponentRegistry::class)
  86. ->addArgument(['key' => 'customFlash']);
  87. $flash = $components->load('Flash');
  88. // Container was modified for the current registry and our factory was called
  89. $this->assertTrue($container->has(ComponentRegistry::class));
  90. $this->assertTrue($this->created);
  91. $this->assertInstanceOf(FlashComponent::class, $flash);
  92. $this->assertSame('customFlash', $flash->getConfig('key'));
  93. }
  94. public function testLoadWithContainerAutoWiring(): void
  95. {
  96. $controller = new Controller(new ServerRequest());
  97. $container = new Container();
  98. $container->delegate(new ReflectionContainer());
  99. $components = new ComponentRegistry($controller, $container);
  100. $container->add(ComponentRegistry::class, $components);
  101. $component = $components->load(ConfiguredComponent::class, ['key' => 'customFlash']);
  102. $this->assertInstanceOf(ConfiguredComponent::class, $component);
  103. $this->assertSame(['key' => 'customFlash'], $component->configCopy);
  104. }
  105. /**
  106. * Tests loading as an alias
  107. */
  108. public function testLoadWithAlias(): void
  109. {
  110. $result = $this->Components->load('Flash', ['className' => FlashAliasComponent::class, 'somesetting' => true]);
  111. $this->assertInstanceOf(FlashAliasComponent::class, $result);
  112. $this->assertInstanceOf(FlashAliasComponent::class, $this->Components->Flash);
  113. $this->assertTrue($this->Components->Flash->getConfig('somesetting'));
  114. $result = $this->Components->loaded();
  115. $this->assertEquals(['Flash'], $result, 'loaded() results are wrong.');
  116. $result = $this->Components->load('Flash');
  117. $this->assertInstanceOf(FlashAliasComponent::class, $result);
  118. $this->loadPlugins(['TestPlugin']);
  119. $result = $this->Components->load('SomeOther', ['className' => 'TestPlugin.Other']);
  120. $this->assertInstanceOf(OtherComponent::class, $result);
  121. $this->assertInstanceOf(OtherComponent::class, $this->Components->SomeOther);
  122. $result = $this->Components->loaded();
  123. $this->assertEquals(['Flash', 'SomeOther'], $result, 'loaded() results are wrong.');
  124. }
  125. /**
  126. * test load and enable = false
  127. */
  128. public function testLoadWithEnableFalse(): void
  129. {
  130. $eventManager = new class extends EventManager {
  131. public function on($eventKey, $options = null, $callable = []): never
  132. {
  133. throw new Exception('Should not be called');
  134. }
  135. };
  136. $this->Components->getController()->setEventManager($eventManager);
  137. $result = $this->Components->load('Flash', ['enabled' => false]);
  138. $this->assertInstanceOf(FlashComponent::class, $result);
  139. $this->assertInstanceOf(FlashComponent::class, $this->Components->Flash);
  140. }
  141. /**
  142. * test MissingComponent exception
  143. */
  144. public function testLoadMissingComponent(): void
  145. {
  146. $this->expectException(MissingComponentException::class);
  147. $this->Components->load('ThisComponentShouldAlwaysBeMissing');
  148. }
  149. /**
  150. * test loading a plugin component.
  151. */
  152. public function testLoadPluginComponent(): void
  153. {
  154. $this->loadPlugins(['TestPlugin']);
  155. $result = $this->Components->load('TestPlugin.Other');
  156. $this->assertInstanceOf(OtherComponent::class, $result, 'Component class is wrong.');
  157. $this->assertInstanceOf(OtherComponent::class, $this->Components->Other, 'Class is wrong');
  158. }
  159. /**
  160. * Test loading components with aliases and plugins.
  161. */
  162. public function testLoadWithAliasAndPlugin(): void
  163. {
  164. $this->loadPlugins(['TestPlugin']);
  165. $result = $this->Components->load('AliasedOther', ['className' => 'TestPlugin.Other']);
  166. $this->assertInstanceOf(OtherComponent::class, $result);
  167. $this->assertInstanceOf(OtherComponent::class, $this->Components->AliasedOther);
  168. $result = $this->Components->loaded();
  169. $this->assertEquals(['AliasedOther'], $result, 'loaded() results are wrong.');
  170. }
  171. /**
  172. * test getting the controller out of the collection
  173. */
  174. public function testGetController(): void
  175. {
  176. $result = $this->Components->getController();
  177. $this->assertInstanceOf(Controller::class, $result);
  178. }
  179. /**
  180. * Test reset.
  181. */
  182. public function testReset(): void
  183. {
  184. $eventManager = $this->Components->getController()->getEventManager();
  185. $instance = $this->Components->load('FormProtection');
  186. $this->assertSame(
  187. $instance,
  188. $this->Components->FormProtection,
  189. 'Instance in registry should be the same as previously loaded'
  190. );
  191. $this->assertCount(1, $eventManager->listeners('Controller.startup'));
  192. $this->assertSame($this->Components, $this->Components->reset());
  193. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  194. $this->assertNotSame($instance, $this->Components->load('FormProtection'));
  195. }
  196. /**
  197. * Test unloading.
  198. */
  199. public function testUnload(): void
  200. {
  201. $eventManager = $this->Components->getController()->getEventManager();
  202. $this->Components->load('FormProtection');
  203. $result = $this->Components->unload('FormProtection');
  204. $this->assertSame($this->Components, $result);
  205. $this->assertFalse(isset($this->Components->FormProtection), 'Should be gone');
  206. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  207. }
  208. /**
  209. * Test __unset.
  210. */
  211. public function testUnset(): void
  212. {
  213. $eventManager = $this->Components->getController()->getEventManager();
  214. $this->Components->load('FormProtection');
  215. unset($this->Components->FormProtection);
  216. $this->assertFalse(isset($this->Components->FormProtection), 'Should be gone');
  217. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  218. }
  219. /**
  220. * Test that unloading a none existing component triggers an error.
  221. */
  222. public function testUnloadUnknown(): void
  223. {
  224. $this->expectException(MissingComponentException::class);
  225. $this->expectExceptionMessage('Component class `FooComponent` could not be found.');
  226. $this->Components->unload('Foo');
  227. }
  228. /**
  229. * Test set.
  230. */
  231. public function testSet(): void
  232. {
  233. $eventManager = $this->Components->getController()->getEventManager();
  234. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  235. $formProtection = new FormProtectionComponent($this->Components);
  236. $result = $this->Components->set('FormProtection', $formProtection);
  237. $this->assertEquals($this->Components, $result);
  238. $this->assertTrue(isset($this->Components->FormProtection), 'Should be present');
  239. $this->assertCount(1, $eventManager->listeners('Controller.startup'));
  240. }
  241. /**
  242. * Test __set.
  243. */
  244. public function testMagicSet(): void
  245. {
  246. $eventManager = $this->Components->getController()->getEventManager();
  247. $this->assertCount(0, $eventManager->listeners('Controller.startup'));
  248. $formProtection = new FormProtectionComponent($this->Components);
  249. $this->Components->FormProtection = $formProtection;
  250. $this->assertTrue(isset($this->Components->FormProtection), 'Should be present');
  251. $this->assertCount(1, $eventManager->listeners('Controller.startup'));
  252. }
  253. /**
  254. * Test Countable.
  255. */
  256. public function testCountable(): void
  257. {
  258. $this->Components->load('FormProtection');
  259. $this->assertInstanceOf(Countable::class, $this->Components);
  260. $count = count($this->Components);
  261. $this->assertSame(1, $count);
  262. }
  263. /**
  264. * Test Traversable.
  265. */
  266. public function testTraversable(): void
  267. {
  268. $this->Components->load('FormProtection');
  269. $this->assertInstanceOf(Traversable::class, $this->Components);
  270. $result = null;
  271. foreach ($this->Components as $component) {
  272. $result = $component;
  273. }
  274. $this->assertNotNull($result);
  275. }
  276. }