ComboBoxTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_View_Helper_ComboBoxTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dojo_View_Helper_ComboBoxTest::main");
  25. }
  26. /** Zend_Dojo_View_Helper_ComboBox */
  27. require_once 'Zend/Dojo/View/Helper/ComboBox.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_View_Helper_ComboBox.
  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_View
  44. */
  45. class Zend_Dojo_View_Helper_ComboBoxTest 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_View_Helper_ComboBoxTest");
  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->helper = new Zend_Dojo_View_Helper_ComboBox();
  69. $this->helper->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 getElementAsSelect()
  88. {
  89. return $this->helper->comboBox(
  90. 'elementId',
  91. 'someCombo',
  92. array(),
  93. array(),
  94. array(
  95. 'red' => 'Rouge',
  96. 'blue' => 'Bleu',
  97. 'white' => 'Blanc',
  98. 'orange' => 'Orange',
  99. 'black' => 'Noir',
  100. 'green' => 'Vert',
  101. )
  102. );
  103. }
  104. public function getElementAsRemoter()
  105. {
  106. return $this->helper->comboBox(
  107. 'elementId',
  108. 'someCombo',
  109. array(
  110. 'store' => array(
  111. 'store' => 'stateStore',
  112. 'type' => 'dojo.data.ItemFileReadStore',
  113. 'params' => array(
  114. 'url' => 'states.txt'
  115. )
  116. ),
  117. 'searchAttr' => 'name'
  118. ),
  119. array()
  120. );
  121. }
  122. public function testShouldAllowDeclarativeDijitCreationAsSelect()
  123. {
  124. $html = $this->getElementAsSelect();
  125. $this->assertRegexp('/<select[^>]*(dojoType="dijit.form.ComboBox")/', $html, $html);
  126. }
  127. public function testShouldAllowProgrammaticDijitCreationAsSelect()
  128. {
  129. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic();
  130. $html = $this->getElementAsSelect();
  131. $this->assertNotRegexp('/<select[^>]*(dojoType="dijit.form.ComboBox")/', $html);
  132. $this->assertNotNull($this->view->dojo()->getDijit('elementId'));
  133. }
  134. public function testShouldAllowDeclarativeDijitCreationAsRemoter()
  135. {
  136. $html = $this->getElementAsRemoter();
  137. if (!preg_match('/(<input[^>]*(dojoType="dijit.form.ComboBox"))/', $html, $m)) {
  138. $this->fail('Did not create text input as remoter: ' . $html);
  139. }
  140. $this->assertContains('type="text"', $m[1]);
  141. }
  142. public function testShouldAllowProgrammaticDijitCreationAsRemoter()
  143. {
  144. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic();
  145. $html = $this->getElementAsRemoter();
  146. $this->assertNotRegexp('/<input[^>]*(dojoType="dijit.form.ComboBox")/', $html);
  147. $this->assertRegexp('/<input[^>]*(type="text")/', $html);
  148. $this->assertNotNull($this->view->dojo()->getDijit('elementId'));
  149. $found = false;
  150. $this->assertContains('var stateStore;', $this->view->dojo()->getJavascript());
  151. $scripts = $this->view->dojo()->_getZendLoadActions();
  152. foreach ($scripts as $js) {
  153. if (strstr($js, 'stateStore = new ')) {
  154. $found = true;
  155. break;
  156. }
  157. }
  158. $this->assertTrue($found, 'No store declaration found: ' . var_export($scripts, 1));
  159. }
  160. public function testShouldAllowAlternateNotationToSpecifyRemoter()
  161. {
  162. $html = $this->helper->comboBox(
  163. 'elementId',
  164. 'someCombo',
  165. array(
  166. 'store' => 'stateStore',
  167. 'storeType' => 'dojo.data.ItemFileReadStore',
  168. 'storeParams' => array('url' => 'states.txt'),
  169. 'searchAttr' => 'name',
  170. )
  171. );
  172. if (!preg_match('/(<input[^>]*(dojoType="dijit.form.ComboBox"))/', $html, $m)) {
  173. $this->fail('Did not create text input as remoter: ' . $html);
  174. }
  175. $this->assertContains('type="text"', $m[1]);
  176. if (!preg_match('/(<div[^>]*(?:dojoType="dojo.data.ItemFileReadStore")[^>]*>)/', $html, $m)) {
  177. $this->fail('Did not create data store: ' . $html);
  178. }
  179. $this->assertContains('url="states.txt"', $m[1]);
  180. }
  181. /**
  182. * @group ZF-5987
  183. * @group ZF-7266
  184. */
  185. public function testStoreCreationWhenUsingProgrammaticCreationShouldRegisterAsDojoJavascript()
  186. {
  187. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic(true);
  188. $html = $this->getElementAsRemoter();
  189. $js = $this->view->dojo()->getJavascript();
  190. $this->assertContains('var stateStore;', $js);
  191. $onLoad = $this->view->dojo()->_getZendLoadActions();
  192. $storeDeclarationFound = false;
  193. foreach ($onLoad as $statement) {
  194. if (strstr($statement, 'stateStore = new ')) {
  195. $storeDeclarationFound = true;
  196. break;
  197. }
  198. }
  199. $this->assertTrue($storeDeclarationFound, 'Store definition not found');
  200. }
  201. }
  202. // Call Zend_Dojo_View_Helper_ComboBoxTest::main() if this source file is executed directly.
  203. if (PHPUnit_MAIN_METHOD == "Zend_Dojo_View_Helper_ComboBoxTest::main") {
  204. Zend_Dojo_View_Helper_ComboBoxTest::main();
  205. }