Completed
Push — master ( 7c2e45...8ff551 )
by Jeroen De
22s
created

ProcessorTest::testNewFromOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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 = array();
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 array( $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 = array(
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 = array(
66
			'awesome' => array(
67
				'type' => 'boolean',
68
			),
69
			'howmuch' => array(
70
				'type' => 'integer',
71
			),
72
			'float' => array(
73
				'type' => 'float',
74
			),
75
			'page' => array(
76
				'type' => 'string',
77
				'hastoexist' => false,
78
			),
79
			'text' => array(),
80
		);
81
82
		$options = new Options();
83
84
		$expected = array(
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 array( $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 = array(
103
			'awesome' => 'omg!',
104
			'howmuch' => 'omg!',
105
			'float' => 'omg!',
106
			'page' => 42,
107
			'whot?' => 'O_o',
108
			'integerr' => ' 9001 ',
109
		);
110
111
		$definitions = array(
112
			'awesome' => array(
113
				'type' => 'boolean',
114
				'default' => true,
115
			),
116
			'howmuch' => array(
117
				'type' => 'integer',
118
				'default' => 9001,
119
			),
120
			'float' => array(
121
				'type' => 'float',
122
				'default' => 4.2,
123
			),
124
			'page' => array(
125
				'type' => 'string',
126
				'hastoexist' => false,
127
				'default' => 'Ohi there!',
128
			),
129
			'text' => array(
130
				'default' => 'foo bar baz o_O',
131
			),
132
			'integerr' => array(
133
				'type' => 'integer',
134
				'default' => 42,
135
			),
136
		);
137
138
		$options = new Options();
139
		$options->setTrimValues( false );
140
141
		$expected = array(
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 array( $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 = array(
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 = array(
171
			'awesome' => array(
172
				'type' => 'boolean',
173
			),
174
			'howmuch' => array(
175
				'type' => 'integer',
176
				'default' => 9001,
177
			),
178
			'float' => array(
179
				'type' => 'float',
180
				'lowerbound' => 9001,
181
				'default' => 9000.1
182
			),
183
			'page' => array(
184
				'type' => 'string',
185
				'hastoexist' => false,
186
			),
187
			'text' => array(
188
				'default' => 'some text',
189
			),
190
			'text1' => array(
191
				'default' => 'some text',
192
			),
193
			'text2' => array(
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 = array(
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 array( $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 = array(
224
			'awesome' => ' yes ',
225
			'text' => ' FOO  bar  ',
226
			'integerr' => ' 9001 ',
227
		);
228
229
		$definitions = array(
230
			'awesome' => array(
231
				'type' => 'boolean',
232
			),
233
			'text' => array(
234
				'default' => 'bar',
235
			),
236
			'integerr' => array(
237
				'type' => 'integer',
238
				'default' => 42,
239
			),
240
		);
241
242
		$options = new Options();
243
		$options->setLowercaseValues( true );
244
		$options->setTrimValues( true );
245
246
		$expected = array(
247
			'awesome' => true,
248
			'text' => 'foo  bar',
249
			'integerr' => 9001,
250
		);
251
252
		return array( $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 = array(
262
			'awesome' => ' yes, no, on, off ',
263
			'float' => ' 9001 ; 42 ; 4.2;0',
264
		);
265
266
		$definitions = array(
267
			'awesome' => array(
268
				'type' => 'boolean',
269
				'islist' => true,
270
			),
271
			'text' => array(
272
				'default' => array( 'bar' ),
273
				'islist' => true,
274
			),
275
			'float' => array(
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 = array(
287
			'awesome' => array( true, false, true, false ),
288
			'text' => array( 'bar' ),
289
			'float' => array( 9001.0, 42.0, 4.2, 0.0 ),
290
		);
291
292
		return array( $params, $definitions, $options, $expected );
293
	}
294
295
	public function parameterProvider() {
296
		// $params, $definitions [, $options, $expected]
297
		$argLists = array();
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 = array() ) {
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 = array() ) {
337
		$validator = Processor::newFromOptions( $options );
338
339
		$validator->setParameters( $params, $definitions );
340
341
		$processingResult = $validator->processParameters();
342
343
		$actualValues = array();
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