/tests/Zend/Form/Element/CheckboxTest.php

https://github.com/Exercise/zf2 · PHP · 219 lines · 157 code · 26 blank · 36 comment · 3 complexity · 9b7982c0fe812737b2979acd0e2f8ae2 MD5 · raw file

  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. namespace ZendTest\Form\Element;
  23. use Zend\Form\Element\Checkbox as CheckboxElement,
  24. Zend\Form\Element\Xhtml as XhtmlElement,
  25. Zend\Form\Element,
  26. Zend\Form\Decorator,
  27. Zend\View\View;
  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 CheckboxTest extends \PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Sets up the fixture, for example, open a network connection.
  42. * This method is called before a test is executed.
  43. *
  44. * @return void
  45. */
  46. public function setUp()
  47. {
  48. $this->element = new CheckboxElement('foo');
  49. }
  50. public function getView()
  51. {
  52. return new View();
  53. }
  54. public function testCheckboxElementSubclassesXhtmlElement()
  55. {
  56. $this->assertTrue($this->element instanceof XhtmlElement);
  57. }
  58. public function testCheckboxElementInstanceOfBaseElement()
  59. {
  60. $this->assertTrue($this->element instanceof Element);
  61. }
  62. public function testCheckboxElementUsesCheckboxHelperInViewHelperDecoratorByDefault()
  63. {
  64. $decorator = $this->element->getDecorator('viewHelper');
  65. $this->assertTrue($decorator instanceof Decorator\ViewHelper);
  66. $decorator->setElement($this->element);
  67. $helper = $decorator->getHelper();
  68. $this->assertEquals('formCheckbox', $helper);
  69. }
  70. public function testCheckedFlagIsFalseByDefault()
  71. {
  72. $this->assertFalse($this->element->checked);
  73. }
  74. public function testCheckedAttributeNotRenderedByDefault()
  75. {
  76. $view = new View();
  77. $html = $this->element->render($view);
  78. $this->assertNotContains('checked="checked"', $html);
  79. }
  80. public function testCheckedAttributeRenderedWhenCheckedFlagTrue()
  81. {
  82. $view = new View();
  83. $this->element->checked = true;
  84. $html = $this->element->render($view);
  85. $this->assertContains('checked="checked"', $html);
  86. }
  87. public function testCheckedValueDefaultsToOne()
  88. {
  89. $this->assertEquals(1, $this->element->getCheckedValue());
  90. }
  91. public function testUncheckedValueDefaultsToZero()
  92. {
  93. $this->assertEquals(0, $this->element->getUncheckedValue());
  94. }
  95. public function testCanSetCheckedValue()
  96. {
  97. $this->testCheckedValueDefaultsToOne();
  98. $this->element->setCheckedValue('foo');
  99. $this->assertEquals('foo', $this->element->getCheckedValue());
  100. }
  101. public function testCanSetUncheckedValue()
  102. {
  103. $this->testUncheckedValueDefaultsToZero();
  104. $this->element->setUncheckedValue('foo');
  105. $this->assertEquals('foo', $this->element->getUncheckedValue());
  106. }
  107. public function testValueInitiallyUncheckedValue()
  108. {
  109. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  110. }
  111. public function testSettingValueToCheckedValueSetsWithEquivalentValue()
  112. {
  113. $this->testValueInitiallyUncheckedValue();
  114. $this->element->setValue($this->element->getCheckedValue());
  115. $this->assertEquals($this->element->getCheckedValue(), $this->element->getValue());
  116. }
  117. public function testSettingValueToAnythingOtherThanCheckedValueSetsAsUncheckedValue()
  118. {
  119. $this->testSettingValueToCheckedValueSetsWithEquivalentValue();
  120. $this->element->setValue('bogus');
  121. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  122. }
  123. public function testSettingCheckedFlagToTrueSetsValueToCheckedValue()
  124. {
  125. $this->testValueInitiallyUncheckedValue();
  126. $this->element->setChecked(true);
  127. $this->assertEquals($this->element->getCheckedValue(), $this->element->getValue());
  128. }
  129. public function testSettingCheckedFlagToFalseSetsValueToUncheckedValue()
  130. {
  131. $this->testSettingCheckedFlagToTrueSetsValueToCheckedValue();
  132. $this->element->setChecked(false);
  133. $this->assertEquals($this->element->getUncheckedValue(), $this->element->getValue());
  134. }
  135. public function testSettingValueToCheckedValueMarksElementAsChecked()
  136. {
  137. $this->testValueInitiallyUncheckedValue();
  138. $this->element->setValue($this->element->getCheckedValue());
  139. $this->assertTrue($this->element->checked);
  140. }
  141. public function testSettingValueToUncheckedValueMarksElementAsNotChecked()
  142. {
  143. $this->testSettingValueToCheckedValueMarksElementAsChecked();
  144. $this->element->setValue($this->element->getUncheckedValue());
  145. $this->assertFalse($this->element->checked);
  146. }
  147. public function testSetOptionsSetsInitialValueAccordingToCheckedAndUncheckedValues()
  148. {
  149. $options = array(
  150. 'checkedValue' => 'foo',
  151. 'uncheckedValue' => 'bar',
  152. );
  153. $element = new CheckboxElement('test', $options);
  154. $this->assertEquals($options['uncheckedValue'], $element->getValue());
  155. }
  156. public function testSetOptionsSetsInitialValueAccordingToSubmittedValues()
  157. {
  158. $options = array(
  159. 'test1' => array(
  160. 'value' => 'foo',
  161. 'checkedValue' => 'foo',
  162. 'uncheckedValue' => 'bar',
  163. ),
  164. 'test2' => array(
  165. 'value' => 'bar',
  166. 'checkedValue' => 'foo',
  167. 'uncheckedValue' => 'bar',
  168. ),
  169. );
  170. foreach ($options as $current) {
  171. $element = new CheckboxElement('test', $current);
  172. $this->assertEquals($current['value'], $element->getValue());
  173. $this->assertEquals($current['checkedValue'], $element->getCheckedValue());
  174. $this->assertEquals($current['uncheckedValue'], $element->getUncheckedValue());
  175. }
  176. }
  177. public function testCheckedValueAlwaysRenderedAsCheckboxValue()
  178. {
  179. $this->element->setValue($this->element->getUncheckedValue());
  180. $html = $this->element->render($this->getView());
  181. if (!preg_match_all('/(<input[^>]+>)/', $html, $matches)) {
  182. $this->fail('Unexpected generated HTML: ' . $html);
  183. }
  184. $this->assertEquals(2, count($matches[1]));
  185. foreach ($matches[1] as $element) {
  186. if (strstr($element, 'hidden')) {
  187. $this->assertContains($this->element->getUncheckedValue(), $element);
  188. } else {
  189. $this->assertContains($this->element->getCheckedValue(), $element);
  190. }
  191. }
  192. }
  193. }