CheckboxTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_Form
  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_Form_Element_CheckboxTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_CheckboxTest::main");
  25. }
  26. require_once 'Zend/Form/Element/Checkbox.php';
  27. /**
  28. * Test class for Zend_Form_Element_Checkbox
  29. *
  30. * @category Zend
  31. * @package Zend_Form
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Form
  36. */
  37. class Zend_Form_Element_CheckboxTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Runs the test methods of this class.
  41. *
  42. * @return void
  43. */
  44. public static function main()
  45. {
  46. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_CheckboxTest");
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. /**
  50. * Sets up the fixture, for example, open a network connection.
  51. * This method is called before a test is executed.
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. $this->element = new Zend_Form_Element_Checkbox('foo');
  58. }
  59. /**
  60. * Tears down the fixture, for example, close a network connection.
  61. * This method is called after a test is executed.
  62. *
  63. * @return void
  64. */
  65. public function tearDown()
  66. {
  67. }
  68. public function getView()
  69. {
  70. require_once 'Zend/View.php';
  71. return new Zend_View();
  72. }
  73. public function testCheckboxElementSubclassesXhtmlElement()
  74. {
  75. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  76. }
  77. public function testCheckboxElementInstanceOfBaseElement()
  78. {
  79. $this->assertTrue($this->element instanceof Zend_Form_Element);
  80. }
  81. public function testCheckboxElementUsesCheckboxHelperInViewHelperDecoratorByDefault()
  82. {
  83. $this->_checkZf2794();
  84. $decorator = $this->element->getDecorator('viewHelper');
  85. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  86. $decorator->setElement($this->element);
  87. $helper = $decorator->getHelper();
  88. $this->assertEquals('formCheckbox', $helper);
  89. }
  90. public function testCheckedFlagIsFalseByDefault()
  91. {
  92. $this->assertFalse($this->element->checked);
  93. }
  94. public function testCheckedAttributeNotRenderedByDefault()
  95. {
  96. require_once 'Zend/View.php';
  97. $view = new Zend_View();
  98. $html = $this->element->render($view);
  99. $this->assertNotContains('checked="checked"', $html);
  100. }
  101. public function testCheckedAttributeRenderedWhenCheckedFlagTrue()
  102. {
  103. require_once 'Zend/View.php';
  104. $view = new Zend_View();
  105. $this->element->checked = true;
  106. $html = $this->element->render($view);
  107. $this->assertContains('checked="checked"', $html);
  108. }
  109. public function testCheckedValueDefaultsToOne()
  110. {
  111. $this->assertEquals(1, $this->element->getCheckedValue());
  112. }
  113. public function testUncheckedValueDefaultsToZero()
  114. {
  115. $this->assertEquals(0, $this->element->getUncheckedValue());
  116. }
  117. public function testCanSetCheckedValue()
  118. {
  119. $this->testCheckedValueDefaultsToOne();
  120. $this->element->setCheckedValue('foo');
  121. $this->assertEquals('foo', $this->element->getCheckedValue());
  122. }
  123. public function testCanSetUncheckedValue()
  124. {
  125. $this->testUncheckedValueDefaultsToZero();
  126. $this->element->setUncheckedValue('foo');
  127. $this->assertEquals('foo', $this->element->getUncheckedValue());
  128. }
  129. public function testValueInitiallyUncheckedValue()
  130. {
  131. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  132. }
  133. public function testSettingValueToCheckedValueSetsWithEquivalentValue()
  134. {
  135. $this->testValueInitiallyUncheckedValue();
  136. $this->element->setValue($this->element->getCheckedValue());
  137. $this->assertEquals($this->element->getCheckedValue(), $this->element->getValue());
  138. }
  139. public function testSettingValueToAnythingOtherThanCheckedValueSetsAsUncheckedValue()
  140. {
  141. $this->testSettingValueToCheckedValueSetsWithEquivalentValue();
  142. $this->element->setValue('bogus');
  143. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  144. }
  145. public function testSettingCheckedFlagToTrueSetsValueToCheckedValue()
  146. {
  147. $this->testValueInitiallyUncheckedValue();
  148. $this->element->setChecked(true);
  149. $this->assertEquals($this->element->getCheckedValue(), $this->element->getValue());
  150. }
  151. public function testSettingCheckedFlagToFalseSetsValueToUncheckedValue()
  152. {
  153. $this->testSettingCheckedFlagToTrueSetsValueToCheckedValue();
  154. $this->element->setChecked(false);
  155. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  156. }
  157. public function testSettingValueToCheckedValueMarksElementAsChecked()
  158. {
  159. $this->testValueInitiallyUncheckedValue();
  160. $this->element->setValue($this->element->getCheckedValue());
  161. $this->assertTrue($this->element->checked);
  162. }
  163. public function testSettingValueToUncheckedValueMarksElementAsNotChecked()
  164. {
  165. $this->testSettingValueToCheckedValueMarksElementAsChecked();
  166. $this->element->setValue($this->element->getUncheckedValue());
  167. $this->assertFalse($this->element->checked);
  168. }
  169. public function testSetOptionsSetsInitialValueAccordingToCheckedAndUncheckedValues()
  170. {
  171. $options = array(
  172. 'checkedValue' => 'foo',
  173. 'uncheckedValue' => 'bar',
  174. );
  175. $element = new Zend_Form_Element_Checkbox('test', $options);
  176. $this->assertEquals($options['uncheckedValue'], $element->getValue());
  177. }
  178. public function testSetOptionsSetsInitialValueAccordingToSubmittedValues()
  179. {
  180. $options = array(
  181. 'test1' => array(
  182. 'value' => 'foo',
  183. 'checkedValue' => 'foo',
  184. 'uncheckedValue' => 'bar',
  185. ),
  186. 'test2' => array(
  187. 'value' => 'bar',
  188. 'checkedValue' => 'foo',
  189. 'uncheckedValue' => 'bar',
  190. ),
  191. );
  192. foreach ($options as $current) {
  193. $element = new Zend_Form_Element_Checkbox('test', $current);
  194. $this->assertEquals($current['value'], $element->getValue());
  195. $this->assertEquals($current['checkedValue'], $element->getCheckedValue());
  196. $this->assertEquals($current['uncheckedValue'], $element->getUncheckedValue());
  197. }
  198. }
  199. public function testCheckedValueAlwaysRenderedAsCheckboxValue()
  200. {
  201. $this->element->setValue($this->element->getUncheckedValue());
  202. $html = $this->element->render($this->getView());
  203. if (!preg_match_all('/(<input[^>]+>)/', $html, $matches)) {
  204. $this->fail('Unexpected generated HTML: ' . $html);
  205. }
  206. $this->assertEquals(2, count($matches[1]));
  207. foreach ($matches[1] as $element) {
  208. if (strstr($element, 'hidden')) {
  209. $this->assertContains($this->element->getUncheckedValue(), $element);
  210. } else {
  211. $this->assertContains($this->element->getCheckedValue(), $element);
  212. }
  213. }
  214. }
  215. /**
  216. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  217. *
  218. * @link http://framework.zend.com/issues/browse/ZF-2794
  219. * @return void
  220. */
  221. protected function _checkZf2794()
  222. {
  223. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  224. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  225. }
  226. }
  227. }
  228. // Call Zend_Form_Element_CheckboxTest::main() if this source file is executed directly.
  229. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_CheckboxTest::main") {
  230. Zend_Form_Element_CheckboxTest::main();
  231. }