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-or-later |
17
|
|
|
* @author Daniel Kinzler |
18
|
|
|
*/ |
19
|
|
|
class UnboundedQuantityValueTest extends DataValuesTestBase { |
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 ) { |
|
|
|
|
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()->getValueFloat(), $quantity->getAmount()->getValueFloat() ); |
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 ); |
|
|
|
|
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->expectException( IllegalValueException::class ); |
155
|
|
|
UnboundedQuantityValue::newFromArray( $data ); |
|
|
|
|
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( [ |
|
|
|
|
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( |
245
|
|
|
UnboundedQuantityValue $quantity, |
246
|
|
|
$transformation, |
247
|
|
|
UnboundedQuantityValue $expected |
248
|
|
|
) { |
249
|
|
|
$args = func_get_args(); |
250
|
|
|
$extraArgs = array_slice( $args, 3 ); |
251
|
|
|
|
252
|
|
|
$call = [ $quantity, 'transform' ]; |
253
|
|
|
$callArgs = array_merge( [ 'x', $transformation ], $extraArgs ); |
254
|
|
|
$actual = call_user_func_array( $call, $callArgs ); |
255
|
|
|
|
256
|
|
|
$this->assertSame( 'x', $actual->getUnit() ); |
257
|
|
|
$this->assertEquals( |
258
|
|
|
floatval( $expected->getAmount()->getValue() ), |
259
|
|
|
floatval( $actual->getAmount()->getValue() ), |
260
|
|
|
'value' |
261
|
|
|
); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function transformProvider() { |
265
|
|
|
$identity = function ( DecimalValue $value ) { |
266
|
|
|
return $value; |
267
|
|
|
}; |
268
|
|
|
|
269
|
|
|
$square = function ( DecimalValue $value ) { |
270
|
|
|
$v = $value->getValueFloat(); |
271
|
|
|
return new DecimalValue( $v * $v * $v ); |
272
|
|
|
}; |
273
|
|
|
|
274
|
|
|
$scale = function ( DecimalValue $value, $factor ) { |
275
|
|
|
return new DecimalValue( $value->getValueFloat() * $factor ); |
276
|
|
|
}; |
277
|
|
|
|
278
|
|
|
return [ |
279
|
|
|
0 => [ |
280
|
|
|
UnboundedQuantityValue::newFromNumber( '+10', '1' ), |
281
|
|
|
$identity, |
282
|
|
|
UnboundedQuantityValue::newFromNumber( '+10', '?' ) |
283
|
|
|
], |
284
|
|
|
1 => [ |
285
|
|
|
UnboundedQuantityValue::newFromNumber( '-0.5', '1' ), |
286
|
|
|
$identity, |
287
|
|
|
UnboundedQuantityValue::newFromNumber( '-0.5', '?' ) |
288
|
|
|
], |
289
|
|
|
2 => [ |
290
|
|
|
UnboundedQuantityValue::newFromNumber( '+0', '1' ), |
291
|
|
|
$square, |
292
|
|
|
UnboundedQuantityValue::newFromNumber( '+0', '?' ) |
293
|
|
|
], |
294
|
|
|
3 => [ |
295
|
|
|
UnboundedQuantityValue::newFromNumber( '+10', '1' ), |
296
|
|
|
$square, |
297
|
|
|
UnboundedQuantityValue::newFromNumber( '+1000', '?' ) |
298
|
|
|
], |
299
|
|
|
4 => [ |
300
|
|
|
UnboundedQuantityValue::newFromNumber( '+0.5', '1' ), |
301
|
|
|
$scale, |
302
|
|
|
UnboundedQuantityValue::newFromNumber( '+0.25', '?' ), |
303
|
|
|
0.5 |
304
|
|
|
], |
305
|
|
|
|
306
|
|
|
// note: absolutely exact values require conversion with infinite precision! |
307
|
|
|
10 => [ |
308
|
|
|
UnboundedQuantityValue::newFromNumber( '+100', '1' ), |
309
|
|
|
$scale, |
310
|
|
|
UnboundedQuantityValue::newFromNumber( '+12825', '?' ), |
311
|
|
|
128.25 |
312
|
|
|
], |
313
|
|
|
13 => [ |
314
|
|
|
UnboundedQuantityValue::newFromNumber( '+100', '1' ), |
315
|
|
|
$scale, |
316
|
|
|
UnboundedQuantityValue::newFromNumber( '+333.33', '?' ), |
317
|
|
|
3.3333 |
318
|
|
|
], |
319
|
|
|
]; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
} |
323
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.