Completed
Push — BoundedQuantityValue ( 544562...86948c )
by Daniel
05:19 queued 02:55
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
71
				=> QuantityFormatter::SHOW_UNCERTAINTY_MARGIN_NEVER
72
		) );
73
74
		$withMargin = new FormatterOptions( array(
75
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN
76
				=> QuantityFormatter::SHOW_UNCERTAINTY_MARGIN_IF_KNOWN
77
		) );
78
79
		$withNonZeroMargin = new FormatterOptions( array(
80
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN
81
			=> QuantityFormatter::SHOW_UNCERTAINTY_MARGIN_IF_NOT_ZERO
82
		) );
83
84
		$noRounding = new FormatterOptions( array(
85
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN
86
				=> QuantityFormatter::SHOW_UNCERTAINTY_MARGIN_IF_KNOWN,
87
			QuantityFormatter::OPT_APPLY_ROUNDING => false
88
		) );
89
90
		$exactRounding = new FormatterOptions( array(
91
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN
92
				=> QuantityFormatter::SHOW_UNCERTAINTY_MARGIN_IF_KNOWN,
93
			QuantityFormatter::OPT_APPLY_ROUNDING => -2
94
		) );
95
96
		$forceSign = new FormatterOptions( array(
97
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN
98
				=> QuantityFormatter::SHOW_UNCERTAINTY_MARGIN_NEVER,
99
			DecimalFormatter::OPT_FORCE_SIGN => true,
100
		) );
101
102
		$noUnit = new FormatterOptions( array(
103
			QuantityFormatter::OPT_APPLY_UNIT => false,
104
		) );
105
106
		return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('+0/nm' => ... (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...
107
			'+0/nm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $noMargin ),
108
			'+0/wm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0±0', $withMargin ),
109
			'+0/zm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $withNonZeroMargin ),
110
111
			'+0.0/nm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0 °', $noMargin ),
112
			'+0.0/wm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0±0.1 °', $withMargin ),
113
			'+0.0/xr' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.00±0.10 °', $exactRounding ),
114
115
			'-1205/nm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1200 m', $noMargin ),
116
			'-1205/wm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $withMargin ),
117
			'-1205/nr' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $noRounding ),
118
			'-1205/xr' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205.00±100.00 m', $exactRounding ),
119
			'-1205/nu' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100', $noUnit ),
120
121
			'+3.025/nm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025', $noMargin ),
122
			'+3.025/wm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025±0.0039', $withMargin ),
123
			'+3.025/zm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025±0.004', $withNonZeroMargin ),
124
			'+3.025/xr' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.03±0.00', $exactRounding ), // TODO: never round to 0! See bug #56892
125
126
			'+3.125/nr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.125±0.125', $noRounding ),
127
			'+3.125/xr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.13±0.13', $exactRounding ),
128
129
			'+3.125/fs' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '+3.13', $forceSign ),
130
131
			'UB: +0.0/nm' => array( UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $noMargin ),
132
			'UB: +0.0/wm' => array( UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $withMargin ),
133
			'UB: +0.0/zm' => array( UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.0 °', $withNonZeroMargin ),
134
			'UB: +0.0/xr' => array( UnboundedQuantityValue::newFromNumber( '+0.0', '°' ), '0.00 °', $exactRounding ),
135
			'UB: +5.020/nm' => array( UnboundedQuantityValue::newFromNumber( '+5.020', '°' ), '5.020 °', $noMargin ),
136
			'UB: +5.020/wm' => array( UnboundedQuantityValue::newFromNumber( '+5.020', '°' ), '5.020 °', $withMargin ),
137
			'UB: +5.020/zm' => array( UnboundedQuantityValue::newFromNumber( '+5.020', '°' ), '5.020 °', $withNonZeroMargin ),
138
			'UB: +5.020/xr' => array( UnboundedQuantityValue::newFromNumber( '+5.020', '°' ), '5.02 °', $exactRounding ),
139
			'UB: +3.125/fs' => array( UnboundedQuantityValue::newFromNumber( '+3.125', '1' ), '+3.125', $forceSign ),
140
141
			// Rounding with a fixed +/-1 margin
142
			array( QuantityValue::newFromNumber( '+1.44', '1', '+2.44', '+0.44' ), '1', $noMargin ),
143
			// FIXME: Rounding this up is just wrong.
144
			array( QuantityValue::newFromNumber( '+1.45', '1', '+2.45', '+0.45' ), '2', $noMargin ),
145
			// FIXME: Rounding this up is just wrong.
146
			array( QuantityValue::newFromNumber( '+1.49', '1', '+2.49', '+0.49' ), '2', $noMargin ),
147
			array( QuantityValue::newFromNumber( '+1.50', '1', '+2.50', '+0.50' ), '2', $noMargin ),
148
			array( QuantityValue::newFromNumber( '+2.50', '1', '+3.50', '+1.50' ), '3', $noMargin ),
149
150
			// Rounding with different margins
151
			'1.55+/-0.09' => array( QuantityValue::newFromNumber( '+1.55', '1', '+1.64', '+1.46' ), '1.55', $noMargin ),
152
			'1.55+/-0.1' => array( QuantityValue::newFromNumber( '+1.55', '1', '+1.65', '+1.45' ), '1.6', $noMargin ),
153
			'1.55+/-0.49' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.04', '+1.06' ), '1.6', $noMargin ),
154
			'1.55+/-0.5' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.05', '+1.05' ), '1.6', $noMargin ),
155
			'1.55+/-0.99' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.54', '+0.56' ), '1.6', $noMargin ),
156
			'1.55+/-1' => array( 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' => array( 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
			array( QuantityValue::newFromNumber( '+1500', '1', '+2500', '+500' ), '1500±1000' ),
162
			array( QuantityValue::newFromNumber( '+2', '1', '+2.005', '+1.995' ), '2±0.005' ),
163
			array( QuantityValue::newFromNumber( '+1.5', '1', '+2.5', '+0.5' ), '1.5±1' ),
164
			array( QuantityValue::newFromNumber( '+1.0005', '1', '+1.0015', '+0.9995' ), '1.0005±0.001' ),
165
			array( QuantityValue::newFromNumber( '+0.0015', '1', '+0.0025', '+0.0005' ), '0.0015±0.001' ),
166
167
			// Never mess with the margin
168
			array( QuantityValue::newFromNumber( '+2', '1', '+3.5', '+0.5' ), '2±1.5' ),
169
			array( QuantityValue::newFromNumber( '+2', '1', '+2.0015', '+1.9985' ), '2±0.0015' ),
170
			array( QuantityValue::newFromNumber( '+0.0015', '1', '+0.003', '+0' ), '0.0015±0.0015' ),
171
			array( QuantityValue::newFromNumber( '+2.0011', '1', '+2.0022', '+2' ), '2.0011±0.0011' ),
172
			array( QuantityValue::newFromNumber( '+2.0099', '1', '+2.0198', '+2' ), '2.0099±0.0099' ),
173
			array(
174
				QuantityValue::newFromNumber(
175
					'+1.00000000000000015',
176
					'1',
177
					'+1.00000000000000025',
178
					'+1.00000000000000005'
179
				),
180
				'1.00000000000000015±0.0000000000000001'
181
			),
182
		);
183
	}
184
185
	public function testFormatWithFormatString() {
186
		$formatter = $this->getQuantityFormatter( null, '<$2>$1' );
187
		$value = UnboundedQuantityValue::newFromNumber( '+5', 'USD' );
188
		$formatted = $formatter->format( $value );
189
		$this->assertSame( '<USD>5', $formatted );
190
	}
191
192
}
193