2
0

RadioTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. // Call Zend_Form_Element_RadioTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_RadioTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/Form/Element/Radio.php';
  8. /**
  9. * Test class for Zend_Form_Element_Radio
  10. */
  11. class Zend_Form_Element_RadioTest extends PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * Runs the test methods of this class.
  15. *
  16. * @return void
  17. */
  18. public static function main()
  19. {
  20. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_RadioTest");
  21. $result = PHPUnit_TextUI_TestRunner::run($suite);
  22. }
  23. /**
  24. * Sets up the fixture, for example, open a network connection.
  25. * This method is called before a test is executed.
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. $this->element = new Zend_Form_Element_Radio('foo');
  32. }
  33. /**
  34. * Tears down the fixture, for example, close a network connection.
  35. * This method is called after a test is executed.
  36. *
  37. * @return void
  38. */
  39. public function tearDown()
  40. {
  41. }
  42. public function getView()
  43. {
  44. require_once 'Zend/View.php';
  45. $view = new Zend_View();
  46. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
  47. return $view;
  48. }
  49. public function testRadioElementSubclassesMultiElement()
  50. {
  51. $this->assertTrue($this->element instanceof Zend_Form_Element_Multi);
  52. }
  53. public function testRadioElementSubclassesXhtmlElement()
  54. {
  55. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  56. }
  57. public function testRadioElementInstanceOfBaseElement()
  58. {
  59. $this->assertTrue($this->element instanceof Zend_Form_Element);
  60. }
  61. public function testRadioElementIsNotAnArrayByDefault()
  62. {
  63. $this->assertFalse($this->element->isArray());
  64. }
  65. public function testHelperAttributeSetToFormRadioByDefault()
  66. {
  67. $this->assertEquals('formRadio', $this->element->getAttrib('helper'));
  68. }
  69. public function testRadioElementUsesRadioHelperInViewHelperDecoratorByDefault()
  70. {
  71. $this->_checkZf2794();
  72. $decorator = $this->element->getDecorator('viewHelper');
  73. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  74. $decorator->setElement($this->element);
  75. $helper = $decorator->getHelper();
  76. $this->assertEquals('formRadio', $helper);
  77. }
  78. public function testCanDisableIndividualRadioOptions()
  79. {
  80. $this->element->setMultiOptions(array(
  81. 'foo' => 'Foo',
  82. 'bar' => 'Bar',
  83. 'baz' => 'Baz',
  84. 'bat' => 'Bat',
  85. 'test' => 'Test',
  86. ))
  87. ->setAttrib('disable', array('baz', 'test'));
  88. $html = $this->element->render($this->getView());
  89. foreach (array('baz', 'test') as $test) {
  90. if (!preg_match('/(<input[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  91. $this->fail('Unable to find matching disabled option for ' . $test);
  92. }
  93. $this->assertRegexp('/<input[^>]*?(disabled="disabled")/', $m[1]);
  94. }
  95. foreach (array('foo', 'bar', 'bat') as $test) {
  96. if (!preg_match('/(<input[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  97. $this->fail('Unable to find matching option for ' . $test);
  98. }
  99. $this->assertNotRegexp('/<input[^>]*?(disabled="disabled")/', $m[1], var_export($m, 1));
  100. }
  101. }
  102. public function testSpecifiedSeparatorIsUsedWhenRendering()
  103. {
  104. $this->element->setMultiOptions(array(
  105. 'foo' => 'Foo',
  106. 'bar' => 'Bar',
  107. 'baz' => 'Baz',
  108. 'bat' => 'Bat',
  109. 'test' => 'Test',
  110. ))
  111. ->setSeparator('--FooBarFunSep--');
  112. $html = $this->element->render($this->getView());
  113. $this->assertContains($this->element->getSeparator(), $html);
  114. $count = substr_count($html, $this->element->getSeparator());
  115. $this->assertEquals(4, $count);
  116. }
  117. public function testRadioElementRendersDtDdWrapper()
  118. {
  119. $this->element->setMultiOptions(array(
  120. 'foo' => 'Foo',
  121. 'bar' => 'Bar',
  122. 'baz' => 'Baz',
  123. 'bat' => 'Bat',
  124. 'test' => 'Test',
  125. ));
  126. $html = $this->element->render($this->getView());
  127. $this->assertRegexp('#<dt[^>]*>&nbsp;</dt>.*?<dd#s', $html, $html);
  128. }
  129. /**
  130. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  131. *
  132. * @link http://framework.zend.com/issues/browse/ZF-2794
  133. * @return void
  134. */
  135. protected function _checkZf2794()
  136. {
  137. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  138. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  139. }
  140. }
  141. }
  142. // Call Zend_Form_Element_RadioTest::main() if this source file is executed directly.
  143. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_RadioTest::main") {
  144. Zend_Form_Element_RadioTest::main();
  145. }