2
0

CheckBoxTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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-2010 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_Checkbox.
  37. *
  38. * @category Zend
  39. * @package Zend_Dojo
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @group Zend_Dojo
  44. * @group Zend_Dojo_Form
  45. */
  46. class Zend_Dojo_Form_Element_CheckBoxTest extends PHPUnit_Framework_TestCase
  47. {
  48. /**
  49. * Runs the test methods of this class.
  50. *
  51. * @return void
  52. */
  53. public static function main()
  54. {
  55. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_Form_Element_CheckBoxTest");
  56. $result = PHPUnit_TextUI_TestRunner::run($suite);
  57. }
  58. /**
  59. * Sets up the fixture, for example, open a network connection.
  60. * This method is called before a test is executed.
  61. *
  62. * @return void
  63. */
  64. public function setUp()
  65. {
  66. Zend_Registry::_unsetInstance();
  67. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  68. $this->view = $this->getView();
  69. $this->element = $this->getElement();
  70. $this->element->setView($this->view);
  71. }
  72. /**
  73. * Tears down the fixture, for example, close a network connection.
  74. * This method is called after a test is executed.
  75. *
  76. * @return void
  77. */
  78. public function tearDown()
  79. {
  80. }
  81. public function getView()
  82. {
  83. require_once 'Zend/View.php';
  84. $view = new Zend_View();
  85. $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
  86. return $view;
  87. }
  88. public function getElement()
  89. {
  90. $element = new Zend_Dojo_Form_Element_CheckBox(
  91. 'foo',
  92. array(
  93. 'label' => 'CheckBox',
  94. 'class' => 'someclass',
  95. 'style' => 'width: 100px;',
  96. )
  97. );
  98. return $element;
  99. }
  100. public function testCheckedFlagIsFalseByDefault()
  101. {
  102. $this->assertFalse($this->element->checked);
  103. }
  104. public function testCheckedAttributeNotRenderedByDefault()
  105. {
  106. $html = $this->element->render();
  107. $this->assertNotContains('checked="checked"', $html);
  108. }
  109. public function testCheckedAttributeRenderedWhenCheckedFlagTrue()
  110. {
  111. $this->element->checked = true;
  112. $html = $this->element->render();
  113. $this->assertContains('checked="checked"', $html);
  114. }
  115. public function testCheckedValueDefaultsToOne()
  116. {
  117. $this->assertEquals(1, $this->element->getCheckedValue());
  118. }
  119. public function testUncheckedValueDefaultsToZero()
  120. {
  121. $this->assertEquals(0, $this->element->getUncheckedValue());
  122. }
  123. public function testCanSetCheckedValue()
  124. {
  125. $this->testCheckedValueDefaultsToOne();
  126. $this->element->setCheckedValue('foo');
  127. $this->assertEquals('foo', $this->element->getCheckedValue());
  128. }
  129. public function testCanSetUncheckedValue()
  130. {
  131. $this->testUncheckedValueDefaultsToZero();
  132. $this->element->setUncheckedValue('foo');
  133. $this->assertEquals('foo', $this->element->getUncheckedValue());
  134. }
  135. public function testValueInitiallyUncheckedValue()
  136. {
  137. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  138. }
  139. public function testSettingValueToCheckedValueSetsWithEquivalentValue()
  140. {
  141. $this->testValueInitiallyUncheckedValue();
  142. $this->element->setValue($this->element->getCheckedValue());
  143. $this->assertEquals($this->element->getCheckedValue(), $this->element->getValue());
  144. }
  145. public function testSettingValueToAnythingOtherThanCheckedValueSetsAsUncheckedValue()
  146. {
  147. $this->testSettingValueToCheckedValueSetsWithEquivalentValue();
  148. $this->element->setValue('bogus');
  149. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  150. }
  151. public function testSettingCheckedFlagToTrueSetsValueToCheckedValue()
  152. {
  153. $this->testValueInitiallyUncheckedValue();
  154. $this->element->setChecked(true);
  155. $this->assertEquals($this->element->getCheckedValue(), $this->element->getValue());
  156. }
  157. public function testSettingCheckedFlagToFalseSetsValueToUncheckedValue()
  158. {
  159. $this->testSettingCheckedFlagToTrueSetsValueToCheckedValue();
  160. $this->element->setChecked(false);
  161. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  162. }
  163. public function testSettingValueToCheckedValueMarksElementAsChecked()
  164. {
  165. $this->testValueInitiallyUncheckedValue();
  166. $this->element->setValue($this->element->getCheckedValue());
  167. $this->assertTrue($this->element->checked);
  168. }
  169. public function testSettingValueToUncheckedValueMarksElementAsNotChecked()
  170. {
  171. $this->testSettingValueToCheckedValueMarksElementAsChecked();
  172. $this->element->setValue($this->element->getUncheckedValue());
  173. $this->assertFalse($this->element->checked);
  174. }
  175. public function testIsCheckedShouldReflectCurrentCheckedStatus()
  176. {
  177. $this->element->setChecked(true);
  178. $this->assertTrue($this->element->isChecked());
  179. $this->element->setChecked(false);
  180. $this->assertFalse($this->element->isChecked());
  181. }
  182. public function testSetOptionsSetsInitialValueAccordingToCheckedAndUncheckedValues()
  183. {
  184. $options = array(
  185. 'checkedValue' => 'foo',
  186. 'uncheckedValue' => 'bar',
  187. );
  188. $element = new Zend_Dojo_Form_Element_CheckBox('test', $options);
  189. $this->assertEquals($options['uncheckedValue'], $element->getValue());
  190. }
  191. public function testSetOptionsSetsInitialValueAccordingToSubmittedValues()
  192. {
  193. $options = array(
  194. 'test1' => array(
  195. 'value' => 'foo',
  196. 'checkedValue' => 'foo',
  197. 'uncheckedValue' => 'bar',
  198. ),
  199. 'test2' => array(
  200. 'value' => 'bar',
  201. 'checkedValue' => 'foo',
  202. 'uncheckedValue' => 'bar',
  203. ),
  204. );
  205. foreach ($options as $current) {
  206. $element = new Zend_Dojo_Form_Element_CheckBox('test', $current);
  207. $this->assertEquals($current['value'], $element->getValue());
  208. $this->assertEquals($current['checkedValue'], $element->getCheckedValue());
  209. $this->assertEquals($current['uncheckedValue'], $element->getUncheckedValue());
  210. }
  211. }
  212. public function testShouldRenderCheckBoxDijit()
  213. {
  214. $html = $this->element->render();
  215. $this->assertContains('dojoType="dijit.form.CheckBox"', $html);
  216. }
  217. /**
  218. * @group ZF-3879
  219. */
  220. public function testOptionsShouldNotBeRenderedAsElementAttribute()
  221. {
  222. $html = $this->element->render();
  223. $this->assertNotContains('options="', $html, $html);
  224. }
  225. /**
  226. * @group ZF-4274
  227. */
  228. public function testCheckedValuesCanBePassedInConstructor()
  229. {
  230. $element = new Zend_Dojo_Form_Element_CheckBox('myCheckbox', array(
  231. 'checkedValue' => 'checkedVal',
  232. 'unCheckedValue' => 'UNCHECKED',
  233. ));
  234. $element->setView(new Zend_View());
  235. $html = $element->render();
  236. $this->assertContains('value="checkedVal"', $html, $html);
  237. }
  238. }
  239. // Call Zend_Dojo_Form_Element_CheckBoxTest::main() if this source file is executed directly.
  240. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_Form_Element_CheckBoxTest::main") {
  241. Zend_Dojo_Form_Element_CheckBoxTest::main();
  242. }