QuantityValueTest   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 439
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 4
dl 0
loc 439
rs 10
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 2
A getClass() 0 3 1
A validConstructorArgumentsProvider() 0 9 1
A invalidConstructorArgumentsProvider() 0 11 1
A testGetValue() 0 3 1
A testGetAmount() 0 3 1
A testGetUnit() 0 3 1
A testGetUpperBound() 0 3 1
A testGetLowerBound() 0 3 1
A testNewFromNumber() 0 7 1
A testNewFromArray() 0 4 1
A validArraySerializationProvider() 0 29 1
A testNewFromArray_failure() 0 4 1
B invalidArraySerializationProvider() 0 56 1
A testTrailingNewlineRobustness() 0 15 1
A testGetSortKey() 0 3 1
A testGetUncertainty() 0 3 1
A getUncertaintyProvider() 0 14 1
A testGetUncertaintyMargin() 0 3 1
A getUncertaintyMarginProvider() 0 12 1
A testGetOrderOfUncertainty() 0 3 1
A getOrderOfUncertaintyProvider() 0 19 1
A testTransform() 0 25 1
B transformProvider() 0 71 1
A newFromNumberProvider() 0 52 1
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\DecimalValue;
6
use DataValues\IllegalValueException;
7
use DataValues\QuantityValue;
8
use DataValues\UnboundedQuantityValue;
9
10
/**
11
 * @covers \DataValues\QuantityValue
12
 *
13
 * @group DataValue
14
 * @group DataValueExtensions
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Daniel Kinzler
18
 */
