Completed
Push — master ( 161d08...f48b40 )
by Jeroen De
05:53 queued 03:09
created

tests/phpunit/ProcessorTest.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace ParamProcessor\Tests;
4
5
use ParamProcessor\Processor;
6
use ParamProcessor\Options;
7
8
/**
9
 * @covers ParamProcessor\Processor
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class ProcessorTest extends \PHPUnit_Framework_TestCase {
15
16
	public function testNewDefault() {
17
		$this->assertInstanceOf( 'ParamProcessor\Processor', Processor::newDefault() );
18
	}
19
20
	public function newFromOptionsProvider() {
21
		$options = [];
22
23
		$option = new Options();
24
25
		$options[] = clone $option;
26
27
		$option->setName( 'foobar' );
28
		$option->setLowercaseNames( false );
29
30
		$options[] = clone $option;
31
32
		return $this->arrayWrap( $options );
33
	}
34
35
	private function arrayWrap( array $elements ) {
36
		return array_map(
37
			function( $element ) {
38
				return [ $element ];
39
			},
40
			$elements
41
		);
42
	}
43
44
	public function testNewFromOptions() {
45
		$options = new Options();
46
		$validator = Processor::newFromOptions( clone $options );
47
		$this->assertInstanceOf( '\ParamProcessor\Processor', $validator );
48
		$this->assertEquals( $options, $validator->getOptions() );
49
	}
50
51
	/**
52
	 * Simple parameter definitions and values that should all pass.
53
	 *
54
	 * @return array
55
	 */
56
	private function getSimpleParams() {
57
		$params = [
58
			'awesome' => 'yes',
59
			'Howmuch ' => '9001',
60
			'FLOAT' => '4.2',
61
			' page' => 'Ohi there!',
62
			' text     ' => 'foo bar baz o_O',
63
		];
64
65
		$definitions = [
66
			'awesome' => [
67
				'type' => 'boolean',
68
			],
69
			'howmuch' => [
70
				'type' => 'integer',
71
			],
72
			'float' => [
73
				'type' => 'float',
74
			],
75
			'page' => [
76
				'type' => 'string',
77
				'hastoexist' => false,
78
			],
79
			'text' => [],
80
		];
81
82
		$options = new Options();
83
84
		$expected = [
85
			'awesome' => true,
86
			'howmuch' => 9001,
87
			'float' => 4.2,
88
			'page' => 'Ohi there!',
89
			'text' => 'foo bar baz o_O',
90
		];
91
92
		return [ $params, $definitions, $options, $expected ];
93
	}
94
95
	/**
96
	 * Simple parameter definitions with defaults and values
97
	 * that are invalid or missing and therefore default.
98
	 *
99
	 * @return array
100
	 */
101
	private function getDefaultingParams() {
102
		$params = [
103
			'awesome' => 'omg!',
104
			'howmuch' => 'omg!',
105
			'float' => 'omg!',
106
			'page' => 42,
107
			'whot?' => 'O_o',
108
			'integerr' => ' 9001 ',
109
		];
110
111
		$definitions = [
112
			'awesome' => [
113
				'type' => 'boolean',
114
				'default' => true,
115
			],
116
			'howmuch' => [
117
				'type' => 'integer',
118
				'default' => 9001,
119
			],
120
			'float' => [
121
				'type' => 'float',
122
				'default' => 4.2,
123
			],
124
			'page' => [
125
				'type' => 'string',
126
				'hastoexist' => false,
127
				'default' => 'Ohi there!',
128
			],
129
			'text' => [
130
				'default' => 'foo bar baz o_O',
131
			],
132
			'integerr' => [
133
				'type' => 'integer',
134
				'default' => 42,
135
			],
136
		];
137
138
		$options = new Options();
139
		$options->setTrimValues( false );
140
141
		$expected = [
142
			'awesome' => true,
143
			'howmuch' => 9001,
144
			'float' => 4.2,
145
			'page' => 'Ohi there!',
146
			'text' => 'foo bar baz o_O',
147
			'integerr' => 42,
148
		];
149
150
		return [ $params, $definitions, $options, $expected ];
151
	}
152
153
	/**
154
	 * Values and definitions in-system parameter handling.
155
	 * Options set to expect non-raw values.
156
	 *
157
	 * @return array
158
	 */
