/standard/branches/release-1.5/tests/Zend/View/Helper/FormSelectTest.php

https://github.com/bhaumik25/zend-framework · PHP · 292 lines · 226 code · 22 blank · 44 comment · 3 complexity · f3163d15a7c6743ab634e862df2b6f98 MD5 · raw file

  1. <?php
  2. // Call Zend_View_Helper_FormSelectTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormSelectTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/View/Helper/FormSelect.php';
  8. require_once 'Zend/View.php';
  9. /**
  10. * Test class for Zend_View_Helper_FormSelect.
  11. */
  12. class Zend_View_Helper_FormSelectTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Runs the test methods of this class.
  16. *
  17. * @return void
  18. */
  19. public static function main()
  20. {
  21. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormSelectTest");
  22. $result = PHPUnit_TextUI_TestRunner::run($suite);
  23. }
  24. /**
  25. * Sets up the fixture, for example, open a network connection.
  26. * This method is called before a test is executed.
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. $this->view = new Zend_View();
  33. $this->helper = new Zend_View_Helper_FormSelect();
  34. $this->helper->setView($this->view);
  35. }
  36. /**
  37. * Tears down the fixture, for example, close a network connection.
  38. * This method is called after a test is executed.
  39. *
  40. * @return void
  41. */
  42. public function tearDown()
  43. {
  44. unset($this->helper, $this->view);
  45. }
  46. public function testFormSelectWithNameOnlyCreatesEmptySelect()
  47. {
  48. $html = $this->helper->formSelect('foo');
  49. $this->assertRegExp('#<select[^>]+name="foo"#', $html);
  50. $this->assertContains('</select>', $html);
  51. $this->assertNotContains('<option', $html);
  52. }
  53. public function testFormSelectWithOptionsCreatesPopulatedSelect()
  54. {
  55. $html = $this->helper->formSelect('foo', null, null, array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  56. $this->assertRegExp('#<select[^>]+name="foo"#', $html);
  57. $this->assertContains('</select>', $html);
  58. $this->assertRegExp('#<option[^>]+value="foo".*?>Foobar</option>#', $html);
  59. $this->assertRegExp('#<option[^>]+value="baz".*?>Bazbat</option>#', $html);
  60. $this->assertEquals(2, substr_count($html, '<option'));
  61. }
  62. public function testFormSelectWithOptionsAndValueSelectsAppropriateValue()
  63. {
  64. $html = $this->helper->formSelect('foo', 'baz', null, array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  65. $this->assertRegExp('#<option[^>]+value="baz"[^>]*selected.*?>Bazbat</option>#', $html);
  66. }
  67. public function testFormSelectWithMultipleAttributeCreatesMultiSelect()
  68. {
  69. $html = $this->helper->formSelect('foo', null, array('multiple' => true), array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  70. $this->assertRegExp('#<select[^>]+name="foo\[\]"#', $html);
  71. $this->assertRegExp('#<select[^>]+multiple="multiple"#', $html);
  72. }
  73. public function testFormSelectWithMultipleAttributeAndValuesCreatesMultiSelectWithSelectedValues()
  74. {
  75. $html = $this->helper->formSelect('foo', array('foo', 'baz'), array('multiple' => true), array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  76. $this->assertRegExp('#<option[^>]+value="foo"[^>]*selected.*?>Foobar</option>#', $html);
  77. $this->assertRegExp('#<option[^>]+value="baz"[^>]*selected.*?>Bazbat</option>#', $html);
  78. }
  79. /**
  80. * ZF-1930
  81. * @return void
  82. */
  83. public function testFormSelectWithZeroValueSelectsValue()
  84. {
  85. $html = $this->helper->formSelect('foo', 0, null, array('foo' => 'Foobar', 0 => 'Bazbat'));
  86. $this->assertRegExp('#<option[^>]+value="0"[^>]*selected.*?>Bazbat</option>#', $html);
  87. }
  88. /**
  89. * ZF-2513
  90. */
  91. public function testCanDisableEntireSelect()
  92. {
  93. $html = $this->helper->formSelect(array(
  94. 'name' => 'baz',
  95. 'options' => array(
  96. 'foo' => 'Foo',
  97. 'bar' => 'Bar'
  98. ),
  99. 'attribs' => array(
  100. 'disable' => true
  101. ),
  102. ));
  103. $this->assertRegexp('/<select[^>]*?disabled/', $html, $html);
  104. $this->assertNotRegexp('/<option[^>]*?disabled="disabled"/', $html, $html);
  105. }
  106. /**
  107. * ZF-2513
  108. */
  109. public function testCanDisableIndividualSelectOptionsOnly()
  110. {
  111. $html = $this->helper->formSelect(array(
  112. 'name' => 'baz',
  113. 'options' => array(
  114. 'foo' => 'Foo',
  115. 'bar' => 'Bar'
  116. ),
  117. 'attribs' => array(
  118. 'disable' => array('bar')
  119. ),
  120. ));
  121. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  122. $this->assertRegexp('/<option value="bar"[^>]*?disabled="disabled"/', $html, $html);
  123. $html = $this->helper->formSelect(
  124. 'baz',
  125. 'foo',
  126. array(
  127. 'disable' => array('bar')
  128. ),
  129. array(
  130. 'foo' => 'Foo',
  131. 'bar' => 'Bar'
  132. )
  133. );
  134. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  135. $this->assertRegexp('/<option value="bar"[^>]*?disabled="disabled"/', $html, $html);
  136. }
  137. /**
  138. * ZF-2513
  139. */
  140. public function testCanDisableMultipleSelectOptions()
  141. {
  142. $html = $this->helper->formSelect(array(
  143. 'name' => 'baz',
  144. 'options' => array(
  145. 'foo' => 'Foo',
  146. 'bar' => 'Bar',
  147. 'baz' => 'Baz,'
  148. ),
  149. 'attribs' => array(
  150. 'disable' => array('foo', 'baz')
  151. ),
  152. ));
  153. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  154. $this->assertRegexp('/<option value="foo"[^>]*?disabled="disabled"/', $html, $html);
  155. $this->assertRegexp('/<option value="baz"[^>]*?disabled="disabled"/', $html, $html);
  156. }
  157. /**
  158. * ZF-2513
  159. */
  160. public function testCanDisableOptGroups()
  161. {
  162. $html = $this->helper->formSelect(array(
  163. 'name' => 'baz',
  164. 'options' => array(
  165. 'foo' => 'Foo',
  166. 'bar' => array(
  167. '1' => 'one',
  168. '2' => 'two'
  169. ),
  170. 'baz' => 'Baz,'
  171. ),
  172. 'attribs' => array(
  173. 'disable' => array('bar')
  174. ),
  175. ));
  176. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  177. $this->assertRegexp('/<optgroup[^>]*?disabled="disabled"[^>]*?"bar"[^>]*?/', $html, $html);
  178. $this->assertNotRegexp('/<option value="1"[^>]*?disabled="disabled"/', $html, $html);
  179. $this->assertNotRegexp('/<option value="2"[^>]*?disabled="disabled"/', $html, $html);
  180. }
  181. /**
  182. * ZF-2513
  183. */
  184. public function testCanDisableOptGroupOptions()
  185. {
  186. $html = $this->helper->formSelect(array(
  187. 'name' => 'baz',
  188. 'options' => array(
  189. 'foo' => 'Foo',
  190. 'bar' => array(
  191. '1' => 'one',
  192. '2' => 'two'
  193. ),
  194. 'baz' => 'Baz,'
  195. ),
  196. 'attribs' => array(
  197. 'disable' => array('2')
  198. ),
  199. ));
  200. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  201. $this->assertNotRegexp('/<optgroup[^>]*?disabled="disabled"[^>]*?"bar"[^>]*?/', $html, $html);
  202. $this->assertNotRegexp('/<option value="1"[^>]*?disabled="disabled"/', $html, $html);
  203. $this->assertRegexp('/<option value="2"[^>]*?disabled="disabled"/', $html, $html);
  204. }
  205. public function testCanSpecifySelectMultipleThroughAttribute()
  206. {
  207. $html = $this->helper->formSelect(array(
  208. 'name' => 'baz',
  209. 'options' => array(
  210. 'foo' => 'Foo',
  211. 'bar' => 'Bar',
  212. 'baz' => 'Baz,'
  213. ),
  214. 'attribs' => array(
  215. 'multiple' => true
  216. ),
  217. ));
  218. $this->assertRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
  219. }
  220. public function testSpecifyingSelectMultipleThroughAttributeAppendsNameWithBrackets()
  221. {
  222. $html = $this->helper->formSelect(array(
  223. 'name' => 'baz',
  224. 'options' => array(
  225. 'foo' => 'Foo',
  226. 'bar' => 'Bar',
  227. 'baz' => 'Baz,'
  228. ),
  229. 'attribs' => array(
  230. 'multiple' => true
  231. ),
  232. ));
  233. $this->assertRegexp('/<select[^>]*?(name="baz\[\]")/', $html, $html);
  234. }
  235. public function testCanSpecifySelectMultipleThroughName()
  236. {
  237. $html = $this->helper->formSelect(array(
  238. 'name' => 'baz[]',
  239. 'options' => array(
  240. 'foo' => 'Foo',
  241. 'bar' => 'Bar',
  242. 'baz' => 'Baz,'
  243. ),
  244. ));
  245. $this->assertRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
  246. }
  247. /**
  248. * ZF-1639
  249. */
  250. public function testNameCanContainBracketsButNotBeMultiple()
  251. {
  252. $html = $this->helper->formSelect(array(
  253. 'name' => 'baz[]',
  254. 'options' => array(
  255. 'foo' => 'Foo',
  256. 'bar' => 'Bar',
  257. 'baz' => 'Baz,'
  258. ),
  259. 'attribs' => array(
  260. 'multiple' => false
  261. ),
  262. ));
  263. $this->assertRegexp('/<select[^>]*?(name="baz\[\]")/', $html, $html);
  264. $this->assertNotRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
  265. }
  266. }
  267. // Call Zend_View_Helper_FormSelectTest::main() if this source file is executed directly.
  268. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormSelectTest::main") {
  269. Zend_View_Helper_FormSelectTest::main();
  270. }