/tests/ZendTest/Form/Element/SelectTest.php

https://github.com/cgmartin/zf2 · PHP · 210 lines · 169 code · 25 blank · 16 comment · 1 complexity · bad662f197ca7d8b7511230a6a1d7882 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Form\Element;
  10. use PHPUnit_Framework_TestCase as TestCase;
  11. use Zend\Form\Element\Select as SelectElement;
  12. class SelectTest extends TestCase
  13. {
  14. public function testProvidesInputSpecificationForSingleSelect()
  15. {
  16. $element = new SelectElement();
  17. $element->setValueOptions(array(
  18. 'Option 1' => 'option1',
  19. 'Option 2' => 'option2',
  20. 'Option 3' => 'option3',
  21. ));
  22. $inputSpec = $element->getInputSpecification();
  23. $this->assertArrayHasKey('validators', $inputSpec);
  24. $this->assertInternalType('array', $inputSpec['validators']);
  25. $expectedClasses = array(
  26. 'Zend\Validator\InArray'
  27. );
  28. foreach ($inputSpec['validators'] as $validator) {
  29. $class = get_class($validator);
  30. $this->assertTrue(in_array($class, $expectedClasses), $class);
  31. }
  32. }
  33. public function testValidateWorksForNestedSelectElementWithSimpleNaming()
  34. {
  35. $element = new SelectElement();
  36. $element->setValueOptions(array(
  37. array('label' => 'group 1', 'options' => array(
  38. 'Option 1' => 'Label 1',
  39. 'Option 2' => 'Label 2',
  40. 'Option 3' => 'Label 2',
  41. ))));
  42. $inputSpec = $element->getInputSpecification();
  43. $inArrayValidator = $inputSpec['validators'][0];
  44. $this->assertTrue($inArrayValidator->isValid('Option 1'));
  45. $this->assertFalse($inArrayValidator->isValid('Option 5'));
  46. }
  47. public function testValidateWorksForNestedSelectElementWithExplicitNaming()
  48. {
  49. $element = new SelectElement();
  50. $element->setValueOptions(array(
  51. array('label' => 'group 1', 'options' => array(
  52. array('value' => 'Option 1', 'label'=> 'Label 1'),
  53. array('value' => 'Option 2', 'label'=> 'Label 2'),
  54. array('value' => 'Option 3', 'label'=> 'Label 3'),
  55. ))));
  56. $inputSpec = $element->getInputSpecification();
  57. $inArrayValidator = $inputSpec['validators'][0];
  58. $this->assertTrue($inArrayValidator->isValid('Option 1'));
  59. $this->assertTrue($inArrayValidator->isValid('Option 2'));
  60. $this->assertTrue($inArrayValidator->isValid('Option 3'));
  61. $this->assertFalse($inArrayValidator->isValid('Option 5'));
  62. }
  63. public function testProvidesInputSpecificationForMultipleSelect()
  64. {
  65. $element = new SelectElement();
  66. $element->setAttributes(array(
  67. 'multiple' => true,
  68. ));
  69. $element->setValueOptions(array(
  70. 'Option 1' => 'option1',
  71. 'Option 2' => 'option2',
  72. 'Option 3' => 'option3',
  73. ));
  74. $inputSpec = $element->getInputSpecification();
  75. $this->assertArrayHasKey('validators', $inputSpec);
  76. $this->assertInternalType('array', $inputSpec['validators']);
  77. $expectedClasses = array(
  78. 'Zend\Validator\Explode'
  79. );
  80. foreach ($inputSpec['validators'] as $validator) {
  81. $class = get_class($validator);
  82. $this->assertTrue(in_array($class, $expectedClasses), $class);
  83. switch ($class) {
  84. case 'Zend\Validator\Explode':
  85. $this->assertInstanceOf('Zend\Validator\InArray', $validator->getValidator());
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. }
  92. public function selectOptionsDataProvider()
  93. {
  94. return array(
  95. array(
  96. array('foo', 'bar'),
  97. array(
  98. 'foo' => 'My Foo Label',
  99. 'bar' => 'My Bar Label',
  100. )
  101. ),
  102. array(
  103. array('foo', 'bar'),
  104. array(
  105. 0 => array('label' => 'My Foo Label', 'value' => 'foo'),
  106. 1 => array('label' => 'My Bar Label', 'value' => 'bar'),
  107. )
  108. ),
  109. );
  110. }
  111. /**
  112. * @dataProvider selectOptionsDataProvider
  113. */
  114. public function testInArrayValidationOfOptions($valueTests, $options)
  115. {
  116. $element = new SelectElement('my-select');
  117. $element->setValueOptions($options);
  118. $inputSpec = $element->getInputSpecification();
  119. $this->assertArrayHasKey('validators', $inputSpec);
  120. $inArrayValidator = $inputSpec['validators'][0];
  121. $this->assertInstanceOf('Zend\Validator\InArray', $inArrayValidator);
  122. foreach ($valueTests as $valueToTest) {
  123. $this->assertTrue($inArrayValidator->isValid($valueToTest));
  124. }
  125. }
  126. /**
  127. * Testing that InArray Validator Haystack is Updated if the Options
  128. * are added after the validator is attached
  129. *
  130. * @dataProvider selectOptionsDataProvider
  131. */
  132. public function testInArrayValidatorHaystakIsUpdated($valueTests, $options)
  133. {
  134. $element = new SelectElement('my-select');
  135. $inputSpec = $element->getInputSpecification();
  136. $inArrayValidator = $inputSpec['validators'][0];
  137. $this->assertInstanceOf('Zend\Validator\InArray', $inArrayValidator);
  138. $element->setValueOptions($options);
  139. $haystack=$inArrayValidator->getHaystack();
  140. $this->assertCount(count($options), $haystack);
  141. }
  142. public function testOptionsHasArrayOnConstruct()
  143. {
  144. $element = new SelectElement();
  145. $this->assertTrue(is_array($element->getValueOptions()));
  146. }
  147. public function testDeprecateOptionsInAttributes()
  148. {
  149. $element = new SelectElement();
  150. $valueOptions = array(
  151. 'Option 1' => 'option1',
  152. 'Option 2' => 'option2',
  153. 'Option 3' => 'option3',
  154. );
  155. $element->setAttributes(array(
  156. 'multiple' => true,
  157. 'options' => $valueOptions,
  158. ));
  159. $this->assertEquals($valueOptions, $element->getValueOptions());
  160. }
  161. public function testSetOptionsOptions()
  162. {
  163. $element = new SelectElement();
  164. $element->setOptions(array(
  165. 'value_options' => array('bar' => 'baz'),
  166. 'options' => array('foo' => 'bar'),
  167. 'empty_option' => array('baz' => 'foo'),
  168. ));
  169. $this->assertEquals(array('bar' => 'baz'), $element->getOption('value_options'));
  170. $this->assertEquals(array('foo' => 'bar'), $element->getOption('options'));
  171. $this->assertEquals(array('baz' => 'foo'), $element->getOption('empty_option'));
  172. }
  173. public function testDisableInputSpecification()
  174. {
  175. $element = new SelectElement();
  176. $element->setValueOptions(array(
  177. 'Option 1' => 'option1',
  178. 'Option 2' => 'option2',
  179. 'Option 3' => 'option3',
  180. ));
  181. $element->setDisableInArrayValidator(true);
  182. $inputSpec = $element->getInputSpecification();
  183. $this->assertArrayNotHasKey('validators', $inputSpec);
  184. }
  185. }