CheckboxTest.php 8.7 KB

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