19
class QuantityValueTest extends DataValuesTestBase {
20
21
	public function setUp(): void {
22
		if ( !\extension_loaded( 'bcmath' ) ) {
23
			$this->markTestSkipped( 'bcmath extension not loaded' );
24
		}
25
	}
26
27
	/**
28
	 * @see DataValueTest::getClass
29
	 *
30
	 * @return string
31
	 */
32
	public function getClass() {
33
		return QuantityValue::class;
34
	}
35
36
	public function validConstructorArgumentsProvider() {
37
		$argLists = [];
38
39
		$argLists[] = [ new DecimalValue( '+42' ), '1', new DecimalValue( '+42' ), new DecimalValue( '+42' ) ];
40
		$argLists[] = [ new DecimalValue( '+0.01' ), '1', new DecimalValue( '+0.02' ), new DecimalValue( '+0.0001' ) ];
41
		$argLists[] = [ new DecimalValue( '-0.5' ), '1', new DecimalValue( '+0.02' ), new DecimalValue( '-0.7' ) ];
42
43
		return $argLists;
44
	}
45
46
	public function invalidConstructorArgumentsProvider() {
47
		$argLists = [];
48
49
		$argLists[] = [ new DecimalValue( '+0' ), '', new DecimalValue( '+0' ), new DecimalValue( '+0' ) ];
50
		$argLists[] = [ new DecimalValue( '+0' ), 1, new DecimalValue( '+0' ), new DecimalValue( '+0' ) ];
51
52
		$argLists[] = [ new DecimalValue( '+0' ), '1', new DecimalValue( '-0.001' ), new DecimalValue( '-1' ) ];
53
		$argLists[] = [ new DecimalValue( '+0' ), '1', new DecimalValue( '+1' ), new DecimalValue( '+0.001' ) ];
54
55
		return $argLists;
56
	}
57
58
	/**
59
	 * @dataProvider instanceProvider
60
	 */
61
	public function testGetValue( QuantityValue $quantity, array $arguments ) {
0 ignored issues
show
Unused Code introduced by
The parameter $arguments 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...
62
		$this->assertSame( $quantity, $quantity->getValue() );
63
	}
64
65
	/**
66
	 * @dataProvider instanceProvider
67
	 */
68
	public function testGetAmount( QuantityValue $quantity, array $arguments ) {
69
		$this->assertSame( $arguments[0], $quantity->getAmount() );
70
	}
71
72
	/**
73
	 * @dataProvider instanceProvider
74
	 */
75
	public function testGetUnit( QuantityValue $quantity, array $arguments ) {
76
		$this->assertSame( $arguments[1], $quantity->getUnit() );
77
	}
78
79
	/**
80
	 * @dataProvider instanceProvider
81
	 */
82
	public function testGetUpperBound( QuantityValue $quantity, array $arguments ) {
83
		$this->assertSame( $arguments[2], $quantity->getUpperBound() );
84
	}
85
86
	/**
87
	 * @dataProvider instanceProvider
88
	 */
89
	public function testGetLowerBound( QuantityValue $quantity, array $arguments ) {
90
		$this->assertSame( $arguments[3], $quantity->getLowerBound() );
91
	}
92
93
	/**
94
	 * @dataProvider newFromNumberProvider
95
	 */
96
	public function testNewFromNumber( $amount, $unit, $upperBound, $lowerBound, QuantityValue $expected ) {
97
		$quantity = QuantityValue::newFromNumber( $amount, $unit, $upperBound, $lowerBound );
98
99
		$this->assertEquals( $expected->getAmount()->getValueFloat(), $quantity->getAmount()->getValueFloat() );
100
		$this->assertEquals( $expected->getUpperBound()->getValueFloat(), $quantity->getUpperBound()->getValueFloat() );
101
		$this->assertEquals( $expected->getLowerBound()->getValueFloat(), $quantity->getLowerBound()->getValueFloat() );
102
	}
103
104
	public function newFromNumberProvider() {
105
		$value = new DecimalValue( '+42' );
106
		$unit = '1';
107
		yield [
108
			$value->getValueFloat(), $unit, null, null,
109
			new QuantityValue( $value, $unit, $value, $value )
110
		];
111
		$value = new DecimalValue( '-0.05' );
112
		yield [
113
			$value->getValueFloat(), $unit, null, null,
114
			new QuantityValue( $value, $unit, $value, $value )
115
		];
116
		$value = new DecimalValue( '+0' );
117
		$value1 = new DecimalValue( '+0.5' );
118
		$value2 = new DecimalValue( '-0.5' );
119
		$unit = 'm';
120
		yield [
121
			$value->getValueFloat(), $unit, $value1->getValueFloat(), $value2->getValueFloat(),
122
			new QuantityValue( $value, $unit, $value1, $value2 )
123
		];
124
		$value = new DecimalValue( '+23' );
125
		$unit = '1';
126
		yield [
127
			$value->getValueFloat(), $unit, null, null,
128
			new QuantityValue( $value, $unit, $value, $value )
129
		];
130
		$value = new DecimalValue( '+42' );
131
		$value1 = new DecimalValue( '+43' );
132
		$value2 = new DecimalValue( '+41' );
133
		$unit = '1';
134
		yield [
135
			$value->getValueFloat(), $unit, $value1->getValueFloat(), $value2->getValueFloat(),
136
			new QuantityValue( $value, $unit, $value1, $value2 )
137
		];
138
		$value = new DecimalValue( '-0.05' );
139
		$value1 = new DecimalValue( '-0.04' );
140
		$value2 = new DecimalValue( '-0.06' );
141
142
		$unit = 'm';
143
		yield [
144
			$value->getValueFloat(), $unit, $value1->getValueFloat(), $value2->getValueFloat(),
145
			new QuantityValue( $value, $unit, $value1, $value2 )
146
		];
147
		$value = new DecimalValue( '+42' );
148
		$value1 = new DecimalValue( 43 );
149
		$value2 = new DecimalValue( 41.0 );
150
		$unit = '1';
151
		yield [
152
			$value, $unit, $value1, $value2,
153
			new QuantityValue( $value, $unit, $value1, $value2 )
154
		];
155
	}
156
157
	/**
158
	 * @dataProvider validArraySerializationProvider
159
	 */
160
	public function testNewFromArray( $data, UnboundedQuantityValue $expected ) {
161
		$value = QuantityValue::newFromArray( $data );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\UnboundedQuantityValue::newFromArray() has been deprecated with message: since 0.8.3. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
162
		$this->assertTrue( $expected->equals( $value ), $value . ' should equal ' . $expected );
163
	}
164
165
	public function validArraySerializationProvider() {
166
		return [
167
			'complete' => [
168
				[
169
					'amount' => '+2',
170
					'unit' => '1',
171
					'upperBound' => '+2.5',
172
					'lowerBound' => '+1.5',
173
				],
174
				QuantityValue::newFromNumber( '+2', '1', '+2.5', '+1.5' )
175
			],
176
			'unbounded' => [
177
				[
178
					'amount' => '+2',
179
					'unit' => '1',
180
				],
181
				UnboundedQuantityValue::newFromNumber( '+2', '1' )
182
			],
183
			'unbounded with existing array keys' => [
184
				[
185
					'amount' => '+2',
186
					'unit' => '1',
187
					'upperBound' => null,
188
					'lowerBound' => null,
189
				],
190
				UnboundedQuantityValue::newFromNumber( '+2', '1' )
191
			],
192
		];
193
	}
194
195
	/**
196
	 * @dataProvider invalidArraySerializationProvider
197
	 */
198
	public function testNewFromArray_failure( $data ) {
199
		$this->expectException( IllegalValueException::class );
200
		QuantityValue::newFromArray( $data );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\UnboundedQuantityValue::newFromArray() has been deprecated with message: since 0.8.3. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
201
	}
202
203
	public function invalidArraySerializationProvider() {
204
		return [
205
			'no-amount' => [
206
				[
207
					'unit' => '1',
208
					'upperBound' => '+2.5',
209
					'lowerBound' => '+1.5',
210
				]
211
			],
212
			'no-unit' => [
213
				[
214
					'amount' => '+2',
215
					'upperBound' => '+2.5',
216
					'lowerBound' => '+1.5',
217
				]
218
			],
219
			'no-upperBound' => [
220
				[
221
					'amount' => '+2',
222
					'unit' => '1',
223
					'lowerBound' => '+1.5',
224
				]
225
			],
226
			'no-lowerBound' => [
227
				[
228
					'amount' => '+2',
229
					'unit' => '1',
230
					'upperBound' => '+2.5',
231
				]
232
			],
233
			'bad-amount' => [
234
				[
235
					'amount' => 'x',
236
					'unit' => '1',
237
					'upperBound' => '+2.5',
238
					'lowerBound' => '+1.5',
239
				]
240
			],
241
			'bad-upperBound' => [
242
				[
243
					'amount' => '+2',
244
					'unit' => '1',
245
					'upperBound' => 'x',
246
					'lowerBound' => '+1.5',
247
				]
248
			],
249
			'bad-lowerBound' => [
250
				[
251
					'amount' => '+2',
252
					'unit' => '1',
253
					'upperBound' => '+2.5',
254
					'lowerBound' => 'x',
255
				]
256
			],
257
		];
258
	}
259
260
	/**
261
	 * @see https://phabricator.wikimedia.org/T110728
262
	 * @see http://www.regular-expressions.info/anchors.html#realend
263
	 */
264
	public function testTrailingNewlineRobustness() {
265
		$value = QuantityValue::newFromArray( [
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\UnboundedQuantityValue::newFromArray() has been deprecated with message: since 0.8.3. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
266
			'amount' => "-0.0\n",
267
			'unit' => "1\n",
268
			'upperBound' => "-0.0\n",
269
			'lowerBound' => "-0.0\n",
270
		] );
271
272
		$this->assertSame( [
273
			'amount' => '+0.0',
274
			'unit' => "1\n",
275
			'upperBound' => '+0.0',
276
			'lowerBound' => '+0.0',
277
		], $value->getArrayValue() );
278
	}
279
280
	/**
281
	 * @dataProvider instanceProvider
282
	 */
283
	public function testGetSortKey( QuantityValue $quantity ) {
284
		$this->assertSame( $quantity->getAmount()->getValueFloat(), $quantity->getSortKey() );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\UnboundedQuantityValue::getSortKey() has been deprecated with message: Kept for compatibility with older DataValues versions.
Do not use.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
285
	}
286
287
	/**
288
	 * @dataProvider getUncertaintyProvider
289
	 */
290
	public function testGetUncertainty( QuantityValue $quantity, $expected ) {
291
		$this->assertSame( $expected, $quantity->getUncertainty() );
292
	}
293
294
	public function getUncertaintyProvider() {
295
		return [
296
			[ QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), 0.0 ],
297
298
			[ QuantityValue::newFromNumber( '+0', '1', '+1', '-1' ), 2.0 ],
299
			[ QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), 0.02 ],
300
			[ QuantityValue::newFromNumber( '+100', '1', '+101', '+99' ), 2.0 ],
301
			[ QuantityValue::newFromNumber( '+100.0', '1', '+100.1', '+99.9' ), 0.2 ],
302
			[ QuantityValue::newFromNumber( '+12.34', '1', '+12.35', '+12.33' ), 0.02 ],
303
304
			[ QuantityValue::newFromNumber( '+0', '1', '+0.2', '-0.6' ), 0.8 ],
305
			[ QuantityValue::newFromNumber( '+7.3', '1', '+7.7', '+5.2' ), 2.5 ],
306
		];
