PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

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