PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/TestCase/View/HelperRegistryTest.php

https://gitlab.com/18runt88/cakephp
PHP | 334 lines | 169 code | 38 blank | 127 comment | 0 complexity | e6fccd456cc54395b815c2348b71d306 MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\View\Helper;
  20. use Cake\View\HelperRegistry;
  21. use Cake\View\View;
  22. /**
  23. * Extended HtmlHelper
  24. */
  25. class HtmlAliasHelper extends Helper
  26. {
  27. public function afterRender($viewFile)
  28. {
  29. }
  30. }
  31. /**
  32. * Class HelperRegistryTest
  33. *
  34. */
  35. class HelperRegistryTest extends TestCase
  36. {
  37. /**
  38. * setUp
  39. *
  40. * @return void
  41. */
  42. public function setUp()
  43. {
  44. parent::setUp();
  45. $this->View = new View();
  46. $this->Events = $this->View->eventManager();
  47. $this->Helpers = new HelperRegistry($this->View);
  48. }
  49. /**
  50. * tearDown
  51. *
  52. * @return void
  53. */
  54. public function tearDown()
  55. {
  56. Plugin::unload();
  57. unset($this->Helpers, $this->View);
  58. parent::tearDown();
  59. }
  60. /**
  61. * test loading helpers.
  62. *
  63. * @return void
  64. */
  65. public function testLoad()
  66. {
  67. $result = $this->Helpers->load('Html');
  68. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
  69. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $this->Helpers->Html);
  70. $result = $this->Helpers->loaded();
  71. $this->assertEquals(['Html'], $result, 'loaded() results are wrong.');
  72. }
  73. /**
  74. * test lazy loading of helpers
  75. *
  76. * @return void
  77. */
  78. public function testLazyLoad()
  79. {
  80. $result = $this->Helpers->Html;
  81. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
  82. $result = $this->Helpers->Form;
  83. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $result);
  84. $this->View->plugin = 'TestPlugin';
  85. Plugin::load(['TestPlugin']);
  86. $result = $this->Helpers->OtherHelper;
  87. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result);
  88. }
  89. /**
  90. * test lazy loading of helpers
  91. *
  92. * @expectedException \Cake\View\Exception\MissingHelperException
  93. * @return void
  94. */
  95. public function testLazyLoadException()
  96. {
  97. $this->Helpers->NotAHelper;
  98. }
  99. /**
  100. * Test that loading helpers subscribes to events.
  101. *
  102. * @return void
  103. */
  104. public function testLoadSubscribeEvents()
  105. {
  106. $this->Helpers->load('Html', ['className' => __NAMESPACE__ . '\HtmlAliasHelper']);
  107. $result = $this->Events->listeners('View.afterRender');
  108. $this->assertCount(1, $result);
  109. }
  110. /**
  111. * Tests loading as an alias
  112. *
  113. * @return void
  114. */
  115. public function testLoadWithAlias()
  116. {
  117. $result = $this->Helpers->load('Html', ['className' => __NAMESPACE__ . '\HtmlAliasHelper']);
  118. $this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $result);
  119. $this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $this->Helpers->Html);
  120. $result = $this->Helpers->loaded();
  121. $this->assertEquals(['Html'], $result, 'loaded() results are wrong.');
  122. $result = $this->Helpers->load('Html');
  123. $this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $result);
  124. }
  125. /**
  126. * Test loading helpers with aliases and plugins.
  127. *
  128. * @return void
  129. */
  130. public function testLoadWithAliasAndPlugin()
  131. {
  132. Plugin::load('TestPlugin');
  133. $result = $this->Helpers->load('SomeOther', ['className' => 'TestPlugin.OtherHelper']);
  134. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result);
  135. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $this->Helpers->SomeOther);
  136. $result = $this->Helpers->loaded();
  137. $this->assertEquals(['SomeOther'], $result, 'loaded() results are wrong.');
  138. }
  139. /**
  140. * test that the enabled setting disables the helper.
  141. *
  142. * @return void
  143. */
  144. public function testLoadWithEnabledFalse()
  145. {
  146. $result = $this->Helpers->load('Html', ['enabled' => false]);
  147. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
  148. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $this->Helpers->Html);
  149. $this->assertEmpty($this->Events->listeners('View.beforeRender'));
  150. }
  151. /**
  152. * test missinghelper exception
  153. *
  154. * @expectedException \Cake\View\Exception\MissingHelperException
  155. * @return void
  156. */
  157. public function testLoadMissingHelper()
  158. {
  159. $this->Helpers->load('ThisHelperShouldAlwaysBeMissing');
  160. }
  161. /**
  162. * test loading a plugin helper.
  163. *
  164. * @return void
  165. */
  166. public function testLoadPluginHelper()
  167. {
  168. Plugin::load(['TestPlugin']);
  169. $result = $this->Helpers->load('TestPlugin.OtherHelper');
  170. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result, 'Helper class is wrong.');
  171. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $this->Helpers->OtherHelper, 'Class is wrong');
  172. }
  173. /**
  174. * test loading helpers with dotted aliases
  175. *
  176. * @return void
  177. */
  178. public function testLoadPluginHelperDottedAlias()
  179. {
  180. Plugin::load(['TestPlugin']);
  181. $result = $this->Helpers->load('thing.helper', [
  182. 'className' => 'TestPlugin.OtherHelper',
  183. ]);
  184. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result, 'Helper class is wrong.');
  185. $this->assertInstanceOf(
  186. 'TestPlugin\View\Helper\OtherHelperHelper',
  187. $this->Helpers->get('thing.helper'),
  188. 'Class is wrong'
  189. );
  190. $this->assertTrue($this->Helpers->has('thing.helper'));
  191. $this->assertFalse($this->Helpers->has('thing'));
  192. $this->assertFalse($this->Helpers->has('helper'));
  193. $this->Helpers->unload('thing.helper');
  194. $this->assertFalse($this->Helpers->has('thing.helper'), 'Should be gone now.');
  195. }
  196. /**
  197. * Test reset.
  198. *
  199. * @return void
  200. */
  201. public function testReset()
  202. {
  203. Configure::write('App.namespace', 'TestApp');
  204. $instance = $this->Helpers->load('EventListenerTest');
  205. $this->assertSame(
  206. $instance,
  207. $this->Helpers->EventListenerTest,
  208. 'Instance in registry should be the same as previously loaded'
  209. );
  210. $this->assertCount(1, $this->Events->listeners('View.beforeRender'));
  211. $this->assertNull($this->Helpers->reset(), 'No return expected');
  212. $this->assertCount(0, $this->Events->listeners('View.beforeRender'));
  213. $this->assertNotSame($instance, $this->Helpers->load('EventListenerTest'));
  214. }
  215. /**
  216. * Test unloading.
  217. *
  218. * @return void
  219. */
  220. public function testUnload()
  221. {
  222. Configure::write('App.namespace', 'TestApp');
  223. $instance = $this->Helpers->load('EventListenerTest');
  224. $this->assertSame(
  225. $instance,
  226. $this->Helpers->EventListenerTest,
  227. 'Instance in registry should be the same as previously loaded'
  228. );
  229. $this->assertCount(1, $this->Events->listeners('View.beforeRender'));
  230. $this->assertNull($this->Helpers->unload('EventListenerTest'), 'No return expected');
  231. $this->assertCount(0, $this->Events->listeners('View.beforeRender'));
  232. }
  233. /**
  234. * Loading a helper with no config should "just work"
  235. *
  236. * The addToAssertionCount call is to record that no exception was thrown
  237. *
  238. * @return void
  239. */
  240. public function testLoadMultipleTimesNoConfig()
  241. {
  242. $this->Helpers->load('Html');
  243. $this->Helpers->load('Html');
  244. $this->addToAssertionCount(1);
  245. }
  246. /**
  247. * Loading a helper with bespoke config, where the subsequent load specifies no
  248. * config should "just work"
  249. *
  250. * The addToAssertionCount call is to record that no exception was thrown
  251. *
  252. * @return void
  253. */
  254. public function testLoadMultipleTimesAlreadyConfigured()
  255. {
  256. $this->Helpers->load('Html', ['same' => 'stuff']);
  257. $this->Helpers->load('Html');
  258. $this->addToAssertionCount(1);
  259. }
  260. /**
  261. * Loading a helper overriding defaults to default value
  262. * should "just work"
  263. *
  264. * @return void
  265. */
  266. public function testLoadMultipleTimesDefaultConfigValuesWorks()
  267. {
  268. $this->Helpers->load('Number', ['engine' => 'Cake\I18n\Number']);
  269. $this->Helpers->load('Number');
  270. $this->addToAssertionCount(1);
  271. }
  272. /**
  273. * Loading a helper with different config, should throw an exception
  274. *
  275. * @expectedException RuntimeException
  276. * @expectedExceptionMessage The "Html" alias has already been loaded with the following
  277. * @return void
  278. */
  279. public function testLoadMultipleTimesDifferentConfigured()
  280. {
  281. $this->Helpers->load('Html');
  282. $this->Helpers->load('Html', ['same' => 'stuff']);
  283. }
  284. /**
  285. * Loading a helper with different config, should throw an exception
  286. *
  287. * @expectedException RuntimeException
  288. * @expectedExceptionMessage The "Html" alias has already been loaded with the following
  289. * @return void
  290. */
  291. public function testLoadMultipleTimesDifferentConfigValues()
  292. {
  293. $this->Helpers->load('Html', ['key' => 'value']);
  294. $this->Helpers->load('Html', ['key' => 'new value']);
  295. }
  296. }