HelperTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. */
  60. class HelperTest extends TestCase
  61. {
  62. /**
  63. * setUp method
  64. *
  65. * @return void
  66. */
  67. public function setUp()
  68. {
  69. parent::setUp();
  70. Router::reload();
  71. $this->View = new View();
  72. $this->Helper = new Helper($this->View);
  73. $this->Helper->request = new Request();
  74. }
  75. /**
  76. * tearDown method
  77. *
  78. * @return void
  79. */
  80. public function tearDown()
  81. {
  82. parent::tearDown();
  83. Configure::delete('Asset');
  84. Plugin::unload();
  85. unset($this->Helper, $this->View);
  86. }
  87. /**
  88. * Test settings merging
  89. *
  90. * @return void
  91. */
  92. public function testSettingsMerging()
  93. {
  94. $Helper = new TestHelper($this->View, [
  95. 'key3' => 'val3',
  96. 'key2' => ['key2.2' => 'newval']
  97. ]);
  98. $expected = [
  99. 'key1' => 'val1',
  100. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'newval'],
  101. 'key3' => 'val3'
  102. ];
  103. $this->assertEquals($expected, $Helper->config());
  104. }
  105. /**
  106. * test lazy loading helpers is seamless
  107. *
  108. * @return void
  109. */
  110. public function testLazyLoadingHelpers()
  111. {
  112. Plugin::load(['TestPlugin']);
  113. $Helper = new TestHelper($this->View);
  114. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $Helper->OtherHelper);
  115. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $Helper->Html);
  116. }
  117. /**
  118. * test that a helpers Helper is not 'attached' to the collection
  119. *
  120. * @return void
  121. */
  122. public function testThatHelperHelpersAreNotAttached()
  123. {
  124. Plugin::loadAll();
  125. $events = $this->getMock('\Cake\Event\EventManager');
  126. $this->View->eventManager($events);
  127. $events->expects($this->never())
  128. ->method('attach');
  129. $Helper = new TestHelper($this->View);
  130. $Helper->OtherHelper;
  131. }
  132. /**
  133. * test that the lazy loader doesn't duplicate objects on each access.
  134. *
  135. * @return void
  136. */
  137. public function testLazyLoadingUsesReferences()
  138. {
  139. $Helper = new TestHelper($this->View);
  140. $resultA = $Helper->Html;
  141. $resultB = $Helper->Html;
  142. $resultA->testprop = 1;
  143. $this->assertEquals($resultA->testprop, $resultB->testprop);
  144. }
  145. /**
  146. * Tests __debugInfo
  147. *
  148. * @return void
  149. */
  150. public function testDebugInfo()
  151. {
  152. $Helper = new TestHelper($this->View);
  153. $expected = [
  154. 'helpers' => [
  155. 'Html',
  156. 'TestPlugin.OtherHelper'
  157. ],
  158. 'theme' => null,
  159. 'plugin' => null,
  160. 'fieldset' => [],
  161. 'tags' => [],
  162. 'implementedEvents' => [
  163. ],
  164. '_config' => [
  165. 'key1' => 'val1',
  166. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'val2.2']
  167. ]
  168. ];
  169. $result = $Helper->__debugInfo();
  170. $this->assertEquals($expected, $result);
  171. }
  172. }