307
	}
308
309
	/**
310
	 * @dataProvider getUncertaintyMarginProvider
311
	 */
312
	public function testGetUncertaintyMargin( QuantityValue $quantity, $expected ) {
313
		$this->assertSame( $expected, $quantity->getUncertaintyMargin()->getValue() );
314
	}
315
316
	public function getUncertaintyMarginProvider() {
317
		return [
318
			[ QuantityValue::newFromNumber( '+0', '1', '+1', '-1' ), '+1' ],
319
			[ QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), '+0.01' ],
320
321
			[ QuantityValue::newFromNumber( '-1', '1', '-1', '-1' ), '+0' ],
322
323
			[ QuantityValue::newFromNumber( '+0', '1', '+0.2', '-0.6' ), '+0.6' ],
324
			[ QuantityValue::newFromNumber( '+7.5', '1', '+7.5', '+5.5' ), '+2.0' ],
325
			[ QuantityValue::newFromNumber( '+11.5', '1', '+15', '+10.5' ), '+3.5' ],
326
		];
327
	}
328
329
	/**
330
	 * @dataProvider getOrderOfUncertaintyProvider
331
	 */
332
	public function testGetOrderOfUncertainty( QuantityValue $quantity, $expected ) {
333
		$this->assertSame( $expected, $quantity->getOrderOfUncertainty() );
334
	}
