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