HelperTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * HelperTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 1.2.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Routing\Router;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\View\Helper;
  23. use Cake\View\View;
  24. class TestHelper extends Helper
  25. {
  26. /**
  27. * Settings for this helper.
  28. *
  29. * @var array
  30. */
  31. protected $_defaultConfig = [
  32. 'key1' => 'val1',
  33. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'val2.2'],
  34. ];
  35. /**
  36. * Helpers for this helper.
  37. *
  38. * @var array
  39. */
  40. public $helpers = ['Html', 'TestPlugin.OtherHelper'];
  41. }
  42. /**
  43. * HelperTest class
  44. */
  45. class HelperTest extends TestCase
  46. {
  47. /**
  48. * @var \Cake\View\View
  49. */
  50. public $View;
  51. /**
  52. * setUp method
  53. *
  54. * @return void
  55. */
  56. public function setUp()
  57. {
  58. parent::setUp();
  59. Router::reload();
  60. $this->View = new View();
  61. }
  62. /**
  63. * tearDown method
  64. *
  65. * @return void
  66. */
  67. public function tearDown()
  68. {
  69. parent::tearDown();
  70. Configure::delete('Asset');
  71. $this->clearPlugins();
  72. unset($this->View);
  73. }
  74. /**
  75. * Test settings merging
  76. *
  77. * @return void
  78. */
  79. public function testSettingsMerging()
  80. {
  81. $Helper = new TestHelper($this->View, [
  82. 'key3' => 'val3',
  83. 'key2' => ['key2.2' => 'newval'],
  84. ]);
  85. $expected = [
  86. 'key1' => 'val1',
  87. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'newval'],
  88. 'key3' => 'val3',
  89. ];
  90. $this->assertEquals($expected, $Helper->getConfig());
  91. }
  92. /**
  93. * test lazy loading helpers is seamless
  94. *
  95. * @return void
  96. */
  97. public function testLazyLoadingHelpers()
  98. {
  99. $this->loadPlugins(['TestPlugin']);
  100. $Helper = new TestHelper($this->View);
  101. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $Helper->OtherHelper);
  102. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $Helper->Html);
  103. }
  104. /**
  105. * test that a helpers Helper is not 'attached' to the collection
  106. *
  107. * @return void
  108. */
  109. public function testThatHelperHelpersAreNotAttached()
  110. {
  111. $events = $this->getMockBuilder('\Cake\Event\EventManager')->getMock();
  112. $this->View->setEventManager($events);
  113. $events->expects($this->never())
  114. ->method('attach');
  115. $Helper = new TestHelper($this->View);
  116. $Helper->OtherHelper;
  117. }
  118. /**
  119. * test that the lazy loader doesn't duplicate objects on each access.
  120. *
  121. * @return void
  122. */
  123. public function testLazyLoadingUsesReferences()
  124. {
  125. $Helper = new TestHelper($this->View);
  126. $resultA = $Helper->Html;
  127. $resultB = $Helper->Html;
  128. $resultA->testprop = 1;
  129. $this->assertEquals($resultA->testprop, $resultB->testprop);
  130. }
  131. /**
  132. * test getting view instance
  133. *
  134. * @return void
  135. */
  136. public function testGetView()
  137. {
  138. $Helper = new TestHelper($this->View);
  139. $this->assertSame($this->View, $Helper->getView());
  140. }
  141. /**
  142. * Tests __debugInfo
  143. *
  144. * @return void
  145. */
  146. public function testDebugInfo()
  147. {
  148. $Helper = new TestHelper($this->View);
  149. $expected = [
  150. 'helpers' => [
  151. 'Html',
  152. 'TestPlugin.OtherHelper',
  153. ],
  154. 'implementedEvents' => [
  155. ],
  156. '_config' => [
  157. 'key1' => 'val1',
  158. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'val2.2'],
  159. ],
  160. ];
  161. $result = $Helper->__debugInfo();
  162. $this->assertEquals($expected, $result);
  163. }
  164. /**
  165. * Test addClass() with 'class' => array
  166. *
  167. * @return void
  168. */
  169. public function testAddClassArray()
  170. {
  171. $helper = new TestHelper($this->View);
  172. $input = ['class' => ['element1', 'element2']];
  173. $expected = ['class' => [
  174. 'element1',
  175. 'element2',
  176. 'element3',
  177. ]];
  178. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  179. }
  180. /**
  181. * Test addClass() with 'class' => string
  182. *
  183. * @return void
  184. */
  185. public function testAddClassString()
  186. {
  187. $helper = new TestHelper($this->View);
  188. $input = ['class' => 'element1 element2'];
  189. $expected = ['class' => 'element1 element2 element3'];
  190. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  191. }
  192. /**
  193. * Test addClass() with no class element
  194. *
  195. * @return void
  196. */
  197. public function testAddClassEmpty()
  198. {
  199. $helper = new TestHelper($this->View);
  200. $input = [];
  201. $expected = ['class' => 'element3'];
  202. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  203. }
  204. /**
  205. * Test addClass() with adding null class
  206. */
  207. public function testAddClassNull()
  208. {
  209. $helper = new TestHelper($this->View);
  210. $input = [];
  211. $expected = ['class' => ''];
  212. $this->assertEquals($expected, $helper->addClass($input, null));
  213. }
  214. }