1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace ParamProcessor\Tests\Integration; |
6
|
|
|
|
7
|
|
|
use ParamProcessor\ParamDefinitionFactory; |
8
|
|
|
use ParamProcessor\ParameterTypes; |
9
|
|
|
use ParamProcessor\ProcessingResult; |
10
|
|
|
use ParamProcessor\Processor; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
class DimensionTypeTest extends TestCase { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @dataProvider widthProvider |
17
|
|
|
*/ |
18
|
|
|
public function testWidth( string $input, string $expected ) { |
19
|
|
|
$parameters = $this->process( |
20
|
|
|
[ |
21
|
|
|
'width' => [ |
22
|
|
|
'type' => ParameterTypes::DIMENSION, |
23
|
|
|
'message' => 'test-message' |
24
|
|
|
] |
25
|
|
|
], |
26
|
|
|
[ |
27
|
|
|
'width' => $input, |
28
|
|
|
] |
29
|
|
|
)->getParameterArray(); |
30
|
|
|
|
31
|
|
|
$this->assertSame( $expected, $parameters['width'] ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function widthProvider() { |
35
|
|
|
yield [ '10', '10px' ]; |
36
|
|
|
yield [ '10px', '10px' ]; |
37
|
|
|
yield [ '10%', '10%' ]; |
38
|
|
|
yield [ '10em', '10em' ]; |
39
|
|
|
yield [ '10ex', '10ex' ]; |
40
|
|
|
yield [ 'auto', 'auto' ]; |
41
|
|
|
yield [ ' 10 ', '10px' ]; |
42
|
|
|
yield [ ' 1 ', '1px' ]; |
43
|
|
|
yield [ '1 px', '1px' ]; |
44
|
|
|
yield [ '1 ex', '1ex' ]; |
45
|
|
|
// TODO: make sure unit is after the value |
46
|
|
|
// TODO: make sure only the unit is present |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function process( array $definitionArrays, array $userInput ): ProcessingResult { |
50
|
|
|
$processor = Processor::newDefault(); |
51
|
|
|
|
52
|
|
|
$processor->setParameters( $userInput ); |
53
|
|
|
$processor->setParameterDefinitions( |
54
|
|
|
ParamDefinitionFactory::newDefault()->newDefinitionsFromArrays( $definitionArrays ) |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
return $processor->processParameters(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @dataProvider heightProvider |
62
|
|
|
*/ |
63
|
|
|
public function testHeight( string $input, string $expected ) { |
64
|
|
|
$parameters = $this->process( |
65
|
|
|
[ |
66
|
|
|
'height' => [ |
67
|
|
|
'type' => ParameterTypes::DIMENSION, |
68
|
|
|
'message' => 'test-message' |
69
|
|
|
] |
70
|
|
|
], |
71
|
|
|
[ |
72
|
|
|
'height' => $input, |
73
|
|
|
] |
74
|
|
|
)->getParameterArray(); |
75
|
|
|
|
76
|
|
|
$this->assertSame( $expected, $parameters['height'] ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function heightProvider() { |
80
|
|
|
yield [ '10', '10px' ]; |
81
|
|
|
yield [ '10px', '10px' ]; |
82
|
|
|
yield [ '10em', '10em' ]; |
83
|
|
|
yield [ '10ex', '10ex' ]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testAlternateDefaultUnit() { |
87
|
|
|
$parameters = $this->process( |
88
|
|
|
[ |
89
|
|
|
'height' => [ |
90
|
|
|
'type' => ParameterTypes::DIMENSION, |
91
|
|
|
'defaultunit' => '%', |
92
|
|
|
'message' => 'test-message' |
93
|
|
|
] |
94
|
|
|
], |
95
|
|
|
[ |
96
|
|
|
'height' => '2.5', |
97
|
|
|
] |
98
|
|
|
)->getParameterArray(); |
99
|
|
|
|
100
|
|
|
$this->assertSame( '2.5%', $parameters['height'] ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @dataProvider invalidDimensionProvider |
105
|
|
|
*/ |
106
|
|
|
public function testInvalidInputsDefault( string $invalidDimension ) { |
107
|
|
|
$parameters = $this->process( |
108
|
|
|
[ |
109
|
|
|
'height' => [ |
110
|
|
|
'type' => ParameterTypes::DIMENSION, |
111
|
|
|
'default' => '42%', |
112
|
|
|
'lowerbound' => 20, |
113
|
|
|
'upperbound' => 80, |
114
|
|
|
'minpercentage' => 30, |
115
|
|
|
'maxpercentage' => 70, |
116
|
|
|
'message' => 'test-message' |
117
|
|
|
] |
118
|
|
|
], |
119
|
|
|
[ |
120
|
|
|
'height' => $invalidDimension, |
121
|
|
|
] |
122
|
|
|
)->getParameterArray(); |
123
|
|
|
|
124
|
|
|
$this->assertSame( '42%', $parameters['height'] ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function invalidDimensionProvider() { |
128
|
|
|
yield [ 'invalid' ]; |
129
|
|
|
yield [ 'px' ]; |
130
|
|
|
yield [ '19' ]; |
131
|
|
|
yield [ '81' ]; |
132
|
|
|
yield [ '29%' ]; |
133
|
|
|
yield [ '71%' ]; |
134
|
|
|
yield 'auto not allowed' => [ 'auto' ]; |
135
|
|
|
yield 'unit not allowed' => [ '1 wtf' ]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @dataProvider validBoundsInput |
140
|
|
|
*/ |
141
|
|
|
public function testValidInputWithBounds( string $valid ) { |
142
|
|
|
$parameters = $this->process( |
143
|
|
|
[ |
144
|
|
|
'height' => [ |
145
|
|
|
'type' => ParameterTypes::DIMENSION, |
146
|
|
|
'default' => '42%', |
147
|
|
|
'lowerbound' => 20, |
148
|
|
|
'upperbound' => 80, |
149
|
|
|
'minpercentage' => 30, |
150
|
|
|
'maxpercentage' => 70, |
151
|
|
|
'message' => 'test-message' |
152
|
|
|
] |
153
|
|
|
], |
154
|
|
|
[ |
155
|
|
|
'height' => $valid, |
156
|
|
|
] |
157
|
|
|
)->getParameterArray(); |
158
|
|
|
|
159
|
|
|
$this->assertSame( $valid, $parameters['height'] ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function validBoundsInput() { |
163
|
|
|
yield [ '20px' ]; |
164
|
|
|
yield [ '21px' ]; |
165
|
|
|
yield [ '80px' ]; |
166
|
|
|
yield [ '79px' ]; |
167
|
|
|
// FIXME |
168
|
|
|
// yield [ '30%' ]; |
169
|
|
|
// yield [ '31%' ]; |
170
|
|
|
// yield [ '70%' ]; |
171
|
|
|
// yield [ '69%' ]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testAllowAuto() { |
175
|
|
|
$parameters = $this->process( |
176
|
|
|
[ |
177
|
|
|
'height' => [ |
178
|
|
|
'type' => ParameterTypes::DIMENSION, |
179
|
|
|
'allowauto' => true, |
180
|
|
|
'message' => 'test-message' |
181
|
|
|
] |
182
|
|
|
], |
183
|
|
|
[ |
184
|
|
|
'height' => 'auto', |
185
|
|
|
] |
186
|
|
|
)->getParameterArray(); |
187
|
|
|
|
188
|
|
|
$this->assertSame( 'auto', $parameters['height'] ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testCanUseSpecialUnit() { |
192
|
|
|
$parameters = $this->process( |
193
|
|
|
[ |
194
|
|
|
'height' => [ |
195
|
|
|
'type' => ParameterTypes::DIMENSION, |
196
|
|
|
'units' => [ 'wtf' ], |
197
|
|
|
'message' => 'test-message' |
198
|
|
|
] |
199
|
|
|
], |
200
|
|
|
[ |
201
|
|
|
'height' => '4.2 wtf', |
202
|
|
|
] |
203
|
|
|
)->getParameterArray(); |
204
|
|
|
|
205
|
|
|
$this->assertSame( '4.2wtf', $parameters['height'] ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|