This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
SelectTest.php
272 lines (239 loc) · 9.19 KB
/
SelectTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Form\Element;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Form\Element\Select as SelectElement;
class SelectTest extends TestCase
{
public function testProvidesInputSpecificationForSingleSelect()
{
$element = new SelectElement();
$element->setValueOptions(array(
'Option 1' => 'option1',
'Option 2' => 'option2',
'Option 3' => 'option3',
));
$inputSpec = $element->getInputSpecification();
$this->assertArrayHasKey('validators', $inputSpec);
$this->assertInternalType('array', $inputSpec['validators']);
$expectedClasses = array(
'Zend\Validator\InArray'
);
foreach ($inputSpec['validators'] as $validator) {
$class = get_class($validator);
$this->assertTrue(in_array($class, $expectedClasses), $class);
}
}
public function testValidateWorksForNestedSelectElementWithSimpleNaming()
{
$element = new SelectElement();
$element->setValueOptions(array(
array('label' => 'group 1', 'options' => array(
'Option 1' => 'Label 1',
'Option 2' => 'Label 2',
'Option 3' => 'Label 2',
))));
$inputSpec = $element->getInputSpecification();
$inArrayValidator = $inputSpec['validators'][0];
$this->assertTrue($inArrayValidator->isValid('Option 1'));
$this->assertFalse($inArrayValidator->isValid('Option 5'));
}
public function testValidateWorksForNestedSelectElementWithExplicitNaming()
{
$element = new SelectElement();
$element->setValueOptions(array(
array('label' => 'group 1', 'options' => array(
array('value' => 'Option 1', 'label'=> 'Label 1'),
array('value' => 'Option 2', 'label'=> 'Label 2'),
array('value' => 'Option 3', 'label'=> 'Label 3'),
))));
$inputSpec = $element->getInputSpecification();
$inArrayValidator = $inputSpec['validators'][0];
$this->assertTrue($inArrayValidator->isValid('Option 1'));
$this->assertTrue($inArrayValidator->isValid('Option 2'));
$this->assertTrue($inArrayValidator->isValid('Option 3'));
$this->assertFalse($inArrayValidator->isValid('Option 5'));
}
public function testProvidesInputSpecificationForMultipleSelect()
{
$element = new SelectElement();
$element->setAttributes(array(
'multiple' => true,
));
$element->setValueOptions(array(
'Option 1' => 'option1',
'Option 2' => 'option2',
'Option 3' => 'option3',
));
$inputSpec = $element->getInputSpecification();
$this->assertArrayHasKey('validators', $inputSpec);
$this->assertInternalType('array', $inputSpec['validators']);
$expectedClasses = array(
'Zend\Validator\Explode'
);
foreach ($inputSpec['validators'] as $validator) {
$class = get_class($validator);
$this->assertTrue(in_array($class, $expectedClasses), $class);
switch ($class) {
case 'Zend\Validator\Explode':
$this->assertInstanceOf('Zend\Validator\InArray', $validator->getValidator());
break;
default:
break;
}
}
}
public function selectOptionsDataProvider()
{
return array(
array(
array('foo', 'bar'),
array(
'foo' => 'My Foo Label',
'bar' => 'My Bar Label',
)
),
array(
array('foo', 'bar'),
array(
0 => array('label' => 'My Foo Label', 'value' => 'foo'),
1 => array('label' => 'My Bar Label', 'value' => 'bar'),
)
),
);
}
/**
* @dataProvider selectOptionsDataProvider
*/
public function testInArrayValidationOfOptions($valueTests, $options)
{
$element = new SelectElement('my-select');
$element->setValueOptions($options);
$inputSpec = $element->getInputSpecification();
$this->assertArrayHasKey('validators', $inputSpec);
$inArrayValidator = $inputSpec['validators'][0];
$this->assertInstanceOf('Zend\Validator\InArray', $inArrayValidator);
foreach ($valueTests as $valueToTest) {
$this->assertTrue($inArrayValidator->isValid($valueToTest));
}
}
/**
* Testing that InArray Validator Haystack is Updated if the Options
* are added after the validator is attached
*
* @dataProvider selectOptionsDataProvider
*/
public function testInArrayValidatorHaystakIsUpdated($valueTests, $options)
{
$element = new SelectElement('my-select');
$inputSpec = $element->getInputSpecification();
$inArrayValidator = $inputSpec['validators'][0];
$this->assertInstanceOf('Zend\Validator\InArray', $inArrayValidator);
$element->setValueOptions($options);
$haystack=$inArrayValidator->getHaystack();
$this->assertCount(count($options), $haystack);
}
public function testOptionsHasArrayOnConstruct()
{
$element = new SelectElement();
$this->assertTrue(is_array($element->getValueOptions()));
}
public function testDeprecateOptionsInAttributes()
{
$element = new SelectElement();
$valueOptions = array(
'Option 1' => 'option1',
'Option 2' => 'option2',
'Option 3' => 'option3',
);
$element->setAttributes(array(
'multiple' => true,
'options' => $valueOptions,
));
$this->assertEquals($valueOptions, $element->getValueOptions());
}
public function testSetOptionsOptions()
{
$element = new SelectElement();
$element->setOptions(array(
'value_options' => array('bar' => 'baz'),
'options' => array('foo' => 'bar'),
'empty_option' => array('baz' => 'foo'),
));
$this->assertEquals(array('bar' => 'baz'), $element->getOption('value_options'));
$this->assertEquals(array('foo' => 'bar'), $element->getOption('options'));
$this->assertEquals(array('baz' => 'foo'), $element->getOption('empty_option'));
}
public function testDisableInputSpecification()
{
$element = new SelectElement();
$element->setValueOptions(array(
'Option 1' => 'option1',
'Option 2' => 'option2',
'Option 3' => 'option3',
));
$element->setDisableInArrayValidator(true);
$inputSpec = $element->getInputSpecification();
$this->assertArrayNotHasKey('validators', $inputSpec);
}
public function testUnsetValueOption()
{
$element = new SelectElement();
$element->setValueOptions(array(
'Option 1' => 'option1',
'Option 2' => 'option2',
'Option 3' => 'option3',
));
$element->unsetValueOption('Option 2');
$valueOptions = $element->getValueOptions();
$this->assertArrayNotHasKey('Option 2', $valueOptions);
}
public function testUnsetUndefinedValueOption()
{
$element = new SelectElement();
$element->setValueOptions(array(
'Option 1' => 'option1',
'Option 2' => 'option2',
'Option 3' => 'option3',
));
$element->unsetValueOption('Option Undefined');
$valueOptions = $element->getValueOptions();
$this->assertArrayNotHasKey('Option Undefined', $valueOptions);
}
public function testSetOptionsToSelectMultiple()
{
$element = new SelectElement(null, array(
'label' => 'Importance',
'use_hidden_element' => true,
'unselected_value' => 'empty',
'value_options' => array(
'foo' => 'Foo',
'bar' => 'Bar'
),
));
$element->setAttributes(array('multiple' => 'multiple'));
$this->assertTrue($element->isMultiple());
$this->assertTrue($element->useHiddenElement());
$this->assertEquals('empty', $element->getUnselectedValue());
}
public function testProvidesInputSpecificationForMultipleSelectWithUseHiddenElement()
{
$element = new SelectElement();
$element
->setUseHiddenElement(true)
->setAttributes(array(
'multiple' => true,
));
$inputSpec = $element->getInputSpecification();
$this->assertArrayHasKey('allow_empty', $inputSpec);
$this->assertTrue($inputSpec['allow_empty']);
$this->assertArrayHasKey('continue_if_empty', $inputSpec);
$this->assertTrue($inputSpec['continue_if_empty']);
}
}