CheckBoxTest.php 8.7 KB

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