HelperRegistryTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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. public function afterRender($viewFile) {
  28. }
  29. }
  30. /**
  31. * Class HelperRegistryTest
  32. *
  33. */
  34. class HelperRegistryTest extends TestCase {
  35. /**
  36. * setUp
  37. *
  38. * @return void
  39. */
  40. public function setUp() {
  41. parent::setUp();
  42. $this->View = new View();
  43. $this->Events = $this->View->eventManager();
  44. $this->Helpers = new HelperRegistry($this->View);
  45. }
  46. /**
  47. * tearDown
  48. *
  49. * @return void
  50. */
  51. public function tearDown() {
  52. Plugin::unload();
  53. unset($this->Helpers, $this->View);
  54. parent::tearDown();
  55. }
  56. /**
  57. * test loading helpers.
  58. *
  59. * @return void
  60. */
  61. public function testLoad() {
  62. $result = $this->Helpers->load('Html');
  63. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
  64. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $this->Helpers->Html);
  65. $result = $this->Helpers->loaded();
  66. $this->assertEquals(array('Html'), $result, 'loaded() results are wrong.');
  67. }
  68. /**
  69. * test lazy loading of helpers
  70. *
  71. * @return void
  72. */
  73. public function testLazyLoad() {
  74. $result = $this->Helpers->Html;
  75. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
  76. $result = $this->Helpers->Form;
  77. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $result);
  78. $this->View->plugin = 'TestPlugin';
  79. Plugin::load(array('TestPlugin'));
  80. $result = $this->Helpers->OtherHelper;
  81. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result);
  82. }
  83. /**
  84. * test lazy loading of helpers
  85. *
  86. * @expectedException \Cake\View\Exception\MissingHelperException
  87. * @return void
  88. */
  89. public function testLazyLoadException() {
  90. $this->Helpers->NotAHelper;
  91. }
  92. /**
  93. * Test that loading helpers subscribes to events.
  94. *
  95. * @return void
  96. */
  97. public function testLoadSubscribeEvents() {
  98. $this->Helpers->load('Html', array('className' => __NAMESPACE__ . '\HtmlAliasHelper'));
  99. $result = $this->Events->listeners('View.afterRender');
  100. $this->assertCount(1, $result);
  101. }
  102. /**
  103. * Tests loading as an alias
  104. *
  105. * @return void
  106. */
  107. public function testLoadWithAlias() {
  108. $result = $this->Helpers->load('Html', array('className' => __NAMESPACE__ . '\HtmlAliasHelper'));
  109. $this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $result);
  110. $this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $this->Helpers->Html);
  111. $result = $this->Helpers->loaded();
  112. $this->assertEquals(array('Html'), $result, 'loaded() results are wrong.');
  113. $result = $this->Helpers->load('Html');
  114. $this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $result);
  115. }
  116. /**
  117. * Test loading helpers with aliases and plugins.
  118. *
  119. * @return void
  120. */
  121. public function testLoadWithAliasAndPlugin() {
  122. Plugin::load('TestPlugin');
  123. $result = $this->Helpers->load('SomeOther', array('className' => 'TestPlugin.OtherHelper'));
  124. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result);
  125. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $this->Helpers->SomeOther);
  126. $result = $this->Helpers->loaded();
  127. $this->assertEquals(['SomeOther'], $result, 'loaded() results are wrong.');
  128. }
  129. /**
  130. * test that the enabled setting disables the helper.
  131. *
  132. * @return void
  133. */
  134. public function testLoadWithEnabledFalse() {
  135. $result = $this->Helpers->load('Html', array('enabled' => false));
  136. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
  137. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $this->Helpers->Html);
  138. $this->assertEmpty($this->Events->listeners('View.beforeRender'));
  139. }
  140. /**
  141. * test missinghelper exception
  142. *
  143. * @expectedException \Cake\View\Exception\MissingHelperException
  144. * @return void
  145. */
  146. public function testLoadMissingHelper() {
  147. $this->Helpers->load('ThisHelperShouldAlwaysBeMissing');
  148. }
  149. /**
  150. * test loading a plugin helper.
  151. *
  152. * @return void
  153. */
  154. public function testLoadPluginHelper() {
  155. Plugin::load(array('TestPlugin'));
  156. $result = $this->Helpers->load('TestPlugin.OtherHelper');
  157. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result, 'Helper class is wrong.');
  158. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $this->Helpers->OtherHelper, 'Class is wrong');
  159. }
  160. /**
  161. * Test reset.
  162. *
  163. * @return void
  164. */
  165. public function testReset() {
  166. Configure::write('App.namespace', 'TestApp');
  167. $instance = $this->Helpers->load('EventListenerTest');
  168. $this->assertSame(
  169. $instance,
  170. $this->Helpers->EventListenerTest,
  171. 'Instance in registry should be the same as previously loaded'
  172. );
  173. $this->assertCount(1, $this->Events->listeners('View.beforeRender'));
  174. $this->assertNull($this->Helpers->reset(), 'No return expected');
  175. $this->assertCount(0, $this->Events->listeners('View.beforeRender'));
  176. $this->assertNotSame($instance, $this->Helpers->load('EventListenerTest'));
  177. }
  178. /**
  179. * Test unloading.
  180. *
  181. * @return void
  182. */
  183. public function testUnload() {
  184. Configure::write('App.namespace', 'TestApp');
  185. $instance = $this->Helpers->load('EventListenerTest');
  186. $this->assertSame(
  187. $instance,
  188. $this->Helpers->EventListenerTest,
  189. 'Instance in registry should be the same as previously loaded'
  190. );
  191. $this->assertCount(1, $this->Events->listeners('View.beforeRender'));
  192. $this->assertNull($this->Helpers->unload('EventListenerTest'), 'No return expected');
  193. $this->assertCount(0, $this->Events->listeners('View.beforeRender'));
  194. }
  195. }