HelperRegistryTest.php 9.7 KB

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