HelperTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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\Routing\Router;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\View\View;
  23. use TestApp\View\Helper\TestHelper;
  24. /**
  25. * HelperTest class
  26. */
  27. class HelperTest extends TestCase
  28. {
  29. /**
  30. * @var \Cake\View\View
  31. */
  32. public $View;
  33. /**
  34. * setUp method
  35. *
  36. * @return void
  37. */
  38. public function setUp()
  39. {
  40. parent::setUp();
  41. Router::reload();
  42. $this->View = new View();
  43. }
  44. /**
  45. * tearDown method
  46. *
  47. * @return void
  48. */
  49. public function tearDown()
  50. {
  51. parent::tearDown();
  52. Configure::delete('Asset');
  53. $this->clearPlugins();
  54. unset($this->View);
  55. }
  56. /**
  57. * Test settings merging
  58. *
  59. * @return void
  60. */
  61. public function testSettingsMerging()
  62. {
  63. $Helper = new TestHelper($this->View, [
  64. 'key3' => 'val3',
  65. 'key2' => ['key2.2' => 'newval'],
  66. ]);
  67. $expected = [
  68. 'key1' => 'val1',
  69. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'newval'],
  70. 'key3' => 'val3',
  71. ];
  72. $this->assertEquals($expected, $Helper->getConfig());
  73. }
  74. /**
  75. * test lazy loading helpers is seamless
  76. *
  77. * @return void
  78. */
  79. public function testLazyLoadingHelpers()
  80. {
  81. $this->loadPlugins(['TestPlugin']);
  82. $Helper = new TestHelper($this->View);
  83. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $Helper->OtherHelper);
  84. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $Helper->Html);
  85. }
  86. /**
  87. * test that a helpers Helper is not 'attached' to the collection
  88. *
  89. * @return void
  90. */
  91. public function testThatHelperHelpersAreNotAttached()
  92. {
  93. $events = $this->getMockBuilder('Cake\Event\EventManager')->getMock();
  94. $this->View->setEventManager($events);
  95. $events->expects($this->never())
  96. ->method('on');
  97. $Helper = new TestHelper($this->View);
  98. $Helper->OtherHelper;
  99. }
  100. /**
  101. * test that the lazy loader doesn't duplicate objects on each access.
  102. *
  103. * @return void
  104. */
  105. public function testLazyLoadingUsesReferences()
  106. {
  107. $Helper = new TestHelper($this->View);
  108. $resultA = $Helper->Html;
  109. $resultB = $Helper->Html;
  110. $resultA->testprop = 1;
  111. $this->assertEquals($resultA->testprop, $resultB->testprop);
  112. }
  113. /**
  114. * test getting view instance
  115. *
  116. * @return void
  117. */
  118. public function testGetView()
  119. {
  120. $Helper = new TestHelper($this->View);
  121. $this->assertSame($this->View, $Helper->getView());
  122. }
  123. /**
  124. * Tests __debugInfo
  125. *
  126. * @return void
  127. */
  128. public function testDebugInfo()
  129. {
  130. $Helper = new TestHelper($this->View);
  131. $expected = [
  132. 'helpers' => [
  133. 'Html',
  134. 'TestPlugin.OtherHelper',
  135. ],
  136. 'implementedEvents' => [
  137. ],
  138. '_config' => [
  139. 'key1' => 'val1',
  140. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'val2.2'],
  141. ],
  142. ];
  143. $result = $Helper->__debugInfo();
  144. $this->assertEquals($expected, $result);
  145. }
  146. /**
  147. * Test addClass() with 'class' => array
  148. *
  149. * @return void
  150. */
  151. public function testAddClassArray()
  152. {
  153. $helper = new TestHelper($this->View);
  154. $input = ['class' => ['element1', 'element2']];
  155. $expected = ['class' => [
  156. 'element1',
  157. 'element2',
  158. 'element3',
  159. ]];
  160. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  161. }
  162. /**
  163. * Test addClass() with 'class' => string
  164. *
  165. * @return void
  166. */
  167. public function testAddClassString()
  168. {
  169. $helper = new TestHelper($this->View);
  170. $input = ['class' => 'element1 element2'];
  171. $expected = ['class' => 'element1 element2 element3'];
  172. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  173. }
  174. /**
  175. * Test addClass() with no class element
  176. *
  177. * @return void
  178. */
  179. public function testAddClassEmpty()
  180. {
  181. $helper = new TestHelper($this->View);
  182. $input = [];
  183. $expected = ['class' => 'element3'];
  184. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  185. }
  186. /**
  187. * Test addClass() with adding null class
  188. */
  189. public function testAddClassNull()
  190. {
  191. $helper = new TestHelper($this->View);
  192. $input = [];
  193. $expected = ['class' => ''];
  194. $this->assertEquals($expected, $helper->addClass($input, null));
  195. }
  196. }