HorizontalSliderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_View_Helper_HorizontalSliderTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dojo_View_Helper_HorizontalSliderTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  27. /** Zend_Dojo_View_Helper_HorizontalSlider */
  28. require_once 'Zend/Dojo/View/Helper/HorizontalSlider.php';
  29. /** Zend_View */
  30. require_once 'Zend/View.php';
  31. /** Zend_Registry */
  32. require_once 'Zend/Registry.php';
  33. /** Zend_Dojo_Form */
  34. require_once 'Zend/Dojo/Form.php';
  35. /** Zend_Dojo_Form_SubForm */
  36. require_once 'Zend/Dojo/Form/SubForm.php';
  37. /** Zend_Dojo_View_Helper_Dojo */
  38. require_once 'Zend/Dojo/View/Helper/Dojo.php';
  39. /**
  40. * Test class for Zend_Dojo_View_Helper_HorizontalSlider.
  41. *
  42. * @package Zend_Dojo
  43. * @subpackage UnitTests
  44. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. */
  47. class Zend_Dojo_View_Helper_HorizontalSliderTest extends PHPUnit_Framework_TestCase
  48. {
  49. /**
  50. * Runs the test methods of this class.
  51. *
  52. * @return void
  53. */
  54. public static function main()
  55. {
  56. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_View_Helper_HorizontalSliderTest");
  57. $result = PHPUnit_TextUI_TestRunner::run($suite);
  58. }
  59. /**
  60. * Sets up the fixture, for example, open a network connection.
  61. * This method is called before a test is executed.
  62. *
  63. * @return void
  64. */
  65. public function setUp()
  66. {
  67. Zend_Registry::_unsetInstance();
  68. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  69. $this->view = $this->getView();
  70. $this->helper = new Zend_Dojo_View_Helper_HorizontalSlider();
  71. $this->helper->setView($this->view);
  72. }
  73. /**
  74. * Tears down the fixture, for example, close a network connection.
  75. * This method is called after a test is executed.
  76. *
  77. * @return void
  78. */
  79. public function tearDown()
  80. {
  81. }
  82. public function getView()
  83. {
  84. require_once 'Zend/View.php';
  85. $view = new Zend_View();
  86. $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
  87. return $view;
  88. }
  89. public function getElement()
  90. {
  91. return $this->helper->horizontalSlider(
  92. 'elementId',
  93. '',
  94. array(
  95. 'minimum' => -10,
  96. 'maximum' => 10,
  97. 'discreteValues' => 11,
  98. 'topDecoration' => array(
  99. 'labels' => array(
  100. ' ',
  101. '20%',
  102. '40%',
  103. '60%',
  104. '80%',
  105. ' ',
  106. ),
  107. 'container' => 'top',
  108. 'attribs' => array(
  109. 'container' => array(
  110. 'style' => 'height:1.2em; font-size=75%;color:gray;',
  111. ),
  112. 'labels' => array(
  113. 'style' => 'height:1em; font-size=75%;color:gray;',
  114. ),
  115. ),
  116. 'dijit' => 'HorizontalRuleLabels',
  117. ),
  118. 'bottomDecoration' => array(
  119. 'labels' => array(
  120. '0%',
  121. '50%',
  122. '100%',
  123. ),
  124. 'attribs' => array(
  125. 'labels' => array(
  126. 'style' => 'height:1em; font-size=75%;color:gray;',
  127. ),
  128. ),
  129. ),
  130. 'leftDecoration' => array(
  131. 'labels' => array(
  132. ' ',
  133. '20%',
  134. '40%',
  135. '60%',
  136. '80%',
  137. ' ',
  138. ),
  139. 'attribs' => array(
  140. 'container' => array(
  141. 'style' => 'height:1.2em; font-size=75%;color:gray;',
  142. ),
  143. 'labels' => array(
  144. 'style' => 'height:1em; font-size=75%;color:gray;',
  145. ),
  146. ),
  147. 'dijit' => 'VerticalRuleLabels',
  148. ),
  149. 'rightDecoration' => array(
  150. 'labels' => array(
  151. '0%',
  152. '50%',
  153. '100%',
  154. ),
  155. 'attribs' => array(
  156. 'labels' => array(
  157. 'style' => 'height:1em; font-size=75%;color:gray;',
  158. ),
  159. ),
  160. ),
  161. ),
  162. array()
  163. );
  164. }
  165. public function testShouldAllowDeclarativeDijitCreation()
  166. {
  167. $html = $this->getElement();
  168. $this->assertRegexp('/<div[^>]*(dojoType="dijit.form.HorizontalSlider")/', $html, $html);
  169. }
  170. public function testShouldAllowProgrammaticDijitCreation()
  171. {
  172. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic();
  173. $html = $this->getElement();
  174. $this->assertNotRegexp('/<div[^>]*(dojoType="dijit.form.HorizontalSlider")/', $html);
  175. $this->assertNotNull($this->view->dojo()->getDijit('elementId-slider'));
  176. }
  177. public function testShouldCreateOnChangeAttributeByDefault()
  178. {
  179. $html = $this->getElement();
  180. $this->assertContains('onChange="dojo.byId(\'elementId\').value = arguments[0];"', $html, $html);
  181. }
  182. public function testShouldCreateHiddenElementWithValue()
  183. {
  184. $html = $this->getElement();
  185. if (!preg_match('/(<input[^>]*(type="hidden")[^>]*>)/', $html, $m)) {
  186. $this->fail('No hidden element found');
  187. }
  188. $this->assertContains('id="elementId"', $m[1]);
  189. $this->assertContains('value="', $m[1]);
  190. }
  191. public function testShouldCreateTopAndBottomDecorationsWhenRequested()
  192. {
  193. $html = $this->getElement();
  194. $this->assertRegexp('/<div[^>]*(dojoType="dijit.form.HorizontalRule")/', $html, $html);
  195. $this->assertRegexp('/<ol[^>]*(dojoType="dijit.form.HorizontalRuleLabels")/', $html, $html);
  196. $this->assertContains('topDecoration', $html);
  197. $this->assertContains('bottomDecoration', $html);
  198. }
  199. public function testShouldIgnoreLeftAndRightDecorationsWhenPassed()
  200. {
  201. $html = $this->getElement();
  202. $this->assertNotContains('leftDecoration', $html);
  203. $this->assertNotContains('rightDecoration', $html);
  204. }
  205. /**
  206. * @expectedException Zend_Dojo_View_Exception
  207. */
  208. public function testSliderShouldRaiseExceptionIfMissingRequiredParameters()
  209. {
  210. $this->helper->prepareSlider('foo', 4);
  211. }
  212. public function testShouldAllowPassingLabelParametersViaDecorationParameters()
  213. {
  214. $html = $this->helper->horizontalSlider(
  215. 'elementId',
  216. '',
  217. array(
  218. 'minimum' => -10,
  219. 'maximum' => 10,
  220. 'discreteValues' => 11,
  221. 'topDecoration' => array(
  222. 'labels' => array(
  223. ' ',
  224. '20%',
  225. '40%',
  226. '60%',
  227. '80%',
  228. ' ',
  229. ),
  230. 'params' => array(
  231. 'required' => true,
  232. 'labels' => array(
  233. 'minimum' => 5,
  234. )
  235. ),
  236. 'dijit' => 'HorizontalRuleLabels',
  237. ),
  238. )
  239. );
  240. $this->assertContains('required="', $html);
  241. $this->assertContains('minimum="', $html);
  242. }
  243. /**
  244. * @group ZF-4435
  245. */
  246. public function testShouldCreateAppropriateIdsForElementsInSubForms()
  247. {
  248. $form = new Zend_Dojo_Form;
  249. $form->setDecorators(array(
  250. 'FormElements',
  251. array('TabContainer', array(
  252. 'id' => 'tabContainer',
  253. 'style' => 'width: 600px; height: 300px;',
  254. 'dijitParams' => array(
  255. 'tabPosition' => 'top'
  256. ),
  257. )),
  258. 'DijitForm',
  259. ));
  260. $sliderForm = new Zend_Dojo_Form_SubForm();
  261. $sliderForm->setAttribs(array(
  262. 'name' => 'slidertab',
  263. 'legend' => 'Slider Elements',
  264. ));
  265. $sliderForm->addElement(
  266. 'HorizontalSlider',
  267. 'slide1',
  268. array(
  269. 'label' => 'Slide me:',
  270. 'minimum' => 0,
  271. 'maximum' => 25,
  272. 'discreteValues' => 10,
  273. 'style' => 'width: 450px;',
  274. 'topDecorationDijit' => 'HorizontalRuleLabels',
  275. 'topDecorationLabels' => array('0%', '50%', '100%'),
  276. 'topDecorationParams' => array('style' => 'padding-bottom: 20px;')
  277. )
  278. );
  279. $form->addSubForm($sliderForm, 'slidertab')
  280. ->setView($this->getView());
  281. $html = $form->render();
  282. $this->assertContains('id="slidertab-slide1-slider"', $html);
  283. $this->assertContains('id="slidertab-slide1-slider-topDecoration"', $html);
  284. $this->assertContains('id="slidertab-slide1-slider-topDecoration-labels"', $html);
  285. }
  286. /**
  287. * @group ZF-5220
  288. */
  289. public function testLabelDivShouldOpenAndCloseBeforeLabelOl()
  290. {
  291. $html = $this->getElement();
  292. $this->assertNotRegexp('/<div[^>]*(dojoType="dijit.form.HorizontalRuleLabels")[^>]*><\/div>\s*<ol/s', $html, $html);
  293. $this->assertRegexp('/<div[^>]*><\/div>\s*<ol[^>]*(dojoType="dijit.form.HorizontalRuleLabels")/s', $html, $html);
  294. }
  295. }
  296. // Call Zend_Dojo_View_Helper_HorizontalSliderTest::main() if this source file is executed directly.
  297. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_View_Helper_HorizontalSliderTest::main") {
  298. Zend_Dojo_View_Helper_HorizontalSliderTest::main();
  299. }