HelperTest.php 4.1 KB

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