HelperCollectionTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/view/1196/Testing 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->attached();
  58. $this->assertEquals(array('Html'), $result, 'attached() 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. * Tests loading as an alias
  79. *
  80. * @return void
  81. */
  82. public function testLoadWithAlias() {
  83. $result = $this->Helpers->load('Html', array('className' => 'HtmlAlias'));
  84. $this->assertInstanceOf('HtmlAliasHelper', $result);
  85. $this->assertInstanceOf('HtmlAliasHelper', $this->Helpers->Html);
  86. $result = $this->Helpers->attached();
  87. $this->assertEquals(array('Html'), $result, 'attached() results are wrong.');
  88. $this->assertTrue($this->Helpers->enabled('Html'));
  89. $result = $this->Helpers->load('Html');
  90. $this->assertInstanceOf('HtmlAliasHelper', $result);
  91. App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)));
  92. CakePlugin::load(array('TestPlugin'));
  93. $result = $this->Helpers->load('SomeOther', array('className' => 'TestPlugin.OtherHelper'));
  94. $this->assertInstanceOf('OtherHelperHelper', $result);
  95. $this->assertInstanceOf('OtherHelperHelper', $this->Helpers->SomeOther);
  96. $result = $this->Helpers->attached();
  97. $this->assertEquals(array('Html', 'SomeOther'), $result, 'attached() results are wrong.');
  98. App::build();
  99. }
  100. /**
  101. * test that the enabled setting disables the helper.
  102. *
  103. * @return void
  104. */
  105. public function testLoadWithEnabledFalse() {
  106. $result = $this->Helpers->load('Html', array('enabled' => false));
  107. $this->assertInstanceOf('HtmlHelper', $result);
  108. $this->assertInstanceOf('HtmlHelper', $this->Helpers->Html);
  109. $this->assertFalse($this->Helpers->enabled('Html'), 'Html should be disabled');
  110. }
  111. /**
  112. * test missinghelper exception
  113. *
  114. * @expectedException MissingHelperException
  115. * @return void
  116. */
  117. public function testLoadMissingHelper() {
  118. $result = $this->Helpers->load('ThisHelperShouldAlwaysBeMissing');
  119. }
  120. /**
  121. * test loading a plugin helper.
  122. *
  123. * @return void
  124. */
  125. public function testLoadPluginHelper() {
  126. App::build(array(
  127. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  128. ));
  129. CakePlugin::load(array('TestPlugin'));
  130. $result = $this->Helpers->load('TestPlugin.OtherHelper');
  131. $this->assertInstanceOf('OtherHelperHelper', $result, 'Helper class is wrong.');
  132. $this->assertInstanceOf('OtherHelperHelper', $this->Helpers->OtherHelper, 'Class is wrong');
  133. App::build();
  134. }
  135. /**
  136. * test unload()
  137. *
  138. * @return void
  139. */
  140. public function testUnload() {
  141. $this->Helpers->load('Form');
  142. $this->Helpers->load('Html');
  143. $result = $this->Helpers->attached();
  144. $this->assertEquals(array('Form', 'Html'), $result, 'loaded helpers is wrong');
  145. $this->Helpers->unload('Html');
  146. $this->assertNotContains('Html', $this->Helpers->attached());
  147. $this->assertContains('Form', $this->Helpers->attached());
  148. $result = $this->Helpers->attached();
  149. $this->assertEquals(array('Form'), $result, 'loaded helpers is wrong');
  150. }
  151. }