HelperTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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\Event\EventListenerInterface;
  21. use Cake\Event\EventManager;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\View\Helper;
  25. use Cake\View\Helper\HtmlHelper;
  26. use Cake\View\View;
  27. use Exception;
  28. use TestApp\View\Helper\TestHelper;
  29. use TestPlugin\View\Helper\OtherHelperHelper;
  30. /**
  31. * HelperTest class
  32. */
  33. class HelperTest extends TestCase
  34. {
  35. /**
  36. * @var \Cake\View\View
  37. */
  38. protected $View;
  39. /**
  40. * setUp method
  41. */
  42. public function setUp(): void
  43. {
  44. parent::setUp();
  45. Router::reload();
  46. $this->View = new View();
  47. }
  48. /**
  49. * tearDown method
  50. */
  51. public function tearDown(): void
  52. {
  53. parent::tearDown();
  54. Configure::delete('Asset');
  55. $this->clearPlugins();
  56. unset($this->View);
  57. }
  58. /**
  59. * Test settings merging
  60. */
  61. public function testSettingsMerging(): void
  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. public function testLazyLoadingHelpers(): void
  78. {
  79. $this->loadPlugins(['TestPlugin']);
  80. $Helper = new TestHelper($this->View);
  81. $this->assertInstanceOf(OtherHelperHelper::class, $Helper->OtherHelper);
  82. $this->assertInstanceOf(HtmlHelper::class, $Helper->Html);
  83. }
  84. /**
  85. * test that a helpers Helper is not 'attached' to the collection
  86. */
  87. public function testThatHelperHelpersAreNotAttached(): void
  88. {
  89. $eventsManager = new class extends EventManager
  90. {
  91. public function on(string|EventListenerInterface $eventKey, callable|array $options = [], ?callable $callable = null)
  92. {
  93. throw new Exception('Should not be called');
  94. }
  95. };
  96. $this->View->setEventManager($eventsManager);
  97. $helper = new TestHelper($this->View);
  98. $this->assertInstanceOf(Helper::class, $helper->OtherHelper);
  99. }
  100. /**
  101. * test that the lazy loader doesn't duplicate objects on each access.
  102. */
  103. public function testLazyLoadingUsesReferences(): void
  104. {
  105. $Helper = new TestHelper($this->View);
  106. $resultA = $Helper->Html;
  107. $resultB = $Helper->Html;
  108. $this->assertSame($resultA, $resultB);
  109. }
  110. /**
  111. * test getting view instance
  112. */
  113. public function testGetView(): void
  114. {
  115. $Helper = new TestHelper($this->View);
  116. $this->assertSame($this->View, $Helper->getView());
  117. }
  118. /**
  119. * Tests __debugInfo
  120. */
  121. public function testDebugInfo(): void
  122. {
  123. $Helper = new TestHelper($this->View);
  124. $expected = [
  125. 'helpers' => [
  126. 'Html' => [],
  127. 'OtherHelper' => ['className' => 'TestPlugin.OtherHelper'],
  128. ],
  129. 'implementedEvents' => [
  130. ],
  131. '_config' => [
  132. 'key1' => 'val1',
  133. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'val2.2'],
  134. ],
  135. ];
  136. $result = $Helper->__debugInfo();
  137. $this->assertEquals($expected, $result);
  138. }
  139. /**
  140. * Test addClass() with 'class' => array
  141. */
  142. public function testAddClassArray(): void
  143. {
  144. $helper = new TestHelper($this->View);
  145. $input = ['class' => ['element1', 'element2']];
  146. $expected = ['class' => [
  147. 'element1',
  148. 'element2',
  149. 'element3',
  150. ]];
  151. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  152. }
  153. /**
  154. * Test addClass() with 'class' => string
  155. */
  156. public function testAddClassString(): void
  157. {
  158. $helper = new TestHelper($this->View);
  159. $input = ['class' => 'element1 element2'];
  160. $expected = ['class' => 'element1 element2 element3'];
  161. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  162. }
  163. /**
  164. * Test addClass() with no class element
  165. */
  166. public function testAddClassEmpty(): void
  167. {
  168. $helper = new TestHelper($this->View);
  169. $input = [];
  170. $expected = ['class' => 'element3'];
  171. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  172. }
  173. }