Completed
Pull Request — master (#66)
by Daniel
06:11 queued 03:53
created

UnboundedQuantityValueTest::testGetAmount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
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->assertInstanceOf( $this->getClass(), $quantity->getValue() );
52
	}
53
54
	/**
55
	 * @dataProvider instanceProvider
56
	 */
57
	public function testGetAmount( UnboundedQuantityValue $quantity, array $arguments ) {
58
		$this->assertEquals( $arguments[0], $quantity->getAmount() );
59
	}
60
61
	/**
62
	 * @dataProvider instanceProvider
63
	 */
64
	public function testGetUnit( UnboundedQuantityValue $quantity, array $arguments ) {
65
		$this->assertEquals( $arguments[1], $quantity->getUnit() );
66
	}
67
68
	public function newFromNumberProvider() {
69
		return array(
70
			array(
71
				42, '1',
72
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
73
			),
74
			array(
75
				-0.05, '1',
76
				new UnboundedQuantityValue( new DecimalValue( '-0.05' ), '1' )
77
			),
78
			array(
79
				0, 'm',
80
				new UnboundedQuantityValue( new DecimalValue( '+0' ), 'm' )
81
			),
82
			array(
83
				'+23', '1',
84
				new UnboundedQuantityValue( new DecimalValue( '+23' ), '1' )
85
			),
86
			array(
87
				'+42', '1',
88
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
89
			),
90
			array(
91
				'-0.05', 'm',
92
				new UnboundedQuantityValue( new DecimalValue( '-0.05' ), 'm' )
93
			),
94
			array(
95
				new DecimalValue( '+42' ), '1',
96
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
97
			),
98
		);
99
	}
100
101
	/**
102
	 * @see https://phabricator.wikimedia.org/T110728
103
	 * @see http://www.regular-expressions.info/anchors.html#realend
104
	 */
105
	public function testTrailingNewlineRobustness() {
106
		$value = UnboundedQuantityValue::newFromArray( array(
107
			'amount' => "-0.0\n",
108
			'unit' => "1\n",
109
		) );
110
111
		$this->assertSame( array(
112
			'amount' => '+0.0',
113
			'unit' => "1\n",
114
		), $value->getArrayValue() );
115
	}
116
117
	/**
118
	 * @dataProvider instanceProvider
119
	 */
120
	public function testGetSortKey( UnboundedQuantityValue $quantity ) {
121
		$this->assertEquals( $quantity->getAmount()->getValueFloat(), $quantity->getSortKey() );
122
	}
123
124
	/**
125
	 * @dataProvider transformProvider
126
	 */
127
	public function testTransform( UnboundedQuantityValue $quantity, $transformation, UnboundedQuantityValue $expected ) {
128
		$args = func_get_args();
129
		$extraArgs = array_slice( $args, 3 );
130
131
		$call = array( $quantity, 'transform' );
132
		$callArgs = array_merge( array( 'x', $transformation ), $extraArgs );
133
		$actual = call_user_func_array( $call, $callArgs );
134
135
		$this->assertEquals( 'x', $actual->getUnit() );
136
		$this->assertEquals( $expected->getAmount()->getValue(), $actual->getAmount()->getValue(), 'value' );
137
	}
138
139
	public function transformProvider() {
140
		$identity = function ( DecimalValue $value ) {
141
			return $value;
142
		};
143
144
		$square = function ( DecimalValue $value ) {
145
			$v = $value->getValueFloat();
146
			return new DecimalValue( $v * $v * $v );
147
		};
148
149
		$scale = function ( DecimalValue $value, $factor ) {
150
			return new DecimalValue( $value->getValueFloat() * $factor );
151
		};
152
153
		return array(
154
			 0 => array( UnboundedQuantityValue::newFromNumber( '+10', '1' ), $identity, UnboundedQuantityValue::newFromNumber( '+10', '?' ) ),
155
			 1 => array( UnboundedQuantityValue::newFromNumber( '-0.5', '1' ), $identity, UnboundedQuantityValue::newFromNumber( '-0.5', '?' ) ),
156
			 2 => array( UnboundedQuantityValue::newFromNumber( '+0', '1' ), $square,   UnboundedQuantityValue::newFromNumber( '+0', '?' ) ),
157
			 3 => array( UnboundedQuantityValue::newFromNumber( '+10', '1' ), $square,   UnboundedQuantityValue::newFromNumber( '+1000', '?' ) ), // note how rounding applies to bounds
158
			 4 => array( UnboundedQuantityValue::newFromNumber( '+0.5', '1' ), $scale,    UnboundedQuantityValue::newFromNumber( '+0.25', '?' ), 0.5 ),
159
160
			// note: absolutely exact values require conversion with infinite precision!
161
			10 => array( UnboundedQuantityValue::newFromNumber( '+100', '1' ), $scale, UnboundedQuantityValue::newFromNumber( '+12825.0', '?' ), 128.25 ),
162
			13 => array( UnboundedQuantityValue::newFromNumber( '+100', '1' ), $scale, UnboundedQuantityValue::newFromNumber( '+333.33', '?' ), 3.3333 ),
163
		);
164
	}
165
166
}
167