HorizontalSliderTest.php 11 KB

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