HelperRegistryTest.php 9.7 KB

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