HelperRegistryTest.php 9.8 KB

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