CheckBoxTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Dojo
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Dojo_Form_Element_CheckBoxTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dojo_Form_Element_CheckBoxTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  27. /** Zend_Dojo_Form_Element_CheckBox */
  28. require_once 'Zend/Dojo/Form/Element/CheckBox.php';
  29. /** Zend_View */
  30. require_once 'Zend/View.php';
  31. /** Zend_Registry */
  32. require_once 'Zend/Registry.php';
  33. /** Zend_Dojo_View_Helper_Dojo */
  34. require_once 'Zend/Dojo/View/Helper/Dojo.php';
  35. /**
  36. * Test class for Zend_Dojo_Form_Element_Dijit.
  37. *
  38. * @package Zend_Dojo
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Dojo_Form_Element_CheckBoxTest extends PHPUnit_Framework_TestCase
  44. {
  45. /**
  46. * Runs the test methods of this class.
  47. *
  48. * @return void
  49. */
  50. public static function main()
  51. {
  52. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_Form_Element_CheckBoxTest");
  53. $result = PHPUnit_TextUI_TestRunner::run($suite);
  54. }
  55. /**
  56. * Sets up the fixture, for example, open a network connection.
  57. * This method is called before a test is executed.
  58. *
  59. * @return void
  60. */
  61. public function setUp()
  62. {
  63. Zend_Registry::_unsetInstance();
  64. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  65. $this->view = $this->getView();
  66. $this->element = $this->getElement();
  67. $this->element->setView($this->view);
  68. }
  69. /**
  70. * Tears down the fixture, for example, close a network connection.
  71. * This method is called after a test is executed.
  72. *
  73. * @return void
  74. */
  75. public function tearDown()
  76. {
  77. }
  78. public function getView()
  79. {
  80. require_once 'Zend/View.php';
  81. $view = new Zend_View();
  82. $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
  83. return $view;
  84. }
  85. public function getElement()
  86. {
  87. $element = new Zend_Dojo_Form_Element_CheckBox(
  88. 'foo',
  89. array(
  90. 'label' => 'CheckBox',
  91. 'class' => 'someclass',
  92. 'style' => 'width: 100px;',
  93. )
  94. );
  95. return $element;
  96. }
  97. public function testCheckedFlagIsFalseByDefault()
  98. {
  99. $this->assertFalse($this->element->checked);
  100. }
  101. public function testCheckedAttributeNotRenderedByDefault()
  102. {
  103. $html = $this->element->render();
  104. $this->assertNotContains('checked="checked"', $html);
  105. }
  106. public function testCheckedAttributeRenderedWhenCheckedFlagTrue()
  107. {
  108. $this->element->checked = true;
  109. $html = $this->element->render();
  110. $this->assertContains('checked="checked"', $html);
  111. }
  112. public function testCheckedValueDefaultsToOne()
  113. {
  114. $this->assertEquals(1, $this->element->getCheckedValue());
  115. }
  116. public function testUncheckedValueDefaultsToZero()
  117. {
  118. $this->assertEquals(0, $this->element->getUncheckedValue());
  119. }
  120. public function testCanSetCheckedValue()
  121. {
  122. $this->testCheckedValueDefaultsToOne();
  123. $this->element->setCheckedValue('foo');
  124. $this->assertEquals('foo', $this->element->getCheckedValue());
  125. }
  126. public function testCanSetUncheckedValue()
  127. {
  128. $this->testUncheckedValueDefaultsToZero();
  129. $this->element->setUncheckedValue('foo');
  130. $this->assertEquals('foo', $this->element->getUncheckedValue());
  131. }
  132. public function testValueInitiallyUncheckedValue()
  133. {
  134. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  135. }
  136. public function testSettingValueToCheckedValueSetsWithEquivalentValue()
  137. {
  138. $this->testValueInitiallyUncheckedValue();
  139. $this->element->setValue($this->element->getCheckedValue());
  140. $this->assertEquals($this->element->getCheckedValue(), $this->element->getValue());
  141. }
  142. public function testSettingValueToAnythingOtherThanCheckedValueSetsAsUncheckedValue()
  143. {
  144. $this->testSettingValueToCheckedValueSetsWithEquivalentValue();
  145. $this->element->setValue('bogus');
  146. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  147. }
  148. public function testSettingCheckedFlagToTrueSetsValueToCheckedValue()
  149. {
  150. $this->testValueInitiallyUncheckedValue();
  151. $this->element->setChecked(true);
  152. $this->assertEquals($this->element->getCheckedValue(), $this->element->getValue());
  153. }
  154. public function testSettingCheckedFlagToFalseSetsValueToUncheckedValue()
  155. {
  156. $this->testSettingCheckedFlagToTrueSetsValueToCheckedValue();
  157. $this->element->setChecked(false);
  158. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  159. }
  160. public function testSettingValueToCheckedValueMarksElementAsChecked()
  161. {
  162. $this->testValueInitiallyUncheckedValue();
  163. $this->element->setValue($this->element->getCheckedValue());
  164. $this->assertTrue($this->element->checked);
  165. }
  166. public function testSettingValueToUncheckedValueMarksElementAsNotChecked()
  167. {
  168. $this->testSettingValueToCheckedValueMarksElementAsChecked();
  169. $this->element->setValue($this->element->getUncheckedValue());
  170. $this->assertFalse($this->element->checked);
  171. }
  172. public function testIsCheckedShouldReflectCurrentCheckedStatus()
  173. {
  174. $this->element->setChecked(true);
  175. $this->assertTrue($this->element->isChecked());
  176. $this->element->setChecked(false);
  177. $this->assertFalse($this->element->isChecked());
  178. }
  179. public function testSetOptionsSetsInitialValueAccordingToCheckedAndUncheckedValues()
  180. {
  181. $options = array(
  182. 'checkedValue' => 'foo',
  183. 'uncheckedValue' => 'bar',
  184. );
  185. $element = new Zend_Dojo_Form_Element_CheckBox('test', $options);
  186. $this->assertEquals($options['uncheckedValue'], $element->getValue());
  187. }
  188. public function testSetOptionsSetsInitialValueAccordingToSubmittedValues()
  189. {
  190. $options = array(
  191. 'test1' => array(
  192. 'value' => 'foo',
  193. 'checkedValue' => 'foo',
  194. 'uncheckedValue' => 'bar',
  195. ),
  196. 'test2' => array(
  197. 'value' => 'bar',
  198. 'checkedValue' => 'foo',
  199. 'uncheckedValue' => 'bar',
  200. ),
  201. );
  202. foreach ($options as $current) {
  203. $element = new Zend_Dojo_Form_Element_CheckBox('test', $current);
  204. $this->assertEquals($current['value'], $element->getValue());
  205. $this->assertEquals($current['checkedValue'], $element->getCheckedValue());
  206. $this->assertEquals($current['uncheckedValue'], $element->getUncheckedValue());
  207. }
  208. }
  209. public function testShouldRenderCheckBoxDijit()
  210. {
  211. $html = $this->element->render();
  212. $this->assertContains('dojoType="dijit.form.CheckBox"', $html);
  213. }
  214. /**
  215. * @group ZF-3879
  216. */
  217. public function testOptionsShouldNotBeRenderedAsElementAttribute()
  218. {
  219. $html = $this->element->render();
  220. $this->assertNotContains('options="', $html, $html);
  221. }
  222. /**
  223. * @group ZF-4274
  224. */
  225. public function testCheckedValuesCanBePassedInConstructor()
  226. {
  227. $element = new Zend_Dojo_Form_Element_CheckBox('myCheckbox', array(
  228. 'checkedValue' => 'checkedVal',
  229. 'unCheckedValue' => 'UNCHECKED',
  230. ));
  231. $element->setView(new Zend_View());
  232. $html = $element->render();
  233. $this->assertContains('value="checkedVal"', $html, $html);
  234. }
  235. }
  236. // Call Zend_Dojo_Form_Element_CheckBoxTest::main() if this source file is executed directly.
  237. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_Form_Element_CheckBoxTest::main") {
  238. Zend_Dojo_Form_Element_CheckBoxTest::main();
  239. }