2
0

ComboBoxTest.php 7.0 KB

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