NumberSpinnerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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-2008 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_View_Helper_NumberSpinnerTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dojo_View_Helper_NumberSpinnerTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  27. /** Zend_Dojo_View_Helper_NumberSpinner */
  28. require_once 'Zend/Dojo/View/Helper/NumberSpinner.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_View_Helper_NumberSpinner.
  37. *
  38. * @package Zend_Dojo
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2008 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_View_Helper_NumberSpinnerTest 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_View_Helper_NumberSpinnerTest");
  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->helper = new Zend_Dojo_View_Helper_NumberSpinner();
  67. $this->helper->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. return $this->helper->numberSpinner(
  88. 'elementId',
  89. '5',
  90. array(
  91. 'smallDelta' => '10',
  92. 'min' => 9,
  93. 'max' => 1550,
  94. 'places' => 0,
  95. 'required' => true,
  96. ),
  97. array()
  98. );
  99. }
  100. public function testShouldAllowDeclarativeDijitCreation()
  101. {
  102. $html = $this->getElement();
  103. $this->assertRegexp('/<input[^>]*(dojoType="dijit.form.NumberSpinner")/', $html, $html);
  104. }
  105. public function testShouldAllowProgrammaticDijitCreation()
  106. {
  107. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic();
  108. $html = $this->getElement();
  109. $this->assertNotRegexp('/<input[^>]*(dojoType="dijit.form.NumberSpinner")/', $html);
  110. $this->assertNotNull($this->view->dojo()->getDijit('elementId'));
  111. }
  112. public function testShouldCreateTextInput()
  113. {
  114. $html = $this->getElement();
  115. $this->assertRegexp('/<input[^>]*(type="text")/', $html);
  116. }
  117. public function testShouldJsonEncodeConstraints()
  118. {
  119. $html = $this->getElement();
  120. if (!preg_match('/constraints="(.*?)(" )/', $html, $m)) {
  121. $this->fail('Did not serialize constraints');
  122. }
  123. $constraints = str_replace("'", '"', $m[1]);
  124. $constraints = Zend_Json::decode($constraints);
  125. $this->assertTrue(is_array($constraints), var_export($m[1], 1));
  126. $this->assertTrue(array_key_exists('min', $constraints));
  127. $this->assertTrue(array_key_exists('max', $constraints));
  128. $this->assertTrue(array_key_exists('places', $constraints));
  129. }
  130. public function testInvalidConstraintsShouldBeStrippedPriorToRendering()
  131. {
  132. $html = $this->helper->numberSpinner(
  133. 'foo',
  134. 5,
  135. array (
  136. 'constraints' => 'bogus',
  137. )
  138. );
  139. $this->assertNotContains('constraints="', $html);
  140. }
  141. }
  142. // Call Zend_Dojo_View_Helper_NumberSpinnerTest::main() if this source file is executed directly.
  143. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_View_Helper_NumberSpinnerTest::main") {
  144. Zend_Dojo_View_Helper_NumberSpinnerTest::main();
  145. }