PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/tests/Zend/Form/Element/RadioTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 270 lines | 169 code | 28 blank | 73 comment | 8 complexity | a21e9f5581f59d93c1195a86920858b4 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: RadioTest.php 25109 2012-11-07 20:48:04Z rob $
  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 'Zend/Form/Element/Radio.php';
  27. /**
  28. * Test class for Zend_Form_Element_Radio
  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 Zend_Form_Element_RadioTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Runs the test methods of this class.
  41. *
  42. * @return void
  43. */
  44. public static function main()
  45. {
  46. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_RadioTest");
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. /**
  50. * Sets up the fixture, for example, open a network connection.
  51. * This method is called before a test is executed.
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. $this->element = new Zend_Form_Element_Radio('foo');
  58. }
  59. /**
  60. * Tears down the fixture, for example, close a network connection.
  61. * This method is called after a test is executed.
  62. *
  63. * @return void
  64. */
  65. public function tearDown()
  66. {
  67. }
  68. public function getView()
  69. {
  70. require_once 'Zend/View.php';
  71. $view = new Zend_View();
  72. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
  73. return $view;
  74. }
  75. public function testRadioElementSubclassesMultiElement()
  76. {
  77. $this->assertTrue($this->element instanceof Zend_Form_Element_Multi);
  78. }
  79. public function testRadioElementSubclassesXhtmlElement()
  80. {
  81. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  82. }
  83. public function testRadioElementInstanceOfBaseElement()
  84. {
  85. $this->assertTrue($this->element instanceof Zend_Form_Element);
  86. }
  87. public function testRadioElementIsNotAnArrayByDefault()
  88. {
  89. $this->assertFalse($this->element->isArray());
  90. }
  91. public function testHelperAttributeSetToFormRadioByDefault()
  92. {
  93. $this->assertEquals('formRadio', $this->element->getAttrib('helper'));
  94. }
  95. public function testRadioElementUsesRadioHelperInViewHelperDecoratorByDefault()
  96. {
  97. $this->_checkZf2794();
  98. $decorator = $this->element->getDecorator('viewHelper');
  99. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  100. $decorator->setElement($this->element);
  101. $helper = $decorator->getHelper();
  102. $this->assertEquals('formRadio', $helper);
  103. }
  104. public function testCanDisableIndividualRadioOptions()
  105. {
  106. $this->element->setMultiOptions(array(
  107. 'foo' => 'Foo',
  108. 'bar' => 'Bar',
  109. 'baz' => 'Baz',
  110. 'bat' => 'Bat',
  111. 'test' => 'Test',
  112. ))
  113. ->setAttrib('disable', array('baz', 'test'));
  114. $html = $this->element->render($this->getView());
  115. foreach (array('baz', 'test') as $test) {
  116. if (!preg_match('/(<input[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  117. $this->fail('Unable to find matching disabled option for ' . $test);
  118. }
  119. $this->assertRegexp('/<input[^>]*?(disabled="disabled")/', $m[1]);
  120. }
  121. foreach (array('foo', 'bar', 'bat') as $test) {
  122. if (!preg_match('/(<input[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  123. $this->fail('Unable to find matching option for ' . $test);
  124. }
  125. $this->assertNotRegexp('/<input[^>]*?(disabled="disabled")/', $m[1], var_export($m, 1));
  126. }
  127. }
  128. public function testSpecifiedSeparatorIsUsedWhenRendering()
  129. {
  130. $this->element->setMultiOptions(array(
  131. 'foo' => 'Foo',
  132. 'bar' => 'Bar',
  133. 'baz' => 'Baz',
  134. 'bat' => 'Bat',
  135. 'test' => 'Test',
  136. ))
  137. ->setSeparator('--FooBarFunSep--');
  138. $html = $this->element->render($this->getView());
  139. $this->assertContains($this->element->getSeparator(), $html);
  140. $count = substr_count($html, $this->element->getSeparator());
  141. $this->assertEquals(4, $count);
  142. }
  143. public function testRadioElementRendersDtDdWrapper()
  144. {
  145. $this->element->setMultiOptions(array(
  146. 'foo' => 'Foo',
  147. 'bar' => 'Bar',
  148. 'baz' => 'Baz',
  149. 'bat' => 'Bat',
  150. 'test' => 'Test',
  151. ));
  152. $html = $this->element->render($this->getView());
  153. $this->assertRegexp('#<dt[^>]*>&\#160;</dt>.*?<dd#s', $html, $html);
  154. }
  155. /**
  156. * @group ZF-9682
  157. */
  158. public function testCustomLabelDecorator()
  159. {
  160. $form = new Zend_Form();
  161. $form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/../_files/decorators/', 'decorator');
  162. $form->addElement($this->element);
  163. $element = $form->getElement('foo');
  164. $this->assertTrue(
  165. $element->getDecorator('Label') instanceof My_Decorator_Label
  166. );
  167. }
  168. /**
  169. * @group ZF-6426
  170. */
  171. public function testRenderingShouldCreateLabelWithoutForAttribute()
  172. {
  173. $this->element->setMultiOptions(array(
  174. 'foo' => 'Foo',
  175. 'bar' => 'Bar',
  176. ))
  177. ->setLabel('Foo');
  178. $html = $this->element->render($this->getView());
  179. $this->assertNotContains('for="foo"', $html);
  180. }
  181. /**
  182. * @group ZF-11517
  183. */
  184. public function testCreationWithIndividualDecoratorsAsConstructorOptionsWithoutLabel()
  185. {
  186. $element = new Zend_Form_Element_Radio(array(
  187. 'name' => 'foo',
  188. 'multiOptions' => array(
  189. 'bar' => 'Bar',
  190. 'baz' => 'Baz',
  191. ),
  192. 'decorators' => array(
  193. 'ViewHelper',
  194. ),
  195. ));
  196. $this->assertFalse($element->getDecorator('label'));
  197. }
  198. /**
  199. * @group ZF-11517
  200. */
  201. public function testRenderingWithIndividualDecoratorsAsConstructorOptionsWithoutLabel()
  202. {
  203. $element = new Zend_Form_Element_Radio(array(
  204. 'name' => 'foo',
  205. 'multiOptions' => array(
  206. 'bar' => 'Bar',
  207. 'baz' => 'Baz',
  208. ),
  209. 'decorators' => array(
  210. 'ViewHelper',
  211. ),
  212. ));
  213. $html = $element->render($this->getView());
  214. $this->assertNotContains('<dt id="foo-label">&#160;</dt>', $html);
  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. * Prove the fluent interface on Zend_Form_Element_Radio::loadDefaultDecorators
  230. *
  231. * @link http://framework.zend.com/issues/browse/ZF-9913
  232. * @return void
  233. */
  234. public function testFluentInterfaceOnLoadDefaultDecorators()
  235. {
  236. $this->assertSame($this->element, $this->element->loadDefaultDecorators());
  237. }
  238. }
  239. // Call Zend_Form_Element_RadioTest::main() if this source file is executed directly.
  240. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_RadioTest::main") {
  241. Zend_Form_Element_RadioTest::main();
  242. }