Test Failed
Pull Request — master (#79)
by no
02:51 queued 26s
created

testFormatWithFormatString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 5
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
11
/**
12
 * @covers ValueFormatters\QuantityFormatter
13
 *
14
 * @group ValueFormatters
15
 * @group DataValueExtensions
16
 *
17
 * @license GPL-2.0+
18
 * @author Daniel Kinzler
19
 */
20
class QuantityFormatterTest extends ValueFormatterTestBase {
21
22
	/**
23
	 * @deprecated since DataValues Interfaces 0.2, just use getInstance.
24
	 */
25
	protected function getFormatterClass() {
26
		throw new \LogicException( 'Should not be called, use getInstance' );
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( 'ValueFormatters\ValueFormatter' );
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( array(
70
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false
71
		) );
72
73
		$withMargin = new FormatterOptions( array(
74
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => true
75
		) );
76
77
		$noRounding = new FormatterOptions( array(
78
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => true,
79
			QuantityFormatter::OPT_APPLY_ROUNDING => false
80
		) );
81
82
		$exactRounding = new FormatterOptions( array(
83
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false,
84
			QuantityFormatter::OPT_APPLY_ROUNDING => -2
85
		) );
86
87
		$forceSign = new FormatterOptions( array(
88
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false,
89
			DecimalFormatter::OPT_FORCE_SIGN => true,
90
		) );
91
92
		$noUnit = new FormatterOptions( array(
93
			QuantityFormatter::OPT_APPLY_UNIT => false,
94
		) );
95
96
		return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('+0/nm' => ...9999999999999964473')); (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' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $noMargin ),
98
			'+0/wm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $withMargin ),
99
100
			'+0.0/nm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0 °', $noMargin ),
101
			'+0.0/wm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0±0.1 °', $withMargin ),
102
			'+0.0/xr' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0 °', $exactRounding ),
103
104
			'-1205/nm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1200 m', $noMargin ),
105
			'-1205/wm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $withMargin ),
106
			'-1205/nr' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $noRounding ),
107
			'-1205/xr' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205 m', $exactRounding ),
108
			'-1205/nu' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100', $noUnit ),
109
110
			'+3.025/nm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025', $noMargin ),
111
			'+3.025/wm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025±0.0039', $withMargin ),
112
			'+3.025/xr' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.03', $exactRounding ),
113
114
			'+3.125/nr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.125±0.125', $noRounding ),
115
			'+3.125/xr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.13', $exactRounding ),
116
117
			'+3.125/fs' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '+3.13', $forceSign ),
118
119
			'UB: +0.0/nm' => array( UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $noMargin ),
120
			'UB: +0.0/wm' => array( UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $withMargin ),
121
			'UB: +0.0/xr' => array( UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $exactRounding ),
122
			'UB: +5.021/nm' => array( UnboundedQuantityValue::newFromNumber( '+5.021', '°' ), '5.021 °', $noMargin ),
123
			'UB: +5.021/wm' => array( UnboundedQuantityValue::newFromNumber( '+5.021', '°' ), '5.021 °', $withMargin ),
124
			'UB: +5.021/xr' => array( UnboundedQuantityValue::newFromNumber( '+5.021', '°' ), '5.02 °', $exactRounding ),
125
			'UB: +3.125/fs' => array( UnboundedQuantityValue::newFromNumber( '+3.125', '1' ), '+3.125', $forceSign ),
126
127
			// Exact rounding
128
			array( UnboundedQuantityValue::newFromNumber( '+0.00155', '1' ), '0.00', $exactRounding ),
129
			array( UnboundedQuantityValue::newFromNumber( '+0.0155', '1' ), '0.02', $exactRounding ),
130
			array( UnboundedQuantityValue::newFromNumber( '+0.155', '1' ), '0.16', $exactRounding ),
131
			array( UnboundedQuantityValue::newFromNumber( '+1.55', '1' ), '1.55', $exactRounding ),
132
			array( UnboundedQuantityValue::newFromNumber( '+15.5', '1' ), '15.5', $exactRounding ),
133
			array( UnboundedQuantityValue::newFromNumber( '+155', '1' ), '155', $exactRounding ),
134
135
			// Rounding with a fixed +/-1 margin
136
			array( QuantityValue::newFromNumber( '+1.44', '1', '+2.44', '+0.44' ), '1', $noMargin ),
