Passed
Push — lineLength120 ( 5ecdb4...e7bd7c )
by no
16:04
created

QuantityFormatterTest::getFormatterClass()   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
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace ValueFormatters\Test;
4
5
use DataValues\QuantityValue;
6
use DataValues\UnboundedQuantityValue;
7
use ValueFormatters\DecimalFormatter;
8
use ValueFormatters\FormatterOptions;
9
use ValueFormatters\QuantityFormatter;
10
use ValueFormatters\ValueFormatter;
11
12
/**
13
 * @covers ValueFormatters\QuantityFormatter
14
 *
15
 * @group ValueFormatters
16
 * @group DataValueExtensions
17
 *
18
 * @license GPL-2.0+
19
 * @author Daniel Kinzler
20
 */
21
class QuantityFormatterTest extends ValueFormatterTestBase {
22
23
	/**
24
	 * @see ValueFormatterTestBase::getInstance
25
	 *
26
	 * @param FormatterOptions|null $options
27
	 *
28
	 * @return QuantityFormatter
29
	 */
30
	protected function getInstance( FormatterOptions $options = null ) {
31
		return $this->getQuantityFormatter( $options );
32
	}
33
34
	/**
35
	 * @param FormatterOptions|null $options
36
	 * @param string|null $quantityWithUnitFormat
37
	 *
38
	 * @return QuantityFormatter
39
	 */
40
	private function getQuantityFormatter(
41
		FormatterOptions $options = null,
42
		$quantityWithUnitFormat = null
43
	) {
44
		$vocabularyUriFormatter = $this->getMock( ValueFormatter::class );
45
		$vocabularyUriFormatter->expects( $this->any() )
46
			->method( 'format' )
47
			->will( $this->returnCallback( function( $unit ) {
48
				return $unit === '1' ? null : $unit;
49
			} ) );
50
51
		return new QuantityFormatter(
52
			$options,
53
			new DecimalFormatter( $options ),
54
			$vocabularyUriFormatter,
55
			$quantityWithUnitFormat
56
		);
57
	}
58
59
	/**
60
	 * @see ValueFormatterTestBase::validProvider
61
	 */
62
	public function validProvider() {
63
		$noMargin = new FormatterOptions( [
64
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false
65
		] );
66
67
		$withMargin = new FormatterOptions( [
68
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => true
69
		] );
70
71
		$noRounding = new FormatterOptions( [
72
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => true,
73
			QuantityFormatter::OPT_APPLY_ROUNDING => false
74
		] );
75
76
		$exactRounding = new FormatterOptions( [
77
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false,
78
			QuantityFormatter::OPT_APPLY_ROUNDING => -2
79
		] );
80
81
		$forceSign = new FormatterOptions( [
82
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false,
83
			DecimalFormatter::OPT_FORCE_SIGN => true,
84
		] );
85
86
		$noUnit = new FormatterOptions( [
87
			QuantityFormatter::OPT_APPLY_UNIT => false,
88
		] );
89
90
		return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('+0/nm' => ...9999999999964473±0')); (array<*,array>) is incompatible with the return type declared by the abstract method ValueFormatters\Test\Val...TestBase::validProvider of type array[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
91
			'+0/nm' => [ QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $noMargin ],
92
			'+0/wm' => [ QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0±0', $withMargin ],
93
94
			'+0.0/nm' => [ QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0 °', $noMargin ],
95
			'+0.0/wm' => [ QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0±0.1 °', $withMargin ],
96
			'+0.0/xr' => [ QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0 °', $exactRounding ],
97
98
			'-1205/nm' => [ QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1200 m', $noMargin ],
99
			'-1205/wm' => [ QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $withMargin ],
100
			'-1205/nr' => [ QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $noRounding ],
101
			'-1205/xr' => [ QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205 m', $exactRounding ],
102
			'-1205/nu' => [ QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100', $noUnit ],
103
104
			'+3.025/nm' => [ QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025', $noMargin ],
105
			'+3.025/wm' => [ QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025±0.0039', $withMargin ],
106
			'+3.025/xr' => [ QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.03', $exactRounding ],
107
108
			'+3.125/nr' => [ QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.125±0.125', $noRounding ],
109
			'+3.125/xr' => [ QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.13', $exactRounding ],
110
111
			'+3.125/fs' => [ QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '+3.13', $forceSign ],
112
113
			// Unbounded quantities with different options
114
			'UB: +0.0/nm' => [ UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $noMargin ],
115
			'UB: +0.0/wm' => [ UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $withMargin ],
116
			'UB: +0.0/xr' => [ UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $exactRounding ],
117
			'UB: +5.021/nm' => [ UnboundedQuantityValue::newFromNumber( '+5.021', '°' ), '5.021 °', $noMargin ],
118
			'UB: +5.021/wm' => [ UnboundedQuantityValue::newFromNumber( '+5.021', '°' ), '5.021 °', $withMargin ],
119
			'UB: +5.021/xr' => [ UnboundedQuantityValue::newFromNumber( '+5.021', '°' ), '5.02 °', $exactRounding ],
120
			'UB: +3.125/fs' => [ UnboundedQuantityValue::newFromNumber( '+3.125', '1' ), '+3.125', $forceSign ],
121
122
			// Unbounded quantities with enforced, exact rounding
123
			[ UnboundedQuantityValue::newFromNumber( '+0.00155', '1' ), '0.00', $exactRounding ],
124
			[ UnboundedQuantityValue::newFromNumber( '+0.0155', '1' ), '0.02', $exactRounding ],
125
			[ UnboundedQuantityValue::newFromNumber( '+0.155', '1' ), '0.16', $exactRounding ],
126
			[ UnboundedQuantityValue::newFromNumber( '+1.55', '1' ), '1.55', $exactRounding ],
127
			[ UnboundedQuantityValue::newFromNumber( '+15.5', '1' ), '15.5', $exactRounding ],
128
			[ UnboundedQuantityValue::newFromNumber( '+155', '1' ), '155', $exactRounding ],
129
130
			// Default options with different margins
131
			'24+-000.01' => [ QuantityValue::newFromNumber( '+24', '1', '+24.01', '+23.99' ), '24±0.01' ],
132
			'24+-000.10' => [ QuantityValue::newFromNumber( '+24', '1', '+24.1', '+23.9' ), '24±0.1' ],
133
			'24+-001.00' => [ QuantityValue::newFromNumber( '+24', '1', '+25', '+23' ), '24±1' ],
134
			'24+-010.00' => [ QuantityValue::newFromNumber( '+24', '1', '+34', '+14' ), '24±10' ],
135
			'24+-100.00' => [ QuantityValue::newFromNumber( '+24', '1', '+124', '-76' ), '24±100' ],
136
137
			// Rounding with a fixed +/-1 margin
138
			[ QuantityValue::newFromNumber( '+1.44', '1', '+2.44', '+0.44' ), '1', $noMargin ],
139
			[ QuantityValue::newFromNumber( '+1.45', '1', '+2.45', '+0.45' ), '1', $noMargin ],
140
			[ QuantityValue::newFromNumber( '+1.49', '1', '+2.49', '+0.49' ), '1', $noMargin ],
141
			[ QuantityValue::newFromNumber( '+1.50', '1', '+2.50', '+0.50' ), '2', $noMargin ],
142
			[ QuantityValue::newFromNumber( '+2.50', '1', '+3.50', '+1.50' ), '3', $noMargin ],
143
144
			// Rounding with different margins
145
			'1.55+/-0.09' => [ QuantityValue::newFromNumber( '+1.55', '1', '+1.64', '+1.46' ), '1.55', $noMargin ],
146
			'1.55+/-0.1' => [ QuantityValue::newFromNumber( '+1.55', '1', '+1.65', '+1.45' ), '1.6', $noMargin ],
147
			'1.55+/-0.49' => [ QuantityValue::newFromNumber( '+1.55', '1', '+2.04', '+1.06' ), '1.6', $noMargin ],
148
			'1.55+/-0.5' => [ QuantityValue::newFromNumber( '+1.55', '1', '+2.05', '+1.05' ), '1.6', $noMargin ],
149
			'1.55+/-0.99' => [ QuantityValue::newFromNumber( '+1.55', '1', '+2.54', '+0.56' ), '1.6', $noMargin ],
150
			'1.55+/-1' => [ QuantityValue::newFromNumber( '+1.55', '1', '+2.55', '+0.55' ), '2', $noMargin ],
151
			// FIXME: We should probably never round to zero as it is confusing.
152
			'1.55+/-10' => [ QuantityValue::newFromNumber( '+1.55', '1', '+11.55', '-8.45' ), '0', $noMargin ],
153
154
			// Do not mess with the value when the margin is rendered
155
			[ QuantityValue::newFromNumber( '+1500', '1', '+2500', '+500' ), '1500±1000' ],
156
			[ QuantityValue::newFromNumber( '+2', '1', '+2.005', '+1.995' ), '2±0.005' ],
157
			[ QuantityValue::newFromNumber( '+1.5', '1', '+2.5', '+0.5' ), '1.5±1' ],
158
			[ QuantityValue::newFromNumber( '+1.0005', '1', '+1.0015', '+0.9995' ), '1.0005±0.001' ],
159
			[ QuantityValue::newFromNumber( '+0.0015', '1', '+0.0025', '+0.0005' ), '0.0015±0.001' ],
160
161
			/**
162
			 * Never mess with the margin
163
			 * @see https://phabricator.wikimedia.org/T58892
164
			 */
165
			[ QuantityValue::newFromNumber( '+2', '1', '+3.5', '+0.5' ), '2±1.5' ],
166
			[ QuantityValue::newFromNumber( '+2', '1', '+2.016', '+1.984' ), '2±0.016' ],
167
			[ QuantityValue::newFromNumber( '+2', '1', '+2.0015', '+1.9985' ), '2±0.0015' ],
168
			[ QuantityValue::newFromNumber( '+0.0015', '1', '+0.003', '+0' ), '0.0015±0.0015' ],
169
			[ QuantityValue::newFromNumber( '+2.0011', '1', '+2.0022', '+2' ), '2.0011±0.0011' ],
170
			[ QuantityValue::newFromNumber( '+2.0099', '1', '+2.0198', '+2' ), '2.0099±0.0099' ],
171
172
			// IEEE edge cases
173
			[
174
				QuantityValue::newFromNumber(
175
					'+1.00000000000000015',
176
					'1',
177
					'+1.00000000000000025',
178
					'+1.00000000000000005'
179
				),
180
				'1.00000000000000015±0.0000000000000001'
181
			],
182
			'0.2 / 3 * 3' => [
183
				QuantityValue::newFromNumber(
184
					'+0.2000000000000000111',
185
					'1',
186
					'+0.2000000000000000111',
187
					'+0.2000000000000000111'
188
				),
189
				'0.2000000000000000111±0'
190
			],
191
			'8 - 6.4' => [
192
				QuantityValue::newFromNumber(
193
					'+1.59999999999999964473',
194
					'1',
195
					'+1.59999999999999964473',
196
					'+1.59999999999999964473'
197
				),
198
				'1.59999999999999964473±0'
199
			],
200
		];
201
	}
202
203
	public function testFormatWithFormatString() {
204
		$formatter = $this->getQuantityFormatter( null, '<$2>$1' );
205
		$value = UnboundedQuantityValue::newFromNumber( '+5', 'USD' );
206
		$formatted = $formatter->format( $value );
207
		$this->assertSame( '<USD>5', $formatted );
208
	}
209
210
}
211