HelperTest.php 4.8 KB

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