PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Dojo/View/Helper/ComboBoxTest.php

https://github.com/mfairchild365/zf2
PHP | 190 lines | 133 code | 17 blank | 40 comment | 5 complexity | 52af7b315ce6c17132bd7eda0110b8fc MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. namespace ZendTest\Dojo\View\Helper;
  22. use Zend\Dojo\View\Helper\ComboBox as ComboBoxHelper,
  23. Zend\Dojo\View\Helper\Dojo as DojoHelper,
  24. Zend\Registry,
  25. Zend\View;
  26. /**
  27. * Test class for Zend_Dojo_View_Helper_ComboBox.
  28. *
  29. * @category Zend
  30. * @package Zend_Dojo
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Dojo
  35. * @group Zend_Dojo_View
  36. */
  37. class ComboBoxTest extends \PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Sets up the fixture, for example, open a network connection.
  41. * This method is called before a test is executed.
  42. *
  43. * @return void
  44. */
  45. public function setUp()
  46. {
  47. Registry::_unsetInstance();
  48. DojoHelper::setUseDeclarative();
  49. $this->view = $this->getView();
  50. $this->helper = new ComboBoxHelper();
  51. $this->helper->setView($this->view);
  52. }
  53. public function getView()
  54. {
  55. $view = new View\PhpRenderer();
  56. \Zend\Dojo\Dojo::enableView($view);
  57. return $view;
  58. }
  59. public function getElementAsSelect()
  60. {
  61. return $this->helper->direct(
  62. 'elementId',
  63. 'someCombo',
  64. array(),
  65. array(),
  66. array(
  67. 'red' => 'Rouge',
  68. 'blue' => 'Bleu',
  69. 'white' => 'Blanc',
  70. 'orange' => 'Orange',
  71. 'black' => 'Noir',
  72. 'green' => 'Vert',
  73. )
  74. );
  75. }
  76. public function getElementAsRemoter()
  77. {
  78. return $this->helper->direct(
  79. 'elementId',
  80. 'someCombo',
  81. array(
  82. 'store' => array(
  83. 'store' => 'stateStore',
  84. 'type' => 'dojo.data.ItemFileReadStore',
  85. 'params' => array(
  86. 'url' => 'states.txt'
  87. )
  88. ),
  89. 'searchAttr' => 'name'
  90. ),
  91. array()
  92. );
  93. }
  94. public function testShouldAllowDeclarativeDijitCreationAsSelect()
  95. {
  96. $html = $this->getElementAsSelect();
  97. $this->assertRegexp('/<select[^>]*(dojoType="dijit.form.ComboBox")/', $html, $html);
  98. }
  99. public function testShouldAllowProgrammaticDijitCreationAsSelect()
  100. {
  101. DojoHelper::setUseProgrammatic();
  102. $html = $this->getElementAsSelect();
  103. $this->assertNotRegexp('/<select[^>]*(dojoType="dijit.form.ComboBox")/', $html);
  104. $this->assertNotNull($this->view->plugin('dojo')->getDijit('elementId'));
  105. }
  106. public function testShouldAllowDeclarativeDijitCreationAsRemoter()
  107. {
  108. $html = $this->getElementAsRemoter();
  109. if (!preg_match('/(<input[^>]*(dojoType="dijit.form.ComboBox"))/', $html, $m)) {
  110. $this->fail('Did not create text input as remoter: ' . $html);
  111. }
  112. $this->assertContains('type="text"', $m[1]);
  113. }
  114. public function testShouldAllowProgrammaticDijitCreationAsRemoter()
  115. {
  116. DojoHelper::setUseProgrammatic();
  117. $html = $this->getElementAsRemoter();
  118. $this->assertNotRegexp('/<input[^>]*(dojoType="dijit.form.ComboBox")/', $html);
  119. $this->assertRegexp('/<input[^>]*(type="text")/', $html);
  120. $this->assertNotNull($this->view->plugin('dojo')->getDijit('elementId'));
  121. $found = false;
  122. $this->assertContains('var stateStore;', $this->view->plugin('dojo')->getJavascript());
  123. $scripts = $this->view->plugin('dojo')->_getZendLoadActions();
  124. foreach ($scripts as $js) {
  125. if (strstr($js, 'stateStore = new ')) {
  126. $found = true;
  127. break;
  128. }
  129. }
  130. $this->assertTrue($found, 'No store declaration found: ' . var_export($scripts, 1));
  131. }
  132. public function testShouldAllowAlternateNotationToSpecifyRemoter()
  133. {
  134. $html = $this->helper->direct(
  135. 'elementId',
  136. 'someCombo',
  137. array(
  138. 'store' => 'stateStore',
  139. 'storeType' => 'dojo.data.ItemFileReadStore',
  140. 'storeParams' => array('url' => 'states.txt'),
  141. 'searchAttr' => 'name',
  142. )
  143. );
  144. if (!preg_match('/(<input[^>]*(dojoType="dijit.form.ComboBox"))/', $html, $m)) {
  145. $this->fail('Did not create text input as remoter: ' . $html);
  146. }
  147. $this->assertContains('type="text"', $m[1]);
  148. if (!preg_match('/(<div[^>]*(?:dojoType="dojo.data.ItemFileReadStore")[^>]*>)/', $html, $m)) {
  149. $this->fail('Did not create data store: ' . $html);
  150. }
  151. $this->assertContains('url="states.txt"', $m[1]);
  152. }
  153. /**
  154. * @group ZF-5987
  155. * @group ZF-7266
  156. */
  157. public function testStoreCreationWhenUsingProgrammaticCreationShouldRegisterAsDojoJavascript()
  158. {
  159. DojoHelper::setUseProgrammatic(true);
  160. $html = $this->getElementAsRemoter();
  161. $js = $this->view->plugin('dojo')->getJavascript();
  162. $this->assertContains('var stateStore;', $js);
  163. $onLoad = $this->view->plugin('dojo')->_getZendLoadActions();
  164. $storeDeclarationFound = false;
  165. foreach ($onLoad as $statement) {
  166. if (strstr($statement, 'stateStore = new ')) {
  167. $storeDeclarationFound = true;
  168. break;
  169. }
  170. }
  171. $this->assertTrue($storeDeclarationFound, 'Store definition not found');
  172. }
  173. }