PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/View/Helper/FormSelectTest.php

https://github.com/Exercise/zf2
PHP | 302 lines | 213 code | 21 blank | 68 comment | 0 complexity | 8732f42f4f69adaf08b9035eec4ba65e 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_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 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. /**
  23. * @namespace
  24. */
  25. namespace ZendTest\View\Helper;
  26. /**
  27. * Test class for Zend_View_Helper_FormSelect.
  28. *
  29. * @category Zend
  30. * @package Zend_View
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_View
  35. * @group Zend_View_Helper
  36. */
  37. class FormSelectTest 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. $this->view = new \Zend\View\View();
  48. $this->helper = new \Zend\View\Helper\FormSelect();
  49. $this->helper->setView($this->view);
  50. }
  51. /**
  52. * Tears down the fixture, for example, close a network connection.
  53. * This method is called after a test is executed.
  54. *
  55. * @return void
  56. */
  57. public function tearDown()
  58. {
  59. unset($this->helper, $this->view);
  60. }
  61. public function testFormSelectWithNameOnlyCreatesEmptySelect()
  62. {
  63. $html = $this->helper->direct('foo');
  64. $this->assertRegExp('#<select[^>]+name="foo"#', $html);
  65. $this->assertContains('</select>', $html);
  66. $this->assertNotContains('<option', $html);
  67. }
  68. public function testFormSelectWithOptionsCreatesPopulatedSelect()
  69. {
  70. $html = $this->helper->direct('foo', null, null, array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  71. $this->assertRegExp('#<select[^>]+name="foo"#', $html);
  72. $this->assertContains('</select>', $html);
  73. $this->assertRegExp('#<option[^>]+value="foo".*?>Foobar</option>#', $html);
  74. $this->assertRegExp('#<option[^>]+value="baz".*?>Bazbat</option>#', $html);
  75. $this->assertEquals(2, substr_count($html, '<option'));
  76. }
  77. public function testFormSelectWithOptionsAndValueSelectsAppropriateValue()
  78. {
  79. $html = $this->helper->direct('foo', 'baz', null, array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  80. $this->assertRegExp('#<option[^>]+value="baz"[^>]*selected.*?>Bazbat</option>#', $html);
  81. }
  82. public function testFormSelectWithMultipleAttributeCreatesMultiSelect()
  83. {
  84. $html = $this->helper->direct('foo', null, array('multiple' => true), array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  85. $this->assertRegExp('#<select[^>]+name="foo\[\]"#', $html);
  86. $this->assertRegExp('#<select[^>]+multiple="multiple"#', $html);
  87. }
  88. public function testFormSelectWithMultipleAttributeAndValuesCreatesMultiSelectWithSelectedValues()
  89. {
  90. $html = $this->helper->direct('foo', array('foo', 'baz'), array('multiple' => true), array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  91. $this->assertRegExp('#<option[^>]+value="foo"[^>]*selected.*?>Foobar</option>#', $html);
  92. $this->assertRegExp('#<option[^>]+value="baz"[^>]*selected.*?>Bazbat</option>#', $html);
  93. }
  94. /**
  95. * ZF-1930
  96. * @return void
  97. */
  98. public function testFormSelectWithZeroValueSelectsValue()
  99. {
  100. $html = $this->helper->direct('foo', 0, null, array('foo' => 'Foobar', 0 => 'Bazbat'));
  101. $this->assertRegExp('#<option[^>]+value="0"[^>]*selected.*?>Bazbat</option>#', $html);
  102. }
  103. /**
  104. * ZF-2513
  105. */
  106. public function testCanDisableEntireSelect()
  107. {
  108. $html = $this->helper->direct(array(
  109. 'name' => 'baz',
  110. 'options' => array(
  111. 'foo' => 'Foo',
  112. 'bar' => 'Bar'
  113. ),
  114. 'attribs' => array(
  115. 'disable' => true
  116. ),
  117. ));
  118. $this->assertRegexp('/<select[^>]*?disabled/', $html, $html);
  119. $this->assertNotRegexp('/<option[^>]*?disabled="disabled"/', $html, $html);
  120. }
  121. /**
  122. * ZF-2513
  123. */
  124. public function testCanDisableIndividualSelectOptionsOnly()
  125. {
  126. $html = $this->helper->direct(array(
  127. 'name' => 'baz',
  128. 'options' => array(
  129. 'foo' => 'Foo',
  130. 'bar' => 'Bar'
  131. ),
  132. 'attribs' => array(
  133. 'disable' => array('bar')
  134. ),
  135. ));
  136. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  137. $this->assertRegexp('/<option value="bar"[^>]*?disabled="disabled"/', $html, $html);
  138. $html = $this->helper->direct(
  139. 'baz',
  140. 'foo',
  141. array(
  142. 'disable' => array('bar')
  143. ),
  144. array(
  145. 'foo' => 'Foo',
  146. 'bar' => 'Bar'
  147. )
  148. );
  149. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  150. $this->assertRegexp('/<option value="bar"[^>]*?disabled="disabled"/', $html, $html);
  151. }
  152. /**
  153. * ZF-2513
  154. */
  155. public function testCanDisableMultipleSelectOptions()
  156. {
  157. $html = $this->helper->direct(array(
  158. 'name' => 'baz',
  159. 'options' => array(
  160. 'foo' => 'Foo',
  161. 'bar' => 'Bar',
  162. 'baz' => 'Baz,'
  163. ),
  164. 'attribs' => array(
  165. 'disable' => array('foo', 'baz')
  166. ),
  167. ));
  168. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  169. $this->assertRegexp('/<option value="foo"[^>]*?disabled="disabled"/', $html, $html);
  170. $this->assertRegexp('/<option value="baz"[^>]*?disabled="disabled"/', $html, $html);
  171. }
  172. /**
  173. * ZF-2513
  174. */
  175. public function testCanDisableOptGroups()
  176. {
  177. $html = $this->helper->direct(array(
  178. 'name' => 'baz',
  179. 'options' => array(
  180. 'foo' => 'Foo',
  181. 'bar' => array(
  182. '1' => 'one',
  183. '2' => 'two'
  184. ),
  185. 'baz' => 'Baz,'
  186. ),
  187. 'attribs' => array(
  188. 'disable' => array('bar')
  189. ),
  190. ));
  191. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  192. $this->assertRegexp('/<optgroup[^>]*?disabled="disabled"[^>]*?"bar"[^>]*?/', $html, $html);
  193. $this->assertNotRegexp('/<option value="1"[^>]*?disabled="disabled"/', $html, $html);
  194. $this->assertNotRegexp('/<option value="2"[^>]*?disabled="disabled"/', $html, $html);
  195. }
  196. /**
  197. * ZF-2513
  198. */
  199. public function testCanDisableOptGroupOptions()
  200. {
  201. $html = $this->helper->direct(array(
  202. 'name' => 'baz',
  203. 'options' => array(
  204. 'foo' => 'Foo',
  205. 'bar' => array(
  206. '1' => 'one',
  207. '2' => 'two'
  208. ),
  209. 'baz' => 'Baz,'
  210. ),
  211. 'attribs' => array(
  212. 'disable' => array('2')
  213. ),
  214. ));
  215. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  216. $this->assertNotRegexp('/<optgroup[^>]*?disabled="disabled"[^>]*?"bar"[^>]*?/', $html, $html);
  217. $this->assertNotRegexp('/<option value="1"[^>]*?disabled="disabled"/', $html, $html);
  218. $this->assertRegexp('/<option value="2"[^>]*?disabled="disabled"/', $html, $html);
  219. }
  220. public function testCanSpecifySelectMultipleThroughAttribute()
  221. {
  222. $html = $this->helper->direct(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[^>]*?(multiple="multiple")/', $html, $html);
  234. }
  235. public function testSpecifyingSelectMultipleThroughAttributeAppendsNameWithBrackets()
  236. {
  237. $html = $this->helper->direct(array(
  238. 'name' => 'baz',
  239. 'options' => array(
  240. 'foo' => 'Foo',
  241. 'bar' => 'Bar',
  242. 'baz' => 'Baz,'
  243. ),
  244. 'attribs' => array(
  245. 'multiple' => true
  246. ),
  247. ));
  248. $this->assertRegexp('/<select[^>]*?(name="baz\[\]")/', $html, $html);
  249. }
  250. public function testCanSpecifySelectMultipleThroughName()
  251. {
  252. $html = $this->helper->direct(array(
  253. 'name' => 'baz[]',
  254. 'options' => array(
  255. 'foo' => 'Foo',
  256. 'bar' => 'Bar',
  257. 'baz' => 'Baz,'
  258. ),
  259. ));
  260. $this->assertRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
  261. }
  262. /**
  263. * ZF-1639
  264. */
  265. public function testNameCanContainBracketsButNotBeMultiple()
  266. {
  267. $html = $this->helper->direct(array(
  268. 'name' => 'baz[]',
  269. 'options' => array(
  270. 'foo' => 'Foo',
  271. 'bar' => 'Bar',
  272. 'baz' => 'Baz,'
  273. ),
  274. 'attribs' => array(
  275. 'multiple' => false
  276. ),
  277. ));
  278. $this->assertRegexp('/<select[^>]*?(name="baz\[\]")/', $html, $html);
  279. $this->assertNotRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
  280. }
  281. }