NumberSpinnerTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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-2012 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_NumberSpinnerTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dojo_Form_Element_NumberSpinnerTest::main");
  25. }
  26. /** Zend_Dojo_Form_Element_NumberSpinner */
  27. require_once 'Zend/Dojo/Form/Element/NumberSpinner.php';
  28. /** Zend_View */
  29. require_once 'Zend/View.php';
  30. /** Zend_Registry */
  31. require_once 'Zend/Registry.php';
  32. /** Zend_Dojo_View_Helper_Dojo */
  33. require_once 'Zend/Dojo/View/Helper/Dojo.php';
  34. /**
  35. * Test class for Zend_Dojo_Form_Element_NumberSpinner.
  36. *
  37. * @category Zend
  38. * @package Zend_Dojo
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Dojo
  43. * @group Zend_Dojo_Form
  44. */
  45. class Zend_Dojo_Form_Element_NumberSpinnerTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * Runs the test methods of this class.
  49. *
  50. * @return void
  51. */
  52. public static function main()
  53. {
  54. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_Form_Element_NumberSpinnerTest");
  55. $result = PHPUnit_TextUI_TestRunner::run($suite);
  56. }
  57. /**
  58. * Sets up the fixture, for example, open a network connection.
  59. * This method is called before a test is executed.
  60. *
  61. * @return void
  62. */
  63. public function setUp()
  64. {
  65. Zend_Registry::_unsetInstance();
  66. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  67. $this->view = $this->getView();
  68. $this->element = $this->getElement();
  69. $this->element->setView($this->view);
  70. }
  71. /**
  72. * Tears down the fixture, for example, close a network connection.
  73. * This method is called after a test is executed.
  74. *
  75. * @return void
  76. */
  77. public function tearDown()
  78. {
  79. }
  80. public function getView()
  81. {
  82. require_once 'Zend/View.php';
  83. $view = new Zend_View();
  84. $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
  85. return $view;
  86. }
  87. public function getElement()
  88. {
  89. $element = new Zend_Dojo_Form_Element_NumberSpinner(
  90. 'foo',
  91. array(
  92. 'value' => 'some text',
  93. 'label' => 'NumberSpinner',
  94. 'class' => 'someclass',
  95. 'style' => 'width: 100px;',
  96. )
  97. );
  98. return $element;
  99. }
  100. public function testDefaultTimeoutAccessorsShouldProxyToDijitParams()
  101. {
  102. $this->assertNull($this->element->getDefaultTimeout());
  103. $this->assertFalse(array_key_exists('defaultTimeout', $this->element->dijitParams));
  104. $this->element->setDefaultTimeout(20);
  105. $this->assertEquals(20, $this->element->getDefaultTimeout());
  106. $this->assertEquals(20, $this->element->dijitParams['defaultTimeout']);
  107. }
  108. public function testTimeoutChangeRateAccessorsShouldProxyToDijitParams()
  109. {
  110. $this->assertNull($this->element->getTimeoutChangeRate());
  111. $this->assertFalse(array_key_exists('timeoutChangeRate', $this->element->dijitParams));
  112. $this->element->setTimeoutChangeRate(20);
  113. $this->assertEquals(20, $this->element->getTimeoutChangeRate());
  114. $this->assertEquals(20, $this->element->dijitParams['timeoutChangeRate']);
  115. }
  116. public function testLargeDeltaAccessorsShouldProxyToDijitParams()
  117. {
  118. $this->assertNull($this->element->getLargeDelta());
  119. $this->assertFalse(array_key_exists('largeDelta', $this->element->dijitParams));
  120. $this->element->setLargeDelta(20);
  121. $this->assertEquals(20, $this->element->getLargeDelta());
  122. $this->assertEquals(20, $this->element->dijitParams['largeDelta']);
  123. }
  124. public function testSmallDeltaAccessorsShouldProxyToDijitParams()
  125. {
  126. $this->assertNull($this->element->getSmallDelta());
  127. $this->assertFalse(array_key_exists('smallDelta', $this->element->dijitParams));
  128. $this->element->setSmallDelta(20);
  129. $this->assertEquals(20, $this->element->getSmallDelta());
  130. $this->assertEquals(20, $this->element->dijitParams['smallDelta']);
  131. }
  132. public function testIntermediateChangesAccessorsShouldProxyToDijitParams()
  133. {
  134. $this->assertFalse($this->element->getIntermediateChanges());
  135. $this->assertFalse(array_key_exists('intermediateChanges', $this->element->dijitParams));
  136. $this->element->setIntermediateChanges(true);
  137. $this->assertTrue($this->element->getIntermediateChanges());
  138. $this->assertTrue($this->element->dijitParams['intermediateChanges']);
  139. }
  140. public function testRangeMessageAccessorsShouldProxyToDijitParams()
  141. {
  142. $this->assertNull($this->element->getRangeMessage());
  143. $this->assertFalse(array_key_exists('rangeMessage', $this->element->dijitParams));
  144. $this->element->setRangeMessage('foo');
  145. $this->assertEquals('foo', $this->element->getRangeMessage());
  146. $this->assertEquals('foo', $this->element->dijitParams['rangeMessage']);
  147. }
  148. public function testMinAccessorsShouldProxyToConstraintsDijitParam()
  149. {
  150. $this->assertNull($this->element->getMin());
  151. $this->assertFalse(array_key_exists('constraints', $this->element->dijitParams));
  152. $this->element->setMin(5);
  153. $this->assertEquals(5, $this->element->getMin());
  154. $this->assertEquals(5, $this->element->dijitParams['constraints']['min']);
  155. }
  156. public function testMaxAccessorsShouldProxyToConstraintsDijitParam()
  157. {
  158. $this->assertNull($this->element->getMax());
  159. $this->assertFalse(array_key_exists('constraints', $this->element->dijitParams));
  160. $this->element->setMax(5);
  161. $this->assertEquals(5, $this->element->getMax());
  162. $this->assertEquals(5, $this->element->dijitParams['constraints']['max']);
  163. }
  164. public function testShouldRenderNumberSpinnerDijit()
  165. {
  166. $html = $this->element->render();
  167. $this->assertContains('dojoType="dijit.form.NumberSpinner"', $html);
  168. }
  169. /**
  170. * @group ZF-4638
  171. */
  172. public function testRenderingShouldOutputMinAndMaxConstraints()
  173. {
  174. $this->element->setMin(5)
  175. ->setMax(10);
  176. $html = $this->element->render();
  177. // Note that ' is converted to &#39; in Zend_View_Helper_HtmlElement::_htmlAttribs() (line 116)
  178. $html = str_replace('&#39;', "'", $html);
  179. $this->assertRegexp('/\'min\':\s*5/', $html, $html);
  180. $this->assertRegexp('/\'max\':\s*10/', $html, $html);
  181. }
  182. public function testSmallAndLargeDeltaCanBeSetAsDecimalValues()
  183. {
  184. $this->element->setSmallDelta(20.5);
  185. $this->assertEquals(20.5, $this->element->getSmallDelta());
  186. $this->element->setLargeDelta(50.5);
  187. $this->assertEquals(50.5, $this->element->getLargeDelta());
  188. }
  189. public function testMinAndMaxValuesCanBeSetAsDecimalValues()
  190. {
  191. $this->element->setMin(20.5);
  192. $this->assertEquals(20.5, $this->element->getMin());
  193. $this->element->setMax(50.5);
  194. $this->assertEquals(50.5, $this->element->getMax());
  195. }
  196. }
  197. // Call Zend_Dojo_Form_Element_NumberSpinnerTest::main() if this source file is executed directly.
  198. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_Form_Element_NumberSpinnerTest::main") {
  199. Zend_Dojo_Form_Element_NumberSpinnerTest::main();
  200. }