1 | <?php |
||||
2 | |||||
3 | /* |
||||
4 | * @copyright 2017 Mautic Contributors. All rights reserved |
||||
5 | * @author Mautic, Inc. |
||||
6 | * |
||||
7 | * @link https://mautic.org |
||||
8 | * |
||||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||||
10 | */ |
||||
11 | |||||
12 | namespace Mautic\CoreBundle\Tests\Unit\Helper; |
||||
13 | |||||
14 | use Mautic\CoreBundle\Helper\AbstractFormFieldHelper; |
||||
15 | |||||
16 | class AbstractFormFieldHelperTest extends \PHPUnit\Framework\TestCase |
||||
17 | { |
||||
18 | /** |
||||
19 | * @testdox The string is parsed correctly into a choice array |
||||
20 | * |
||||
21 | * @covers \Mautic\CoreBundle\Helper\AbstractFormFieldHelper::parseList |
||||
22 | * @covers \Mautic\CoreBundle\Helper\ListParser\BarListParser::parse |
||||
23 | */ |
||||
24 | public function testBarFormatConvertedToArray() |
||||
25 | { |
||||
26 | $string = 'value1|value2|value3'; |
||||
27 | $expected = [ |
||||
28 | 'value1' => 'value1', |
||||
29 | 'value2' => 'value2', |
||||
30 | 'value3' => 'value3', |
||||
31 | ]; |
||||
32 | $actual = AbstractFormFieldHelper::parseList($string); |
||||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||||
33 | |||||
34 | $this->assertEquals($expected, $actual); |
||||
35 | } |
||||
36 | |||||
37 | /** |
||||
38 | * @testdox The string is parsed correctly into a choice array |
||||
39 | * |
||||
40 | * @covers \Mautic\CoreBundle\Helper\AbstractFormFieldHelper::parseList |
||||
41 | * @covers \Mautic\CoreBundle\Helper\ListParser\BarListParser::parse |
||||
42 | */ |
||||
43 | public function testBarLabelValueFormatConvertedToArray() |
||||
44 | { |
||||
45 | $string = 'label1|label2|label3||value1|value2|value3'; |
||||
46 | $expected = [ |
||||
47 | 'value1' => 'label1', |
||||
48 | 'value2' => 'label2', |
||||
49 | 'value3' => 'label3', |
||||
50 | ]; |
||||
51 | $actual = AbstractFormFieldHelper::parseList($string); |
||||
0 ignored issues
–
show
The function
Mautic\CoreBundle\Helper...ieldHelper::parseList() has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
52 | |||||
53 | $this->assertEquals($expected, $actual); |
||||
54 | } |
||||
55 | |||||
56 | /** |
||||
57 | * @testdox The string is parsed correctly into a choice array |
||||
58 | * |
||||
59 | * @covers \Mautic\CoreBundle\Helper\AbstractFormFieldHelper::parseList |
||||
60 | * @covers \Mautic\CoreBundle\Helper\ListParser\JsonListParser::parse |
||||
61 | */ |
||||
62 | public function testJsonEncodedFormatConvertedToArray() |
||||
63 | { |
||||
64 | $string = '{"value1":"label1","value2":"label2","value3":"label3"}'; |
||||
65 | $expected = [ |
||||
66 | 'value1' => 'label1', |
||||
67 | 'value2' => 'label2', |
||||
68 | 'value3' => 'label3', |
||||
69 | ]; |
||||
70 | $actual = AbstractFormFieldHelper::parseList($string); |
||||
0 ignored issues
–
show
The function
Mautic\CoreBundle\Helper...ieldHelper::parseList() has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
71 | |||||
72 | $this->assertEquals($expected, $actual); |
||||
73 | } |
||||
74 | |||||
75 | /** |
||||
76 | * @testdox The string is parsed correctly into a choice array |
||||
77 | * |
||||
78 | * @covers \Mautic\CoreBundle\Helper\AbstractFormFieldHelper::parseList |
||||
79 | * @covers \Mautic\CoreBundle\Helper\ListParser\ValueListParser::parse |
||||
80 | */ |
||||
81 | public function testSingleSelectedValueDoesNotGoIntoJson() |
||||
82 | { |
||||
83 | $string = '1'; |
||||
84 | $expected = [ |
||||
85 | '1' => '1', |
||||
86 | ]; |
||||
87 | $actual = AbstractFormFieldHelper::parseList($string); |
||||
0 ignored issues
–
show
The function
Mautic\CoreBundle\Helper...ieldHelper::parseList() has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
88 | |||||
89 | $this->assertEquals($expected, $actual); |
||||
90 | } |
||||
91 | |||||
92 | /** |
||||
93 | * @testdox The string is parsed correctly into a choice array |
||||
94 | * |
||||
95 | * @covers \Mautic\CoreBundle\Helper\AbstractFormFieldHelper::parseList |
||||
96 | * @covers \Mautic\CoreBundle\Helper\ListParser\ArrayListParser::parse |
||||
97 | */ |
||||
98 | public function testLabelValuePairsAreFlattened() |
||||
99 | { |
||||
100 | $array = [ |
||||
101 | [ |
||||
102 | 'label' => 'label1', |
||||
103 | 'value' => 'value1', |
||||
104 | ], |
||||
105 | [ |
||||
106 | 'label' => 'label2', |
||||
107 | 'value' => 'value2', |
||||
108 | ], |
||||
109 | [ |
||||
110 | 'label' => 'label3', |
||||
111 | 'value' => 'value3', |
||||
112 | ], |
||||
113 | ]; |
||||
114 | $expected = [ |
||||
115 | 'value1' => 'label1', |
||||
116 | 'value2' => 'label2', |
||||
117 | 'value3' => 'label3', |
||||
118 | ]; |
||||
119 | $actual = AbstractFormFieldHelper::parseList($array); |
||||
0 ignored issues
–
show
The function
Mautic\CoreBundle\Helper...ieldHelper::parseList() has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
120 | |||||
121 | $this->assertEquals($expected, $actual); |
||||
122 | } |
||||
123 | |||||
124 | /** |
||||
125 | * @testdox The string is parsed correctly into a choice array |
||||
126 | * |
||||
127 | * @covers \Mautic\CoreBundle\Helper\AbstractFormFieldHelper::parseList |
||||
128 | * @covers \Mautic\CoreBundle\Helper\ListParser\ArrayListParser::parse |
||||
129 | */ |
||||
130 | public function testLabelValuePairsAreFlattenedWithOptGroup() |
||||
131 | { |
||||
132 | $array['optGroup1'] = [ |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
133 | [ |
||||
134 | 'label' => 'label1', |
||||
135 | 'value' => 'value1', |
||||
136 | ], |
||||
137 | [ |
||||
138 | 'label' => 'label2', |
||||
139 | 'value' => 'value2', |
||||
140 | ], |
||||
141 | [ |
||||
142 | 'label' => 'label3', |
||||
143 | 'value' => 'value3', |
||||
144 | ], |
||||
145 | ]; |
||||
146 | $array['optGroup2'] = [ |
||||
147 | [ |
||||
148 | 'label' => 'label1', |
||||
149 | 'value' => 'value1', |
||||
150 | ], |
||||
151 | [ |
||||
152 | 'label' => 'label2', |
||||
153 | 'value' => 'value2', |
||||
154 | ], |
||||
155 | [ |
||||
156 | 'label' => 'label3', |
||||
157 | 'value' => 'value3', |
||||
158 | ], |
||||
159 | ]; |
||||
160 | $expected = [ |
||||
161 | 'optGroup1' => [ |
||||
162 | 'value1' => 'label1', |
||||
163 | 'value2' => 'label2', |
||||
164 | 'value3' => 'label3', |
||||
165 | ], |
||||
166 | 'optGroup2' => [ |
||||
167 | 'value1' => 'label1', |
||||
168 | 'value2' => 'label2', |
||||
169 | 'value3' => 'label3', |
||||
170 | ], |
||||
171 | ]; |
||||
172 | $actual = AbstractFormFieldHelper::parseList($array); |
||||
0 ignored issues
–
show
The function
Mautic\CoreBundle\Helper...ieldHelper::parseList() has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
173 | |||||
174 | $this->assertEquals($expected, $actual); |
||||
175 | } |
||||
176 | |||||
177 | /** |
||||
178 | * @covers \Mautic\CoreBundle\Helper\ListParser\ArrayListParser::parse |
||||
179 | */ |
||||
180 | public function testNumericalArrayConvertedToKeyLabelPairs() |
||||
181 | { |
||||
182 | $array = [ |
||||
183 | 'value1', |
||||
184 | 'value2', |
||||
185 | 'value3', |
||||
186 | ]; |
||||
187 | |||||
188 | $expected = [ |
||||
189 | 'value1' => 'value1', |
||||
190 | 'value2' => 'value2', |
||||
191 | 'value3' => 'value3', |
||||
192 | ]; |
||||
193 | $actual = AbstractFormFieldHelper::parseList($array); |
||||
0 ignored issues
–
show
The function
Mautic\CoreBundle\Helper...ieldHelper::parseList() has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
194 | |||||
195 | $this->assertEquals($expected, $actual); |
||||
196 | } |
||||
197 | |||||
198 | /** |
||||
199 | * @covers \Mautic\CoreBundle\Helper\ListParser\ArrayListParser::parse |
||||
200 | */ |
||||
201 | public function testBooleanArrayList() |
||||
202 | { |
||||
203 | $array = [ |
||||
204 | 0 => 'no', |
||||
205 | 1 => 'yes', |
||||
206 | ]; |
||||
207 | |||||
208 | $expected = [ |
||||
209 | 0 => 'no', |
||||
210 | 1 => 'yes', |
||||
211 | ]; |
||||
212 | |||||
213 | $actual = AbstractFormFieldHelper::parseBooleanList($array); |
||||
214 | |||||
215 | $this->assertEquals($expected, $actual); |
||||
216 | } |
||||
217 | |||||
218 | /** |
||||
219 | * @covers \Mautic\CoreBundle\Helper\ListParser\BarListParser::parse |
||||
220 | * @covers \Mautic\CoreBundle\Helper\ListParser\ArrayListParser::parse |
||||
221 | */ |
||||
222 | public function testBooleanBarStringList() |
||||
223 | { |
||||
224 | $string = 'no|yes||0|1'; |
||||
225 | $expected = [ |
||||
226 | 0 => 'no', |
||||
227 | 1 => 'yes', |
||||
228 | ]; |
||||
229 | |||||
230 | $actual = AbstractFormFieldHelper::parseBooleanList($string); |
||||
231 | |||||
232 | $this->assertEquals($expected, $actual); |
||||
233 | } |
||||
234 | |||||
235 | /** |
||||
236 | * @covers \Mautic\CoreBundle\Helper\ListParser\JsonListParser::parse |
||||
237 | * @covers \Mautic\CoreBundle\Helper\ListParser\ArrayListParser::parse |
||||
238 | */ |
||||
239 | public function testBooleanJsonStringList() |
||||
240 | { |
||||
241 | $string = '["no", "yes"]'; |
||||
242 | $expected = [ |
||||
243 | 0 => 'no', |
||||
244 | 1 => 'yes', |
||||
245 | ]; |
||||
246 | |||||
247 | $actual = AbstractFormFieldHelper::parseBooleanList($string); |
||||
248 | |||||
249 | $this->assertEquals($expected, $actual); |
||||
250 | } |
||||
251 | |||||
252 | /** |
||||
253 | * @covers \Mautic\CoreBundle\Helper\ListParser\JsonListParser::parse |
||||
254 | * @covers \Mautic\CoreBundle\Helper\ListParser\ArrayListParser::parse |
||||
255 | */ |
||||
256 | public function testNumericalJsonStringList() |
||||
257 | { |
||||
258 | $string = '["no", "yes"]'; |
||||
259 | $expected = [ |
||||
260 | 'no' => 'no', |
||||
261 | 'yes' => 'yes', |
||||
262 | ]; |
||||
263 | |||||
264 | $actual = AbstractFormFieldHelper::parseList($string); |
||||
0 ignored issues
–
show
The function
Mautic\CoreBundle\Helper...ieldHelper::parseList() has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
265 | |||||
266 | $this->assertEquals($expected, $actual); |
||||
267 | } |
||||
268 | } |
||||
269 |