UnboundedQuantityValueTest   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 296
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getClass() 0 3 1
A validConstructorArgumentsProvider() 0 9 1
A invalidConstructorArgumentsProvider() 0 8 1
A testGetValue() 0 3 1
A testGetAmount() 0 3 1
A testGetUnit() 0 3 1
A testNewFromNumber() 0 5 1
A newFromNumberProvider() 0 32 1
A testNewFromArray() 0 4 1
A validArraySerializationProvider() 0 29 1
A testNewFromArray_failure() 0 4 1
B invalidArraySerializationProvider() 0 59 1
A testTrailingNewlineRobustness() 0 11 1
A testGetSortKey() 0 3 1
A testTransform() 0 11 1
B transformProvider() 0 57 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\UnboundedQuantityValue
12
 *
13
 * @group DataValue
14
 * @group DataValueExtensions
15
 *
16
 * @license GPL-2.0+
17
 * @author Daniel Kinzler
18
 */
19
class UnboundedQuantityValueTest extends DataValueTest {
20
21
	/**
22
	 * @see DataValueTest::getClass
23
	 *
24
	 * @return string
25
	 */
26
	public function getClass() {
27
		return UnboundedQuantityValue::class;
28
	}
29
30
	public function validConstructorArgumentsProvider() {
31
		$argLists = [];
32
33
		$argLists[] = [ new DecimalValue( '+42' ), '1' ];
34
		$argLists[] = [ new DecimalValue( '+0.01' ), '1' ];
35
		$argLists[] = [ new DecimalValue( '-0.5' ), '1' ];
36
37
		return $argLists;
38
	}
39
40
	public function invalidConstructorArgumentsProvider() {
41
		$argLists = [];
42
43
		$argLists[] = [ new DecimalValue( '+0' ), '' ];
44
		$argLists[] = [ new DecimalValue( '+0' ), 1 ];
45
46
		return $argLists;
47
	}
48
49
	/**
50
	 * @dataProvider instanceProvider
51
	 */
52
	public function testGetValue( UnboundedQuantityValue $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...
53
		$this->assertSame( $quantity, $quantity->getValue() );
54
	}
55
56
	/**
57
	 * @dataProvider instanceProvider
58
	 */
59
	public function testGetAmount( UnboundedQuantityValue $quantity, array $arguments ) {
60
		$this->assertSame( $arguments[0], $quantity->getAmount() );
61
	}
62
63
	/**
64
	 * @dataProvider instanceProvider
65
	 */
66
	public function testGetUnit( UnboundedQuantityValue $quantity, array $arguments ) {
67
		$this->assertSame( $arguments[1], $quantity->getUnit() );
68
	}
69
70
	/**
71
	 * @dataProvider newFromNumberProvider
72
	 */
73
	public function testNewFromNumber( $amount, $unit, UnboundedQuantityValue $expected ) {
74
		$quantity = UnboundedQuantityValue::newFromNumber( $amount, $unit );
75
76
		$this->assertEquals( $expected->getAmount()->getValue(), $quantity->getAmount()->getValue() );
77
	}
78
79
	public function newFromNumberProvider() {
80
		return [
81
			[
82
				42, '1',
83
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
84
			],
85
			[
86
				-0.05, '1',
87
				new UnboundedQuantityValue( new DecimalValue( '-0.05' ), '1' )
88
			],
89
			[
90
				0, 'm',
91
				new UnboundedQuantityValue( new DecimalValue( '+0' ), 'm' )
92
			],
93
			[
94
				'+23', '1',
95
				new UnboundedQuantityValue( new DecimalValue( '+23' ), '1' )
96
			],
97
			[
98
				'+42', '1',
99
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
100
			],
101
			[
102
				'-0.05', 'm',
103
				new UnboundedQuantityValue( new DecimalValue( '-0.05' ), 'm' )
104
			],
105
			[
106
				new DecimalValue( '+42' ), '1',
107
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
108
			],
109
		];
110
	}
111
112
	/**
113
	 * @dataProvider validArraySerializationProvider
114
	 */
115
	public function testNewFromArray( $data, UnboundedQuantityValue $expected ) {
116
		$value = UnboundedQuantityValue::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...
117
		$this->assertTrue( $expected->equals( $value ), $value . ' should equal ' . $expected );
118
	}
119
120
	public function validArraySerializationProvider() {
121
		return [
122
			'unbounded' => [
123
				[
124
					'amount' => '+2',
125
					'unit' => '1',
126
				],
127
				UnboundedQuantityValue::newFromNumber( '+2', '1' )
128
			],
129
			'unbounded with existing array keys' => [
130
				[
131
					'amount' => '+2',
132
					'unit' => '1',
133
					'upperBound' => null,
134
					'lowerBound' => null,
135
				],
136
				UnboundedQuantityValue::newFromNumber( '+2', '1' )
137
			],
138
			'with-extra' => [
139
				[
140
					'amount' => '+2',
141
					'unit' => '1',
142
					'upperBound' => '+2.5',
143
					'lowerBound' => '+1.5',
144
				],
145
				QuantityValue::newFromNumber( '+2', '1', '+2.5', '+1.5' )
146
			],
147
		];
148
	}
149
150
	/**
151
	 * @dataProvider invalidArraySerializationProvider
152
	 */
153
	public function testNewFromArray_failure( $data ) {
154
		$this->setExpectedException( IllegalValueException::class );
155
		UnboundedQuantityValue::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...
156
	}
157
158
	public function invalidArraySerializationProvider() {
159
		return [
160
			'not an array (string)' => [
161
				'foo'
162
			],
163
			'not an array (int)' => [
164
				303
165
			],
166
			'not an array (object)' => [
167
				new \stdClass()
168
			],
169
			'no-amount' => [
170
				[
171
					'unit' => '1',
172
				]
173
			],
174
			'no-unit' => [
175
				[
176
					'amount' => '+2',
177
				]
178
			],
179
			'no-upperBound' => [
180
				[
181
					'amount' => '+2',
182
					'unit' => '1',
183
					'lowerBound' => '+1.5',
184
				]
185
			],
186
			'no-lowerBound' => [
187
				[
188
					'amount' => '+2',
189
					'unit' => '1',
190
					'upperBound' => '+2.5',
191
				]
192
			],
193
			'bad-amount' => [
194
				[
195
					'amount' => 'x',
196
					'unit' => '1',
197
				]
198
			],
199
			'bad-upperBound' => [
200
				[
201
					'amount' => '+2',
202
					'unit' => '1',
203
					'upperBound' => 'x',
204
					'lowerBound' => '+1.5',
205
				]
206
			],
207
			'bad-lowerBound' => [
208
				[
209
					'amount' => '+2',
210
					'unit' => '1',
211
					'upperBound' => '+2.5',
212
					'lowerBound' => 'x',
213
				]
214
			],
215
		];
216
	}
217
218
	/**
219
	 * @see https://phabricator.wikimedia.org/T110728
220
	 * @see http://www.regular-expressions.info/anchors.html#realend
221
	 */
222
	public function testTrailingNewlineRobustness() {
223
		$value = UnboundedQuantityValue::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...
224
			'amount' => "-0.0\n",
225
			'unit' => "1\n",
226
		] );
