Test Failed
Push — httpUnits ( 76c237...022822 )
by no
02:53 queued 40s
created

tests/ValueFormatters/QuantityFormatterTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
	 * @deprecated since DataValues Interfaces 0.2, just use getInstance.
25
	 */
26
	protected function getFormatterClass() {
27
		throw new \LogicException( 'Should not be called, use getInstance' );
28
	}
29
30
	/**
31
	 * @see ValueFormatterTestBase::getInstance
32
	 *
33
	 * @param FormatterOptions|null $options
34
	 *
35
	 * @return QuantityFormatter
36
	 */
37
	protected function getInstance( FormatterOptions $options = null ) {
38
		return $this->getQuantityFormatter( $options );
39
	}
40
41
	/**
42
	 * @param FormatterOptions|null $options
43
	 * @param string|null $quantityWithUnitFormat
44
	 *
45
	 * @return QuantityFormatter
46
	 */
47
	private function getQuantityFormatter(
48
		FormatterOptions $options = null,
49
		$quantityWithUnitFormat = null
50
	) {
51
		$vocabularyUriFormatter = $this->getMock( ValueFormatter::class );
52
		$vocabularyUriFormatter->expects( $this->any() )
53
			->method( 'format' )
54
			->will( $this->returnCallback( function( $unit ) {
55
				return $unit === '1' ? null : $unit;
56
			} ) );
57
58
		return new QuantityFormatter(
59
			$options,
60
			new DecimalFormatter( $options ),
61
			$vocabularyUriFormatter,
62
			$quantityWithUnitFormat
63
		);
64
	}
65
66
	/**
67
	 * @see ValueFormatterTestBase::validProvider
68
	 */
69
	public function validProvider() {
70
		$noMargin = new FormatterOptions( array(
71
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false
72
		) );
73
74
		$withMargin = new FormatterOptions( array(
75
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => true
76
		) );
77
78
		$noRounding = new FormatterOptions( array(
79
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => true,
80
			QuantityFormatter::OPT_APPLY_ROUNDING => false
81
		) );
82
83
		$exactRounding = new FormatterOptions( array(
84
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false,
85
			QuantityFormatter::OPT_APPLY_ROUNDING => -2
86
		) );
87
88
		$forceSign = new FormatterOptions( array(
89
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false,
90
			DecimalFormatter::OPT_FORCE_SIGN => true,
91
		) );
92
93
		$noUnit = new FormatterOptions( array(
94
			QuantityFormatter::OPT_APPLY_UNIT => false,
95
		) );
96
97
		return array(
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...
98
			'+0/nm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $noMargin ),
99
			'+0/wm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0±0', $withMargin ),
100
101
			'+0.0/nm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0 °', $noMargin ),
102
			'+0.0/wm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0±0.1 °', $withMargin ),
103
			'+0.0/xr' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0 °', $exactRounding ),
104
105
			'-1205/nm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1200 m', $noMargin ),
106
			'-1205/wm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $withMargin ),
107
			'-1205/nr' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $noRounding ),
108
			'-1205/xr' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205 m', $exactRounding ),
109
			'-1205/nu' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100', $noUnit ),
110
111
			'+3.025/nm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025', $noMargin ),
112
			'+3.025/wm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025±0.0039', $withMargin ),
113
			'+3.025/xr' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.03', $exactRounding ),
114
115
			'+3.125/nr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.125±0.125', $noRounding ),
116
			'+3.125/xr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.13', $exactRounding ),
117
118
			'+3.125/fs' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '+3.13', $forceSign ),
119
120
			// Unbounded quantities with different options
121
			'UB: +0.0/nm' => array( UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $noMargin ),
122
			'UB: +0.0/wm' => array( UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $withMargin ),
123
			'UB: +0.0/xr' => array( UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $exactRounding ),
124
			'UB: +5.021/nm' => array( UnboundedQuantityValue::newFromNumber( '+5.021', '°' ), '5.021 °', $noMargin ),
125
			'UB: +5.021/wm' => array( UnboundedQuantityValue::newFromNumber( '+5.021', '°' ), '5.021 °', $withMargin ),
126
			'UB: +5.021/xr' => array( UnboundedQuantityValue::newFromNumber( '+5.021', '°' ), '5.02 °', $exactRounding ),
127
			'UB: +3.125/fs' => array( UnboundedQuantityValue::newFromNumber( '+3.125', '1' ), '+3.125', $forceSign ),
128
129
			// Unbounded quantities with enforced, exact rounding
130
			array( UnboundedQuantityValue::newFromNumber( '+0.00155', '1' ), '0.00', $exactRounding ),
131
			array( UnboundedQuantityValue::newFromNumber( '+0.0155', '1' ), '0.02', $exactRounding ),
132
			array( UnboundedQuantityValue::newFromNumber( '+0.155', '1' ), '0.16', $exactRounding ),
133
			array( UnboundedQuantityValue::newFromNumber( '+1.55', '1' ), '1.55', $exactRounding ),
134
			array( UnboundedQuantityValue::newFromNumber( '+15.5', '1' ), '15.5', $exactRounding ),
135
			array( UnboundedQuantityValue::newFromNumber( '+155', '1' ), '155', $exactRounding ),
136
137
			// Default options with different margins
