Test Failed
Push — pureUnbound ( c79936 )
by no
04:16
created

UnboundedQuantityValueTest::getClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\DataValue;
6
use DataValues\DecimalValue;
7
use DataValues\IllegalValueException;
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 'DataValues\UnboundedQuantityValue';
28
	}
29
30
	public function validConstructorArgumentsProvider() {
31
		$argLists = array();
32
33
		$argLists[] = array( new DecimalValue( '+42' ), '1' );
34
		$argLists[] = array( new DecimalValue( '+0.01' ), '1' );
35
		$argLists[] = array( new DecimalValue( '-0.5' ), '1' );
36
37
		return $argLists;
38
	}
39
40
	public function invalidConstructorArgumentsProvider() {
41
		$argLists = array();
42
43
		$argLists[] = array( new DecimalValue( '+0' ), '' );
44
		$argLists[] = array( 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->assertInstanceOf( $this->getClass(), $quantity->getValue() );
54
	}
55
56
	/**
57
	 * @dataProvider instanceProvider
58
	 */
59
	public function testGetAmount( UnboundedQuantityValue $quantity, array $arguments ) {
60
		$this->assertEquals( $arguments[0], $quantity->getAmount() );
61
	}
62
63
	/**
64
	 * @dataProvider instanceProvider
65
	 */
66
	public function testGetUnit( UnboundedQuantityValue $quantity, array $arguments ) {
67
		$this->assertEquals( $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 array(
81
			array(
82
				42, '1',
83
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
84
			),
85
			array(
86
				-0.05, '1',
87
				new UnboundedQuantityValue( new DecimalValue( '-0.05' ), '1' )
88
			),
89
			array(
90
				0, 'm',
91
				new UnboundedQuantityValue( new DecimalValue( '+0' ), 'm' )
92
			),
93
			array(
94
				'+23', '1',
95
				new UnboundedQuantityValue( new DecimalValue( '+23' ), '1' )
96
			),
97
			array(
98
				'+42', '1',
99
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
100
			),
101
			array(
102
				'-0.05', 'm',
103
				new UnboundedQuantityValue( new DecimalValue( '-0.05' ), 'm' )
104
			),
105
			array(
106
				new DecimalValue( '+42' ), '1',
107
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
108
			),
109
		);
110
	}
111
112
	/**
113
	 * @see https://phabricator.wikimedia.org/T110728
114
	 * @see http://www.regular-expressions.info/anchors.html#realend
115
	 */
116
	public function testTrailingNewlineRobustness() {
117
		$value = UnboundedQuantityValue::newFromArray( array(
118
			'amount' => "-0.0\n",
119
			'unit' => "1\n",
120
		) );
121
122
		$this->assertSame( array(
123
			'amount' => '+0.0',
124
			'unit' => "1\n",
125
		), $value->getArrayValue() );
126
	}
127
128
	/**
129
	 * @dataProvider instanceProvider
130
	 */
131
	public function testGetSortKey( UnboundedQuantityValue $quantity ) {
132
		$this->assertEquals( $quantity->getAmount()->getValueFloat(), $quantity->getSortKey() );
133
	}
134
135
	/**
136
	 * @dataProvider transformProvider
137
	 */
138
	public function testTransform( UnboundedQuantityValue $quantity, $transformation, UnboundedQuantityValue $expected ) {
139
		$args = func_get_args();
140
		$extraArgs = array_slice( $args, 3 );
141
142
		$call = array( $quantity, 'transform' );
143
		$callArgs = array_merge( array( 'x', $transformation ), $extraArgs );
144
		$actual = call_user_func_array( $call, $callArgs );
145
146
		$this->assertEquals( 'x', $actual->getUnit() );
147
		$this->assertEquals( $expected->getAmount()->getValue(), $actual->getAmount()->getValue(), 'value' );
148
	}
149
150
	public function transformProvider() {
151
		$identity = function ( DecimalValue $value ) {
152
			return $value;
153
		};
154
155
		$square = function ( DecimalValue $value ) {
156
			$v = $value->getValueFloat();
157
			return new DecimalValue( $v * $v * $v );
158
		};
159
160
		$scale = function ( DecimalValue $value, $factor ) {
161
			return new DecimalValue( $value->getValueFloat() * $factor );
162
		};
163
164
		return array(
165
			 0 => array( UnboundedQuantityValue::newFromNumber( '+10', '1' ), $identity, UnboundedQuantityValue::newFromNumber( '+10', '?' ) ),
166
			 1 => array( UnboundedQuantityValue::newFromNumber( '-0.5', '1' ), $identity, UnboundedQuantityValue::newFromNumber( '-0.5', '?' ) ),
167
			 2 => array( UnboundedQuantityValue::newFromNumber( '+0', '1' ), $square,   UnboundedQuantityValue::newFromNumber( '+0', '?' ) ),
168
			 3 => array( UnboundedQuantityValue::newFromNumber( '+10', '1' ), $square,   UnboundedQuantityValue::newFromNumber( '+1000', '?' ) ), // note how rounding applies to bounds
169
			 4 => array( UnboundedQuantityValue::newFromNumber( '+0.5', '1' ), $scale,    UnboundedQuantityValue::newFromNumber( '+0.25', '?' ), 0.5 ),
170
171
			// note: absolutely exact values require conversion with infinite precision!
172
			10 => array( UnboundedQuantityValue::newFromNumber( '+100', '1' ), $scale, UnboundedQuantityValue::newFromNumber( '+12825.0', '?' ), 128.25 ),
173
			13 => array( UnboundedQuantityValue::newFromNumber( '+100', '1' ), $scale, UnboundedQuantityValue::newFromNumber( '+333.33', '?' ), 3.3333 ),
174
		);
175
	}
176
177
	public function provideNewFromArray() {
178
		return [
179
			'unbounded' => [
180
				[
181
					'amount' => '+2',
182
					'unit' => '1',
183
				],
184
				UnboundedQuantityValue::newFromNumber( '+2', '1' )
185
			],
186
			'with-extra' => [
187
				[
188
					'amount' => '+2',
189
					'unit' => '1',
190
					'upperBound' => '+2.5',
191
					'lowerBound' => '+1.5',
192
				],
193
				UnboundedQuantityValue::newFromNumber( '+2', '1' )
194
			],
195
		];
196
	}
197
198
	/**
199
	 * @dataProvider provideNewFromArray
200
	 */
201
	public function testNewFromArray( $data, DataValue $expected ) {
202
		$value = UnboundedQuantityValue::newFromArray( $data );
203
		$this->assertTrue( $expected->equals( $value ), $value . ' should equal ' . $expected );
204
	}
205
206
	public function provideNewFromArray_failure() {
207
		return [
208
			'no-amount' => [
209
				[
210
					'unit' => "1",
211
				]
212
			],
213
			'no-unit' => [
214
				[
215
					'amount' => '+2',
216
				]
217
			],
218
			'bad-amount' => [
219
				[
220
					'amount' => 'x',
221
					'unit' => "1",
222
				]
223
			],
224
		];
225
	}
226
227
	/**
228
	 * @dataProvider provideNewFromArray_failure
229
	 */
230
	public function testNewFromArray_failure( $data ) {
231
		$this->setExpectedException( IllegalValueException::class );
232
		UnboundedQuantityValue::newFromArray( $data );
233
	}
234
235
}
236