QuantityFormatterTest::validProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 140

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 140
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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