RadioButtonTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_Dojo
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 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_Dojo_Form_Element_RadioButtonTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dojo_Form_Element_RadioButtonTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  27. /** Zend_Dojo_Form_Element_RadioButton */
  28. require_once 'Zend/Dojo/Form/Element/RadioButton.php';
  29. /** Zend_View */
  30. require_once 'Zend/View.php';
  31. /** Zend_Registry */
  32. require_once 'Zend/Registry.php';
  33. /** Zend_Dojo_View_Helper_Dojo */
  34. require_once 'Zend/Dojo/View/Helper/Dojo.php';
  35. /**
  36. * Test class for Zend_Dojo_Form_Element_RadioButton.
  37. *
  38. * @category Zend
  39. * @package Zend_Dojo
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @group Zend_Dojo
  44. * @group Zend_Dojo_Form
  45. */
  46. class Zend_Dojo_Form_Element_RadioButtonTest extends PHPUnit_Framework_TestCase
  47. {
  48. /**
  49. * Runs the test methods of this class.
  50. *
  51. * @return void
  52. */
  53. public static function main()
  54. {
  55. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_Form_Element_RadioButtonTest");
  56. $result = PHPUnit_TextUI_TestRunner::run($suite);
  57. }
  58. /**
  59. * Sets up the fixture, for example, open a network connection.
  60. * This method is called before a test is executed.
  61. *
  62. * @return void
  63. */
  64. public function setUp()
  65. {
  66. Zend_Registry::_unsetInstance();
  67. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  68. $this->view = $this->getView();
  69. $this->element = $this->getElement();
  70. $this->element->setView($this->view);
  71. }
  72. /**
  73. * Tears down the fixture, for example, close a network connection.
  74. * This method is called after a test is executed.
  75. *
  76. * @return void
  77. */
  78. public function tearDown()
  79. {
  80. }
  81. public function getView()
  82. {
  83. require_once 'Zend/View.php';
  84. $view = new Zend_View();
  85. $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
  86. return $view;
  87. }
  88. public function getElement()
  89. {
  90. $element = new Zend_Dojo_Form_Element_RadioButton(
  91. 'foo',
  92. array(
  93. 'value' => 'bar',
  94. 'label' => 'RadioButton',
  95. 'class' => 'someclass',
  96. 'style' => 'width: 100px;',
  97. 'multiOptions' => array(
  98. 'foo' => 'Foo',
  99. 'bar' => 'Bar',
  100. 'baz' => 'Baz',
  101. ),
  102. )
  103. );
  104. return $element;
  105. }
  106. public function testShouldAllowSpecifyingSeparatorText()
  107. {
  108. $this->element->setSeparator('<br />');
  109. $this->assertEquals('<br />', $this->element->getSeparator());
  110. }
  111. public function testAddingAnOptionShouldResetOptionsToArrayIfScalar()
  112. {
  113. $this->element->options = 'foo';
  114. $this->element->addMultiOption('bar', 'baz');
  115. $this->assertTrue(is_array($this->element->options));
  116. }
  117. public function testAddMultiOptionsShouldPassKeyValueArraysAsIndividualOptions()
  118. {
  119. $this->element->addMultiOptions(array(
  120. array('key' => 'foo', 'value' => 'bar'),
  121. array('key' => 'bar', 'value' => 'baz'),
  122. ));
  123. $this->assertEquals('bar', $this->element->getMultiOption('foo'));
  124. $this->assertEquals('baz', $this->element->getMultiOption('bar'));
  125. }
  126. public function testShouldAllowRemovingIndividualOptions()
  127. {
  128. $this->element->removeMultiOption('bar');
  129. $this->assertNull($this->element->getMultiOption('bar'));
  130. }
  131. public function testOptionsShouldBeTranslatable()
  132. {
  133. $translations = array(
  134. 'Foo' => 'This is Foo',
  135. 'Bar' => 'This is Bar',
  136. 'Baz' => 'This is Baz',
  137. );
  138. require_once 'Zend/Translate.php';
  139. $translate = new Zend_Translate('array', $translations, 'en');
  140. $this->element->setTranslator($translate);
  141. $html = $this->element->render();
  142. foreach ($translations as $string) {
  143. $this->assertContains($string, $html);
  144. }
  145. }
  146. public function testShouldRenderRadioButtonDijit()
  147. {
  148. $html = $this->element->render();
  149. $this->assertContains('dojoType="dijit.form.RadioButton"', $html);
  150. }
  151. public function testPassingValueShouldMarkThatValueCheckedWhenRendering()
  152. {
  153. $html = $this->element->render();
  154. if (!preg_match('/(<input[^>]*(id="foo-bar")[^>]*>)/', $html, $matches)) {
  155. $this->fail('Did not find radio option matching bar');
  156. }
  157. $this->assertContains('checked="checked"', $matches[1]);
  158. }
  159. /**#+
  160. * @see ZF-3286
  161. */
  162. public function testShouldRegisterInArrayValidatorByDefault()
  163. {
  164. $this->assertTrue($this->element->registerInArrayValidator());
  165. }
  166. public function testShouldAllowSpecifyingWhetherOrNotToUseInArrayValidator()
  167. {
  168. $this->testShouldRegisterInArrayValidatorByDefault();
  169. $this->element->setRegisterInArrayValidator(false);
  170. $this->assertFalse($this->element->registerInArrayValidator());
  171. $this->element->setRegisterInArrayValidator(true);
  172. $this->assertTrue($this->element->registerInArrayValidator());
  173. }
  174. public function testInArrayValidatorShouldBeRegisteredAfterValidation()
  175. {
  176. $options = array(
  177. 'foo' => 'Foo Value',
  178. 'bar' => 'Bar Value',
  179. 'baz' => 'Baz Value',
  180. );
  181. $this->element->setMultiOptions($options);
  182. $this->assertFalse($this->element->getValidator('InArray'));
  183. $this->element->isValid('test');
  184. $validator = $this->element->getValidator('InArray');
  185. $this->assertTrue($validator instanceof Zend_Validate_InArray);
  186. }
  187. public function testShouldNotValidateIfValueIsNotInArray()
  188. {
  189. $options = array(
  190. 'foo' => 'Foo Value',
  191. 'bar' => 'Bar Value',
  192. 'baz' => 'Baz Value',
  193. );
  194. $this->element->setMultiOptions($options);
  195. $this->assertFalse($this->element->getValidator('InArray'));
  196. $this->assertFalse($this->element->isValid('test'));
  197. }
  198. /**#@-*/
  199. }
  200. // Call Zend_Dojo_Form_Element_RadioButtonTest::main() if this source file is executed directly.
  201. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_Form_Element_RadioButtonTest::main") {
  202. Zend_Dojo_Form_Element_RadioButtonTest::main();
  203. }