227
228
		$this->assertSame( [
229
			'amount' => '+0.0',
230
			'unit' => "1\n",
231
		], $value->getArrayValue() );
232
	}
233
234
	/**
235
	 * @dataProvider instanceProvider
236
	 */
237
	public function testGetSortKey( UnboundedQuantityValue $quantity ) {
238
		$this->assertSame( $quantity->getAmount()->getValueFloat(), $quantity->getSortKey() );
239
	}
240
241
	/**
242
	 * @dataProvider transformProvider
243
	 */
244
	public function testTransform( UnboundedQuantityValue $quantity, $transformation, UnboundedQuantityValue $expected ) {
245
		$args = func_get_args();
246
		$extraArgs = array_slice( $args, 3 );
247
248
		$call = [ $quantity, 'transform' ];
249
		$callArgs = array_merge( [ 'x', $transformation ], $extraArgs );
250
		$actual = call_user_func_array( $call, $callArgs );
251
252
		$this->assertSame( 'x', $actual->getUnit() );
253
		$this->assertEquals( $expected->getAmount()->getValue(), $actual->getAmount()->getValue(), 'value' );
254
	}
255
256
	public function transformProvider() {
257
		$identity = function ( DecimalValue $value ) {
258
			return $value;
259
		};
260
261
		$square = function ( DecimalValue $value ) {
262
			$v = $value->getValueFloat();
263
			return new DecimalValue( $v * $v * $v );
264
		};
265
266
		$scale = function ( DecimalValue $value, $factor ) {
267
			return new DecimalValue( $value->getValueFloat() * $factor );
268
		};
269
270
		return [
271
			0 => [
272
				UnboundedQuantityValue::newFromNumber( '+10', '1' ),
273
				$identity,
274
				UnboundedQuantityValue::newFromNumber( '+10', '?' )
275
			],
276
			1 => [
277
				UnboundedQuantityValue::newFromNumber( '-0.5', '1' ),
278
				$identity,
279
				UnboundedQuantityValue::newFromNumber( '-0.5', '?' )
280
			],
281
			2 => [
282
				UnboundedQuantityValue::newFromNumber( '+0', '1' ),
283
				$square,
284
				UnboundedQuantityValue::newFromNumber( '+0', '?' )
285
			],
286
			3 => [
287
				UnboundedQuantityValue::newFromNumber( '+10', '1' ),
288
				$square,
289
				UnboundedQuantityValue::newFromNumber( '+1000', '?' )
290
			],
291
			4 => [
292
				UnboundedQuantityValue::newFromNumber( '+0.5', '1' ),
293
				$scale,
294
				UnboundedQuantityValue::newFromNumber( '+0.25', '?' ),
295
				0.5
296
			],
297
298
			// note: absolutely exact values require conversion with infinite precision!
299
			10 => [
300
				UnboundedQuantityValue::newFromNumber( '+100', '1' ),
301
				$scale,
302
				UnboundedQuantityValue::newFromNumber( '+12825', '?' ),
303
				128.25
304
			],
305
			13 => [
306
				UnboundedQuantityValue::newFromNumber( '+100', '1' ),
307
				$scale,
308
				UnboundedQuantityValue::newFromNumber( '+333.33', '?' ),
309
				3.3333
310
			],
311
		];
312
	}
313
314
}
315