HelperCollectionTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * HelperCollectionTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('HelperCollection', 'View');
  20. App::uses('HtmlHelper', 'View/Helper');
  21. App::uses('View', 'View');
  22. /**
  23. * Extended HtmlHelper
  24. */
  25. class HtmlAliasHelper extends HtmlHelper {
  26. }
  27. class HelperCollectionTest extends CakeTestCase {
  28. /**
  29. * setUp
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. $this->View = $this->getMock('View', array(), array(null));
  36. $this->Helpers = new HelperCollection($this->View);
  37. }
  38. /**
  39. * tearDown
  40. *
  41. * @return void
  42. */
  43. public function tearDown() {
  44. CakePlugin::unload();
  45. unset($this->Helpers, $this->View);
  46. parent::tearDown();
  47. }
  48. /**
  49. * test triggering callbacks on loaded helpers
  50. *
  51. * @return void
  52. */
  53. public function testLoad() {
  54. $result = $this->Helpers->load('Html');
  55. $this->assertInstanceOf('HtmlHelper', $result);
  56. $this->assertInstanceOf('HtmlHelper', $this->Helpers->Html);
  57. $result = $this->Helpers->loaded();
  58. $this->assertEquals(array('Html'), $result, 'loaded() results are wrong.');
  59. $this->assertTrue($this->Helpers->enabled('Html'));
  60. }
  61. /**
  62. * test lazy loading of helpers
  63. *
  64. * @return void
  65. */
  66. public function testLazyLoad() {
  67. $result = $this->Helpers->Html;
  68. $this->assertInstanceOf('HtmlHelper', $result);
  69. $result = $this->Helpers->Form;
  70. $this->assertInstanceOf('FormHelper', $result);
  71. App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)));
  72. $this->View->plugin = 'TestPlugin';
  73. CakePlugin::load(array('TestPlugin'));
  74. $result = $this->Helpers->OtherHelper;
  75. $this->assertInstanceOf('OtherHelperHelper', $result);
  76. }
  77. /**
  78. * test lazy loading of helpers
  79. *
  80. * @expectedException MissingHelperException
  81. * @return void
  82. */
  83. public function testLazyLoadException() {
  84. $this->Helpers->NotAHelper;
  85. }
  86. /**
  87. * Tests loading as an alias
  88. *
  89. * @return void
  90. */
  91. public function testLoadWithAlias() {
  92. $result = $this->Helpers->load('Html', array('className' => 'HtmlAlias'));
  93. $this->assertInstanceOf('HtmlAliasHelper', $result);
  94. $this->assertInstanceOf('HtmlAliasHelper', $this->Helpers->Html);
  95. $result = $this->Helpers->loaded();
  96. $this->assertEquals(array('Html'), $result, 'loaded() results are wrong.');
  97. $this->assertTrue($this->Helpers->enabled('Html'));
  98. $result = $this->Helpers->load('Html');
  99. $this->assertInstanceOf('HtmlAliasHelper', $result);
  100. App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)));
  101. CakePlugin::load(array('TestPlugin'));
  102. $result = $this->Helpers->load('SomeOther', array('className' => 'TestPlugin.OtherHelper'));
  103. $this->assertInstanceOf('OtherHelperHelper', $result);
  104. $this->assertInstanceOf('OtherHelperHelper', $this->Helpers->SomeOther);
  105. $result = $this->Helpers->loaded();
  106. $this->assertEquals(array('Html', 'SomeOther'), $result, 'loaded() results are wrong.');
  107. App::build();
  108. }
  109. /**
  110. * test that the enabled setting disables the helper.
  111. *
  112. * @return void
  113. */
  114. public function testLoadWithEnabledFalse() {
  115. $result = $this->Helpers->load('Html', array('enabled' => false));
  116. $this->assertInstanceOf('HtmlHelper', $result);
  117. $this->assertInstanceOf('HtmlHelper', $this->Helpers->Html);
  118. $this->assertFalse($this->Helpers->enabled('Html'), 'Html should be disabled');
  119. }
  120. /**
  121. * test missinghelper exception
  122. *
  123. * @expectedException MissingHelperException
  124. * @return void
  125. */
  126. public function testLoadMissingHelper() {
  127. $this->Helpers->load('ThisHelperShouldAlwaysBeMissing');
  128. }
  129. /**
  130. * test loading a plugin helper.
  131. *
  132. * @return void
  133. */
  134. public function testLoadPluginHelper() {
  135. App::build(array(
  136. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  137. ));
  138. CakePlugin::load(array('TestPlugin'));
  139. $result = $this->Helpers->load('TestPlugin.OtherHelper');
  140. $this->assertInstanceOf('OtherHelperHelper', $result, 'Helper class is wrong.');
  141. $this->assertInstanceOf('OtherHelperHelper', $this->Helpers->OtherHelper, 'Class is wrong');
  142. App::build();
  143. }
  144. /**
  145. * test unload()
  146. *
  147. * @return void
  148. */
  149. public function testUnload() {
  150. $this->Helpers->load('Form');
  151. $this->Helpers->load('Html');
  152. $result = $this->Helpers->loaded();
  153. $this->assertEquals(array('Form', 'Html'), $result, 'loaded helpers is wrong');
  154. $this->Helpers->unload('Html');
  155. $this->assertNotContains('Html', $this->Helpers->loaded());
  156. $this->assertContains('Form', $this->Helpers->loaded());
  157. $result = $this->Helpers->loaded();
  158. $this->assertEquals(array('Form'), $result, 'loaded helpers is wrong');
  159. }
  160. }