Passed
Push — master ( 5dec3e...1c52e3 )
by Jeroen De
02:02
created

DimensionTypeTest::widthProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A DimensionTypeTest::validInputProvider() 0 13 1
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 validInputProvider
17
	 */
18
	public function testValidInput( string $input, string $expected ) {
19
		$parameters = $this->process(
20
			[
21
				'width' => [
22
					'type' => ParameterTypes::DIMENSION,
23
					'units' => [ 'px', 'ex', 'em', '%' ],
24
					'message' => 'test-message'
25
				]
26
			],
27
			[
28
				'width' => $input,
29
			]
30
		)->getParameterArray();
31
32
		$this->assertSame( $expected, $parameters['width'] );
33
	}
34
35
	public function validInputProvider() {
36
		yield [ '10', '10px' ];
37
		yield [ '10px', '10px' ];
38
		yield [ '10%', '10%' ];
39
		yield [ '10em', '10em' ];
40
		yield [ '10ex', '10ex' ];
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
	/**
50
	 * @dataProvider validInputProvider
51
	 */
52
	public function testValidInputWhenDefaultIsDefined( string $input, string $expected ) {
53
		$parameters = $this->process(
54
			[
55
				'width' => [
56
					'type' => ParameterTypes::DIMENSION,
57
					'units' => [ 'px', 'ex', 'em', '%' ],
58
					'default' => '1337px',
59
					'message' => 'test-message'
60
				]
61
			],
62
			[
63
				'width' => $input,
64
			]
65
		)->getParameterArray();
66
67
		$this->assertSame( $expected, $parameters['width'] );
68
	}
69
70
	private function process( array $definitionArrays, array $userInput ): ProcessingResult {
71
		$processor = Processor::newDefault();
72
73
		$processor->setParameters( $userInput );
74
		$processor->setParameterDefinitions(
75
			ParamDefinitionFactory::newDefault()->newDefinitionsFromArrays( $definitionArrays )
76
		);
77
78
		return $processor->processParameters();
79
	}
80
81
	/**
82
	 * @dataProvider heightProvider
83
	 */
84
	public function testHeight( string $input, string $expected ) {
85
		$parameters = $this->process(
86
			[
87
				'height' => [
88
					'type' => ParameterTypes::DIMENSION,
89
					'message' => 'test-message'
90
				]
91
			],
92
			[
93
				'height' => $input,
94
			]
95
		)->getParameterArray();
96
97
		$this->assertSame( $expected, $parameters['height'] );
98
	}
99
100
	public function heightProvider() {
101
		yield [ '10', '10px' ];
102
		yield [ '10px', '10px' ];
103
		yield [ '10em', '10em' ];
104
		yield [ '10ex', '10ex' ];
105
	}
106
107
	public function testAlternateDefaultUnit() {
108
		$parameters = $this->process(
109
			[
110
				'height' => [
111
					'type' => ParameterTypes::DIMENSION,
112
					'defaultunit' => '%',
113
					'message' => 'test-message'
114
				]
115
			],
116
			[
117
				'height' => '2.5',
118
			]
119
		)->getParameterArray();
120
121
		$this->assertSame( '2.5%', $parameters['height'] );
122
	}
123
124
	/**
125
	 * @dataProvider invalidDimensionProvider
126
	 */
127
	public function testInvalidInputsDefault( string $invalidDimension ) {
128
		$parameters = $this->process(
129
			[
130
				'height' => [
131
					'type' => ParameterTypes::DIMENSION,
132
					'default' => '42%',
133
					'lowerbound' => 20,
134
					'upperbound' => 80,
135
					'minpercentage' => 30,
136
					'maxpercentage' => 70,
137
					'message' => 'test-message'
138
				]
139
			],
140
			[
141
				'height' => $invalidDimension,
142
			]
143
		)->getParameterArray();
144
145
		$this->assertSame( '42%', $parameters['height'] );
146
	}
147
148
	public function invalidDimensionProvider() {
149
		yield [ 'invalid' ];
150
		yield [ 'px' ];
151
		yield [ '19' ];
152
		yield [ '81' ];
153
		yield [ '29%' ];
154
		yield [ '71%' ];
155
		yield 'auto not allowed' => [ 'auto' ];
156
		yield 'unit not allowed' => [ '1 wtf' ];
157
	}
158
159
	/**
160
	 * @dataProvider validBoundsInput
161
	 */
162
	public function testValidInputWithBounds( string $valid ) {
163
		$parameters = $this->process(
164
			[
165
				'height' => [
166
					'type' => ParameterTypes::DIMENSION,
167
					'units' => [ 'px', '%' ],
168
					'default' => '42%',
169
					'lowerbound' => 20,
170
					'upperbound' => 80,
171
					'minpercentage' => 30,
172
					'maxpercentage' => 70,
173
					'message' => 'test-message'
174
				]
175
			],
176
			[
177
				'height' => $valid,
178
			]
179
		)->getParameterArray();
180
181
		$this->assertSame( $valid, $parameters['height'] );
182
	}
183
184
	public function validBoundsInput() {
185
		yield [ '20px' ];
186
		yield [ '21px' ];
187
		yield [ '80px' ];
188
		yield [ '79px' ];
189
		yield [ '30%' ];
190
		yield [ '31%' ];
191
		yield [ '70%' ];
192
		yield [ '69%' ];
193
	}
194
195
	public function testAllowAuto() {
196
		$parameters = $this->process(
197
			[
198
				'height' => [
199
					'type' => ParameterTypes::DIMENSION,
200
					'allowauto' => true,
201
					'message' => 'test-message'
202
				]
203
			],
204
			[
205
				'height' => 'auto',
206
			]
207
		)->getParameterArray();
208
209
		$this->assertSame( 'auto', $parameters['height'] );
210
	}
211
212
	public function testCanUseSpecialUnit() {
213
		$parameters = $this->process(
214
			[
215
				'height' => [
216
					'type' => ParameterTypes::DIMENSION,
217
					'units' => [ 'wtf' ],
218
					'message' => 'test-message'
219
				]
220
			],
221
			[
222
				'height' => '4.2 wtf',
223
			]
224
		)->getParameterArray();
225
226
		$this->assertSame( '4.2wtf', $parameters['height'] );
227
	}
228
229
}
230