335
336
	public function getOrderOfUncertaintyProvider() {
337
		return [
338
			0 => [ QuantityValue::newFromNumber( '+0' ), 0 ],
339
			1 => [ QuantityValue::newFromNumber( '-123' ), 0 ],
340
			2 => [ QuantityValue::newFromNumber( '-1.23' ), -2 ],
341
342
			10 => [ QuantityValue::newFromNumber( '-100', '1', '-99', '-101' ), 0 ],
343
			11 => [ QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), -2 ],
344
			12 => [ QuantityValue::newFromNumber( '-117.3', '1', '-117.2', '-117.4' ), -1 ],
345
346
			20 => [ QuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.97' ), -2 ],
347
			21 => [ QuantityValue::newFromNumber( '-0.002', '1', '-0.001', '-0.004' ), -3 ],
348
			22 => [ QuantityValue::newFromNumber( '-0.002', '1', '+0.001', '-0.06' ), -3 ],
349
			23 => [ QuantityValue::newFromNumber( '-21', '1', '+1.1', '-120' ), 1 ],
350
			24 => [ QuantityValue::newFromNumber( '-2', '1', '+1.1', '-120' ), 0 ],
351
			25 => [ QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900.03' ), 1 ],
352
			26 => [ QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900' ), 2 ],
353
		];
354
	}
355
356
	/**
357
	 * @dataProvider transformProvider
358
	 */
359
	public function testTransform( QuantityValue $quantity, $transformation, QuantityValue $expected ) {
360
		$args = func_get_args();
361
		$extraArgs = array_slice( $args, 3 );
362
363
		$call = [ $quantity, 'transform' ];
364
		$callArgs = array_merge( [ 'x', $transformation ], $extraArgs );
365
		$actual = call_user_func_array( $call, $callArgs );
366
367
		$this->assertSame( 'x', $actual->getUnit() );
368
		$this->assertEquals(
369
			$expected->getAmount()->getValue(),
370
			$actual->getAmount()->getValue(),
371
			'value'
372
		);
373
		$this->assertEquals(
374
			$expected->getUpperBound()->getValue(),
375
			$actual->getUpperBound()->getValue(),
376
			'upper bound'
377
		);
378
		$this->assertEquals(
379
			$expected->getLowerBound()->getValue(),
380
			$actual->getLowerBound()->getValue(),
381
			'lower bound'
382
		);
383
	}
384
385
	public function transformProvider() {
386
		$identity = function ( DecimalValue $value ) {
387
			return $value;
388
		};
389
390
		$square = function ( DecimalValue $value ) {
391
			$v = $value->getValueFloat();
392
			return new DecimalValue( $v * $v * $v );
393
		};
394
395
		$scale = function ( DecimalValue $value, $factor ) {
396
			return new DecimalValue( $value->getValueFloat() * $factor );
397
		};
398
399
		return [
400
			0 => [
401
				QuantityValue::newFromNumber( '+10', '1', '+11', '+9' ),
402
				$identity,
403
				QuantityValue::newFromNumber( '+10', '?', '+11', '+9' )
404
			],
405
			1 => [
406
				QuantityValue::newFromNumber( '-0.5', '1', '-0.4', '-0.6' ),
407
				$identity,
408
				QuantityValue::newFromNumber( '-0.5', '?', '-0.4', '-0.6' )
409
			],
410
			2 => [
411
				QuantityValue::newFromNumber( '+0', '1', '+1', '-1' ),
412
				$square,
413
				QuantityValue::newFromNumber( '+0', '?', '+1', '-1' )
414
			],
415
			3 => [
416
				QuantityValue::newFromNumber( '+10', '1', '+11', '+9' ),
417
				$square,
418
				// note how rounding applies to bounds
419
				QuantityValue::newFromNumber( '+1000', '?', '+1300', '+700' )
420
			],
421
			4 => [
422
				QuantityValue::newFromNumber( '+0.5', '1', '+0.6', '+0.4' ),
423
				$scale,
424
				QuantityValue::newFromNumber( '+0.25', '?', '+0.30', '+0.20' ),
425
				0.5
426
			],
427
428
			// note: absolutely exact values require conversion with infinite precision!
429
			10 => [
430
				QuantityValue::newFromNumber( '+100', '1', '+100', '+100' ),
431
				$scale,
432
				QuantityValue::newFromNumber( '+12825', '?', '+12825', '+12825' ),
433
				128.25
434
			],
435
436
			11 => [
437
				QuantityValue::newFromNumber( '+100', '1', '+110', '+90' ),
438
				$scale,
439
				QuantityValue::newFromNumber( '+330', '?', '+370', '+300' ),
440
				3.3333
441
			],
442
			12 => [
443
				QuantityValue::newFromNumber( '+100', '1', '+100.1', '+99.9' ),
444
				$scale,
445
				QuantityValue::newFromNumber( '+333.3', '?', '+333.7', '+333.0' ),
446
				3.3333
447
			],
448
			13 => [
449
				QuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.99' ),
450
				$scale,
451
				QuantityValue::newFromNumber( '+333.33', '?', '+333.36', '+333.30' ),
452
				3.3333
453
			],
454
		];
455
	}
456
457
}
458