HelperTest.php 5.6 KB

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