HelperCollectionTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * HelperCollectionTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, 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-2011, 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. $this->View = $this->getMock('View', array(), array(null));
  35. $this->Helpers = new HelperCollection($this->View);
  36. }
  37. /**
  38. * teardown
  39. *
  40. * @return void
  41. */
  42. public function teardown() {
  43. CakePlugin::unload();
  44. unset($this->Helpers, $this->View);
  45. }
  46. /**
  47. * test triggering callbacks on loaded helpers
  48. *
  49. * @return void
  50. */
  51. public function testLoad() {
  52. $result = $this->Helpers->load('Html');
  53. $this->assertInstanceOf('HtmlHelper', $result);
  54. $this->assertInstanceOf('HtmlHelper', $this->Helpers->Html);
  55. $result = $this->Helpers->attached();
  56. $this->assertEquals(array('Html'), $result, 'attached() results are wrong.');
  57. $this->assertTrue($this->Helpers->enabled('Html'));
  58. }
  59. /**
  60. * Tests loading as an alias
  61. *
  62. * @return void
  63. */
  64. public function testLoadWithAlias() {
  65. $result = $this->Helpers->load('Html', array('className' => 'HtmlAlias'));
  66. $this->assertInstanceOf('HtmlAliasHelper', $result);
  67. $this->assertInstanceOf('HtmlAliasHelper', $this->Helpers->Html);
  68. $result = $this->Helpers->attached();
  69. $this->assertEquals(array('Html'), $result, 'attached() results are wrong.');
  70. $this->assertTrue($this->Helpers->enabled('Html'));
  71. $result = $this->Helpers->load('Html');
  72. $this->assertInstanceOf('HtmlAliasHelper', $result);
  73. App::build(array('plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)));
  74. CakePlugin::loadAll();
  75. $result = $this->Helpers->load('SomeOther', array('className' => 'TestPlugin.OtherHelper'));
  76. $this->assertInstanceOf('OtherHelperHelper', $result);
  77. $this->assertInstanceOf('OtherHelperHelper', $this->Helpers->SomeOther);
  78. $result = $this->Helpers->attached();
  79. $this->assertEquals(array('Html', 'SomeOther'), $result, 'attached() results are wrong.');
  80. App::build();
  81. }
  82. /**
  83. * test that the enabled setting disables the helper.
  84. *
  85. * @return void
  86. */
  87. public function testLoadWithEnabledFalse() {
  88. $result = $this->Helpers->load('Html', array('enabled' => false));
  89. $this->assertInstanceOf('HtmlHelper', $result);
  90. $this->assertInstanceOf('HtmlHelper', $this->Helpers->Html);
  91. $this->assertFalse($this->Helpers->enabled('Html'), 'Html should be disabled');
  92. }
  93. /**
  94. * test missinghelper exception
  95. *
  96. * @expectedException MissingHelperClassException
  97. * @return void
  98. */
  99. public function testLoadMissingHelperFile() {
  100. $result = $this->Helpers->load('ThisHelperShouldAlwaysBeMissing');
  101. }
  102. /**
  103. * test loading a plugin helper.
  104. *
  105. * @return void
  106. */
  107. public function testLoadPluginHelper() {
  108. App::build(array(
  109. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  110. ));
  111. CakePlugin::loadAll();
  112. $result = $this->Helpers->load('TestPlugin.OtherHelper');
  113. $this->assertInstanceOf('OtherHelperHelper', $result, 'Helper class is wrong.');
  114. $this->assertInstanceOf('OtherHelperHelper', $this->Helpers->OtherHelper, 'Class is wrong');
  115. App::build();
  116. }
  117. /**
  118. * test unload()
  119. *
  120. * @return void
  121. */
  122. public function testUnload() {
  123. $this->Helpers->load('Form');
  124. $this->Helpers->load('Html');
  125. $result = $this->Helpers->attached();
  126. $this->assertEquals(array('Form', 'Html'), $result, 'loaded helpers is wrong');
  127. $this->Helpers->unload('Html');
  128. $this->assertFalse(isset($this->Helpers->Html));
  129. $this->assertTrue(isset($this->Helpers->Form));
  130. $result = $this->Helpers->attached();
  131. $this->assertEquals(array('Form'), $result, 'loaded helpers is wrong');
  132. }
  133. }