HelperRegistryTest.php 6.0 KB

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