HelperTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * HelperTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since 1.2.0
  15. * @license http://www.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\Network\Request;
  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. * expose a method as public
  44. *
  45. * @param string $options
  46. * @param string $exclude
  47. * @param string $insertBefore
  48. * @param string $insertAfter
  49. * @return void
  50. */
  51. public function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null)
  52. {
  53. return $this->_parseAttributes($options, $exclude, $insertBefore, $insertAfter);
  54. }
  55. }
  56. /**
  57. * HelperTest class
  58. */
  59. class HelperTest extends TestCase
  60. {
  61. /**
  62. * setUp method
  63. *
  64. * @return void
  65. */
  66. public function setUp()
  67. {
  68. parent::setUp();
  69. Router::reload();
  70. $this->View = new View();
  71. $this->Helper = new Helper($this->View);
  72. $this->Helper->request = new Request();
  73. }
  74. /**
  75. * tearDown method
  76. *
  77. * @return void
  78. */
  79. public function tearDown()
  80. {
  81. parent::tearDown();
  82. Configure::delete('Asset');
  83. Plugin::unload();
  84. unset($this->Helper, $this->View);
  85. }
  86. /**
  87. * Test settings merging
  88. *
  89. * @return void
  90. */
  91. public function testSettingsMerging()
  92. {
  93. $Helper = new TestHelper($this->View, [
  94. 'key3' => 'val3',
  95. 'key2' => ['key2.2' => 'newval']
  96. ]);
  97. $expected = [
  98. 'key1' => 'val1',
  99. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'newval'],
  100. 'key3' => 'val3'
  101. ];
  102. $this->assertEquals($expected, $Helper->config());
  103. }
  104. /**
  105. * test lazy loading helpers is seamless
  106. *
  107. * @return void
  108. */
  109. public function testLazyLoadingHelpers()
  110. {
  111. Plugin::load(['TestPlugin']);
  112. $Helper = new TestHelper($this->View);
  113. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $Helper->OtherHelper);
  114. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $Helper->Html);
  115. }
  116. /**
  117. * test that a helpers Helper is not 'attached' to the collection
  118. *
  119. * @return void
  120. */
  121. public function testThatHelperHelpersAreNotAttached()
  122. {
  123. Plugin::loadAll();
  124. $events = $this->getMockBuilder('\Cake\Event\EventManager')->getMock();
  125. $this->View->eventManager($events);
  126. $events->expects($this->never())
  127. ->method('attach');
  128. $Helper = new TestHelper($this->View);
  129. $Helper->OtherHelper;
  130. }
  131. /**
  132. * test that the lazy loader doesn't duplicate objects on each access.
  133. *
  134. * @return void
  135. */
  136. public function testLazyLoadingUsesReferences()
  137. {
  138. $Helper = new TestHelper($this->View);
  139. $resultA = $Helper->Html;
  140. $resultB = $Helper->Html;
  141. $resultA->testprop = 1;
  142. $this->assertEquals($resultA->testprop, $resultB->testprop);
  143. }
  144. /**
  145. * Tests __debugInfo
  146. *
  147. * @return void
  148. */
  149. public function testDebugInfo()
  150. {
  151. $Helper = new TestHelper($this->View);
  152. $expected = [
  153. 'helpers' => [
  154. 'Html',
  155. 'TestPlugin.OtherHelper'
  156. ],
  157. 'theme' => null,
  158. 'plugin' => null,
  159. 'fieldset' => [],
  160. 'tags' => [],
  161. 'implementedEvents' => [
  162. ],
  163. '_config' => [
  164. 'key1' => 'val1',
  165. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'val2.2']
  166. ]
  167. ];
  168. $result = $Helper->__debugInfo();
  169. $this->assertEquals($expected, $result);
  170. }
  171. }