159
	private function getTypedParams() {
160
		$params = [
161
			'awesome' => true,
162
			'howmuch' => '42',
163
			'float' => 4.2,
164
			'page' => 'Ohi there!',
165
			'Text' => 'foo bar baz o_O',
166
			'text1 ' => 'foo bar baz o_O',
167
			' text2' => 'foo bar baz o_O',
168
		];
169
170
		$definitions = [
171
			'awesome' => [
172
				'type' => 'boolean',
173
			],
174
			'howmuch' => [
175
				'type' => 'integer',
176
				'default' => 9001,
177
			],
178
			'float' => [
179
				'type' => 'float',
180
				'lowerbound' => 9001,
181
				'default' => 9000.1
182
			],
183
			'page' => [
184
				'type' => 'string',
185
				'hastoexist' => false,
186
			],
187
			'text' => [
188
				'default' => 'some text',
189
			],
190
			'text1' => [
191
				'default' => 'some text',
192
			],
193
			'text2' => [
194
				'default' => 'some text',
195
			],
196
		];
197
198
		$options = new Options();
199
		$options->setRawStringInputs( false );
200
		$options->setLowercaseNames( false );
201
		$options->setTrimNames( false );
202
203
		$expected = [
204
			'awesome' => true,
205
			'howmuch' => 9001,
206
			'float' => 9000.1,
207
			'page' => 'Ohi there!',
208
			'text' => 'some text',
209
			'text1' => 'some text',
210
			'text2' => 'some text',
211
		];
212
213
		return [ $params, $definitions, $options, $expected ];
214
	}
215
216
	/**
217
	 * Values with capitalization and preceding/tailing spaces to test
218
	 * of the clean options work.
219
	 *
220
	 * @return array
221
	 */
222
	private function getUncleanParams() {
223
		$params = [
224
			'awesome' => ' yes ',
225
			'text' => ' FOO  bar  ',
226
			'integerr' => ' 9001 ',
227
		];
228
229
		$definitions = [
230
			'awesome' => [
231
				'type' => 'boolean',
232
			],
233
			'text' => [
234
				'default' => 'bar',
235
			],
236
			'integerr' => [
237
				'type' => 'integer',
238
				'default' => 42,
239
			],
240
		];
241
242
		$options = new Options();
243
		$options->setLowercaseValues( true );
244
		$options->setTrimValues( true );
245
246
		$expected = [
247
			'awesome' => true,
248
			'text' => 'foo  bar',
249
			'integerr' => 9001,
250
		];
251
252
		return [ $params, $definitions, $options, $expected ];
253
	}
254
255
	/**
256
	 * List parameters to test if list handling works correctly.
257
	 *
258
	 * @return array
259
	 */
260
	private function getListParams() {
261
		$params = [
262
			'awesome' => ' yes, no, on, off ',
263
			'float' => ' 9001 ; 42 ; 4.2;0',
264
		];
265
266
		$definitions = [
267
			'awesome' => [
268
				'type' => 'boolean',
269
				'islist' => true,
270
			],
271
			'text' => [
272
				'default' => [ 'bar' ],
273
				'islist' => true,
274
			],
275
			'float' => [
276
				'type' => 'float',
277
				'islist' => true,
278
				'delimiter' => ';'
279
			],
280
		];
281
282
		$options = new Options();
283
		$options->setLowercaseValues( true );
284
		$options->setTrimValues( true );
285
286
		$expected = [
287
			'awesome' => [ true, false, true, false ],
288
			'text' => [ 'bar' ],
289
			'float' => [ 9001.0, 42.0, 4.2, 0.0 ],
290
		];
291
292
		return [ $params, $definitions, $options, $expected ];
293
	}
294
295
	public function parameterProvider() {
296
		// $params, $definitions [, $options, $expected]
297
		$argLists = [];
298
299
		$argLists[] = $this->getSimpleParams();
300
301
		$argLists[] = $this->getDefaultingParams();
302
303
		$argLists[] = $this->getTypedParams();
304
305
		$argLists[] = $this->getUncleanParams();
306
307
		$argLists[] = $this->getListParams();
308
309
		foreach ( $argLists as &$argList ) {
310
			foreach ( $argList[1] as $key => &$definition ) {
311
				$definition['message'] = 'test-' . $key;
312
			}
313
314
			if ( !array_key_exists( 2, $argList ) ) {
315
				$argList[2] = new Options();
316
			}
317
		}
318
319
		return $argLists;
320
	}
321
322
	/**
323
	 * @dataProvider parameterProvider
324
	 */
325
	public function testSetParameters( array $params, array $definitions, Options $options, array $expected = [] ) {
0 ignored issues
show
The parameter $expected is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
326
		$validator = Processor::newFromOptions( $options );
327
328
		$validator->setParameters( $params, $definitions );
329
330
		$this->assertTrue( true ); // TODO
331
	}
332
333
	/**
334
	 * @dataProvider parameterProvider
335
	 */
336
	public function testValidateParameters( array $params, array $definitions, Options $options, array $expected = [] ) {
337
		$validator = Processor::newFromOptions( $options );
338
339
		$validator->setParameters( $params, $definitions );
340
341
		$processingResult = $validator->processParameters();
342
343
		$actualValues = [];
344
345
		foreach ( $processingResult->getParameters() as $param ) {
346
			$actualValues[$param->getName()] = $param->getValue();
347
		}
348
349
		$this->assertEquals( $expected, $actualValues );
350
351
352
	}
353
354
	public function testProcessParametersOnEmptyOptions() {
355
356
		$options = new Options();
357
		$validator = Processor::newFromOptions( $options );
358
359
		$this->assertInstanceOf(
360
			'\ParamProcessor\ProcessingResult',
361
			$validator->processParameters()
362
		);
363
	}
364
365
}
366