RadioButtonTest.php 7.0 KB

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