137
			array( QuantityValue::newFromNumber( '+1.45', '1', '+2.45', '+0.45' ), '1', $noMargin ),
138
			array( QuantityValue::newFromNumber( '+1.49', '1', '+2.49', '+0.49' ), '1', $noMargin ),
139
			array( QuantityValue::newFromNumber( '+1.50', '1', '+2.50', '+0.50' ), '2', $noMargin ),
140
			array( QuantityValue::newFromNumber( '+2.50', '1', '+3.50', '+1.50' ), '3', $noMargin ),
141
142
			// Rounding with different margins
143
			'1.55+/-0.09' => array( QuantityValue::newFromNumber( '+1.55', '1', '+1.64', '+1.46' ), '1.55', $noMargin ),
144
			'1.55+/-0.1' => array( QuantityValue::newFromNumber( '+1.55', '1', '+1.65', '+1.45' ), '1.6', $noMargin ),
145
			'1.55+/-0.49' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.04', '+1.06' ), '1.6', $noMargin ),
146
			'1.55+/-0.5' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.05', '+1.05' ), '1.6', $noMargin ),
147
			'1.55+/-0.99' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.54', '+0.56' ), '1.6', $noMargin ),
148
			'1.55+/-1' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.55', '+0.55' ), '2', $noMargin ),
149
			// FIXME: We should probably never round to zero as it is confusing.
150
			'1.55+/-10' => array( QuantityValue::newFromNumber( '+1.55', '1', '+11.55', '-8.45' ), '0', $noMargin ),
151
152
			// Do not mess with the value when the margin is rendered
153
			array( QuantityValue::newFromNumber( '+1500', '1', '+2500', '+500' ), '1500±1000' ),
154
			array( QuantityValue::newFromNumber( '+2', '1', '+2.005', '+1.995' ), '2±0.005' ),
155
			array( QuantityValue::newFromNumber( '+1.5', '1', '+2.5', '+0.5' ), '1.5±1' ),
156
			array( QuantityValue::newFromNumber( '+1.0005', '1', '+1.0015', '+0.9995' ), '1.0005±0.001' ),
157
			array( QuantityValue::newFromNumber( '+0.0015', '1', '+0.0025', '+0.0005' ), '0.0015±0.001' ),
158
159
			// Never mess with the margin
160
			array( QuantityValue::newFromNumber( '+2', '1', '+3.5', '+0.5' ), '2±1.5' ),
161
			array( QuantityValue::newFromNumber( '+2', '1', '+2.0015', '+1.9985' ), '2±0.0015' ),
162
			array( QuantityValue::newFromNumber( '+0.0015', '1', '+0.003', '+0' ), '0.0015±0.0015' ),
163
			array( QuantityValue::newFromNumber( '+2.0011', '1', '+2.0022', '+2' ), '2.0011±0.0011' ),
164
			array( QuantityValue::newFromNumber( '+2.0099', '1', '+2.0198', '+2' ), '2.0099±0.0099' ),
165
166
			// IEEE edge cases
167
			array(
168
				QuantityValue::newFromNumber(
169
					'+1.00000000000000015',
170
					'1',
171
					'+1.00000000000000025',
172
					'+1.00000000000000005'
173
				),
174
				'1.00000000000000015±0.0000000000000001'
175
			),
176
			'0.2 / 3 * 3' => array(
177
				QuantityValue::newFromNumber(
178
					'+0.2000000000000000111',
179
					'1',
180
					'+0.2000000000000000111',
181
					'+0.2000000000000000111'
182
				),
183
				'0.2000000000000000111'
184
			),
185
			'8 - 6.4' => array(
186
				QuantityValue::newFromNumber(
187
					'+1.59999999999999964473',
188
					'1',
189
					'+1.59999999999999964473',
190
					'+1.59999999999999964473'
191
				),
192
				'1.59999999999999964473'
193
			),
194
		);
195
	}
196
197
	public function testFormatWithFormatString() {
198
		$formatter = $this->getQuantityFormatter( null, '<$2>$1' );
199
		$value = QuantityValue::newFromNumber( '+5', 'USD' );
200
		$formatted = $formatter->format( $value );
201
		$this->assertSame( '<USD>5', $formatted );
202
	}
203
204
}
205