138
			'24+-000.01' => array( QuantityValue::newFromNumber( '+24', '1', '+24.01', '+23.99' ), '24±0.01' ),
139
			'24+-000.10' => array( QuantityValue::newFromNumber( '+24', '1', '+24.1', '+23.9' ), '24±0.1' ),
140
			'24+-001.00' => array( QuantityValue::newFromNumber( '+24', '1', '+25', '+23' ), '24±1' ),
141
			'24+-010.00' => array( QuantityValue::newFromNumber( '+24', '1', '+34', '+14' ), '24±10' ),
142
			'24+-100.00' => array( QuantityValue::newFromNumber( '+24', '1', '+124', '-76' ), '24±100' ),
143
144
			// Rounding with a fixed +/-1 margin
145
			array( QuantityValue::newFromNumber( '+1.44', '1', '+2.44', '+0.44' ), '1', $noMargin ),
146
			array( QuantityValue::newFromNumber( '+1.45', '1', '+2.45', '+0.45' ), '1', $noMargin ),
147
			array( QuantityValue::newFromNumber( '+1.49', '1', '+2.49', '+0.49' ), '1', $noMargin ),
148
			array( QuantityValue::newFromNumber( '+1.50', '1', '+2.50', '+0.50' ), '2', $noMargin ),
149
			array( QuantityValue::newFromNumber( '+2.50', '1', '+3.50', '+1.50' ), '3', $noMargin ),
150
151
			// Rounding with different margins
152
			'1.55+/-0.09' => array( QuantityValue::newFromNumber( '+1.55', '1', '+1.64', '+1.46' ), '1.55', $noMargin ),
153
			'1.55+/-0.1' => array( QuantityValue::newFromNumber( '+1.55', '1', '+1.65', '+1.45' ), '1.6', $noMargin ),
154
			'1.55+/-0.49' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.04', '+1.06' ), '1.6', $noMargin ),
155
			'1.55+/-0.5' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.05', '+1.05' ), '1.6', $noMargin ),
156
			'1.55+/-0.99' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.54', '+0.56' ), '1.6', $noMargin ),
157
			'1.55+/-1' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.55', '+0.55' ), '2', $noMargin ),
158
			// FIXME: We should probably never round to zero as it is confusing.
159
			'1.55+/-10' => array( QuantityValue::newFromNumber( '+1.55', '1', '+11.55', '-8.45' ), '0', $noMargin ),
160
161
			// Do not mess with the value when the margin is rendered
162
			array( QuantityValue::newFromNumber( '+1500', '1', '+2500', '+500' ), '1500±1000' ),
163
			array( QuantityValue::newFromNumber( '+2', '1', '+2.005', '+1.995' ), '2±0.005' ),
164
			array( QuantityValue::newFromNumber( '+1.5', '1', '+2.5', '+0.5' ), '1.5±1' ),
165
			array( QuantityValue::newFromNumber( '+1.0005', '1', '+1.0015', '+0.9995' ), '1.0005±0.001' ),
166
			array( QuantityValue::newFromNumber( '+0.0015', '1', '+0.0025', '+0.0005' ), '0.0015±0.001' ),
167
168
			/**
169
			 * Never mess with the margin
170
			 * @see https://phabricator.wikimedia.org/T58892
171
			 */
172
			array( QuantityValue::newFromNumber( '+2', '1', '+3.5', '+0.5' ), '2±1.5' ),
173
			array( QuantityValue::newFromNumber( '+2', '1', '+2.016', '+1.984' ), '2±0.016' ),
174
			array( QuantityValue::newFromNumber( '+2', '1', '+2.0015', '+1.9985' ), '2±0.0015' ),
175
			array( QuantityValue::newFromNumber( '+0.0015', '1', '+0.003', '+0' ), '0.0015±0.0015' ),
176
			array( QuantityValue::newFromNumber( '+2.0011', '1', '+2.0022', '+2' ), '2.0011±0.0011' ),
177
			array( QuantityValue::newFromNumber( '+2.0099', '1', '+2.0198', '+2' ), '2.0099±0.0099' ),
178
179
			// IEEE edge cases
180
			array(
181
				QuantityValue::newFromNumber(
182
					'+1.00000000000000015',
183
					'1',
184
					'+1.00000000000000025',
185
					'+1.00000000000000005'
186
				),
187
				'1.00000000000000015±0.0000000000000001'
188
			),
189
			'0.2 / 3 * 3' => array(
190
				QuantityValue::newFromNumber(
191
					'+0.2000000000000000111',
192
					'1',
193
					'+0.2000000000000000111',
194
					'+0.2000000000000000111'
195
				),
196
				'0.2000000000000000111±0'
197
			),
198
			'8 - 6.4' => array(
199
				QuantityValue::newFromNumber(
200
					'+1.59999999999999964473',
201
					'1',
202
					'+1.59999999999999964473',
203
					'+1.59999999999999964473'
204
				),
205
				'1.59999999999999964473±0'
206
			),
207
		);
208
	}
209
210
	public function testFormatWithFormatString() {
211
		$formatter = $this->getQuantityFormatter( null, '<$2>$1' );
212
		$value = UnboundedQuantityValue::newFromNumber( '+5', 'USD' );
213
		$formatted = $formatter->format( $value );
214
		$this->assertSame( '<USD>5', $formatted );
215
	}
216
217
}
218