2
0

RadioTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_RadioTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_RadioTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once 'Zend/Form/Element/Radio.php';
  28. /**
  29. * Test class for Zend_Form_Element_Radio
  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_RadioTest 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_RadioTest");
  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_Radio('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. $view = new Zend_View();
  73. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
  74. return $view;
  75. }
  76. public function testRadioElementSubclassesMultiElement()
  77. {
  78. $this->assertTrue($this->element instanceof Zend_Form_Element_Multi);
  79. }
  80. public function testRadioElementSubclassesXhtmlElement()
  81. {
  82. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  83. }
  84. public function testRadioElementInstanceOfBaseElement()
  85. {
  86. $this->assertTrue($this->element instanceof Zend_Form_Element);
  87. }
  88. public function testRadioElementIsNotAnArrayByDefault()
  89. {
  90. $this->assertFalse($this->element->isArray());
  91. }
  92. public function testHelperAttributeSetToFormRadioByDefault()
  93. {
  94. $this->assertEquals('formRadio', $this->element->getAttrib('helper'));
  95. }
  96. public function testRadioElementUsesRadioHelperInViewHelperDecoratorByDefault()
  97. {
  98. $this->_checkZf2794();
  99. $decorator = $this->element->getDecorator('viewHelper');
  100. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  101. $decorator->setElement($this->element);
  102. $helper = $decorator->getHelper();
  103. $this->assertEquals('formRadio', $helper);
  104. }
  105. public function testCanDisableIndividualRadioOptions()
  106. {
  107. $this->element->setMultiOptions(array(
  108. 'foo' => 'Foo',
  109. 'bar' => 'Bar',
  110. 'baz' => 'Baz',
  111. 'bat' => 'Bat',
  112. 'test' => 'Test',
  113. ))
  114. ->setAttrib('disable', array('baz', 'test'));
  115. $html = $this->element->render($this->getView());
  116. foreach (array('baz', 'test') as $test) {
  117. if (!preg_match('/(<input[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  118. $this->fail('Unable to find matching disabled option for ' . $test);
  119. }
  120. $this->assertRegexp('/<input[^>]*?(disabled="disabled")/', $m[1]);
  121. }
  122. foreach (array('foo', 'bar', 'bat') as $test) {
  123. if (!preg_match('/(<input[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  124. $this->fail('Unable to find matching option for ' . $test);
  125. }
  126. $this->assertNotRegexp('/<input[^>]*?(disabled="disabled")/', $m[1], var_export($m, 1));
  127. }
  128. }
  129. public function testSpecifiedSeparatorIsUsedWhenRendering()
  130. {
  131. $this->element->setMultiOptions(array(
  132. 'foo' => 'Foo',
  133. 'bar' => 'Bar',
  134. 'baz' => 'Baz',
  135. 'bat' => 'Bat',
  136. 'test' => 'Test',
  137. ))
  138. ->setSeparator('--FooBarFunSep--');
  139. $html = $this->element->render($this->getView());
  140. $this->assertContains($this->element->getSeparator(), $html);
  141. $count = substr_count($html, $this->element->getSeparator());
  142. $this->assertEquals(4, $count);
  143. }
  144. public function testRadioElementRendersDtDdWrapper()
  145. {
  146. $this->element->setMultiOptions(array(
  147. 'foo' => 'Foo',
  148. 'bar' => 'Bar',
  149. 'baz' => 'Baz',
  150. 'bat' => 'Bat',
  151. 'test' => 'Test',
  152. ));
  153. $html = $this->element->render($this->getView());
  154. $this->assertRegexp('#<dt[^>]*>&nbsp;</dt>.*?<dd#s', $html, $html);
  155. }
  156. /**
  157. * @group ZF-6426
  158. */
  159. public function testRenderingShouldCreateLabelWithoutForAttribute()
  160. {
  161. $this->element->setMultiOptions(array(
  162. 'foo' => 'Foo',
  163. 'bar' => 'Bar',
  164. ))
  165. ->setLabel('Foo');
  166. $html = $this->element->render($this->getView());
  167. $this->assertNotContains('for="foo"', $html);
  168. }
  169. /**
  170. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  171. *
  172. * @link http://framework.zend.com/issues/browse/ZF-2794
  173. * @return void
  174. */
  175. protected function _checkZf2794()
  176. {
  177. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  178. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  179. }
  180. }
  181. }
  182. // Call Zend_Form_Element_RadioTest::main() if this source file is executed directly.
  183. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_RadioTest::main") {
  184. Zend_Form_Element_RadioTest::main();
  185. }