HelperTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * HelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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. * Settings for this helper.
  28. *
  29. * @var array
  30. */
  31. protected $_defaultConfig = array(
  32. 'key1' => 'val1',
  33. 'key2' => array('key2.1' => 'val2.1', 'key2.2' => 'val2.2')
  34. );
  35. /**
  36. * Helpers for this helper.
  37. *
  38. * @var array
  39. */
  40. public $helpers = array('Html', 'TestPlugin.OtherHelper');
  41. /**
  42. * expose a method as public
  43. *
  44. * @param string $options
  45. * @param string $exclude
  46. * @param string $insertBefore
  47. * @param string $insertAfter
  48. * @return void
  49. */
  50. public function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
  51. return $this->_parseAttributes($options, $exclude, $insertBefore, $insertAfter);
  52. }
  53. }
  54. /**
  55. * HelperTest class
  56. *
  57. */
  58. class HelperTest extends TestCase {
  59. /**
  60. * setUp method
  61. *
  62. * @return void
  63. */
  64. public function setUp() {
  65. parent::setUp();
  66. Router::reload();
  67. $this->View = new View();
  68. $this->Helper = new Helper($this->View);
  69. $this->Helper->request = new Request();
  70. }
  71. /**
  72. * tearDown method
  73. *
  74. * @return void
  75. */
  76. public function tearDown() {
  77. parent::tearDown();
  78. Configure::delete('Asset');
  79. Plugin::unload();
  80. unset($this->Helper, $this->View);
  81. }
  82. /**
  83. * Test settings merging
  84. *
  85. * @return void
  86. */
  87. public function testSettingsMerging() {
  88. $Helper = new TestHelper($this->View, array(
  89. 'key3' => 'val3',
  90. 'key2' => array('key2.2' => 'newval')
  91. ));
  92. $expected = array(
  93. 'key1' => 'val1',
  94. 'key2' => array('key2.1' => 'val2.1', 'key2.2' => 'newval'),
  95. 'key3' => 'val3'
  96. );
  97. $this->assertEquals($expected, $Helper->config());
  98. }
  99. /**
  100. * test lazy loading helpers is seamless
  101. *
  102. * @return void
  103. */
  104. public function testLazyLoadingHelpers() {
  105. Plugin::load(array('TestPlugin'));
  106. $Helper = new TestHelper($this->View);
  107. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $Helper->OtherHelper);
  108. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $Helper->Html);
  109. }
  110. /**
  111. * test that a helpers Helper is not 'attached' to the collection
  112. *
  113. * @return void
  114. */
  115. public function testThatHelperHelpersAreNotAttached() {
  116. Plugin::loadAll();
  117. $events = $this->getMock('\Cake\Event\EventManager');
  118. $this->View->eventManager($events);
  119. $events->expects($this->never())
  120. ->method('attach');
  121. $Helper = new TestHelper($this->View);
  122. $Helper->OtherHelper;
  123. }
  124. /**
  125. * test that the lazy loader doesn't duplicate objects on each access.
  126. *
  127. * @return void
  128. */
  129. public function testLazyLoadingUsesReferences() {
  130. $Helper = new TestHelper($this->View);
  131. $resultA = $Helper->Html;
  132. $resultB = $Helper->Html;
  133. $resultA->testprop = 1;
  134. $this->assertEquals($resultA->testprop, $resultB->testprop);
  135. }
  136. }