Completed
Push — master ( 80013f...49a5fb )
by Daniel
24s
created

QuantityFormatterTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 11
Bugs 2 Features 2
Metric Value
wmc 6
c 11
b 2
f 2
lcom 0
cbo 5
dl 0
loc 151
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormatterClass() 0 3 1
A getInstance() 0 3 1
A getQuantityFormatter() 0 18 2
B validProvider() 0 94 1
A testFormatWithFormatString() 0 6 1
1
<?php
2
3
namespace ValueFormatters\Test;
4
5
use DataValues\QuantityValue;
6
use ValueFormatters\DecimalFormatter;
7
use ValueFormatters\FormatterOptions;
8
use ValueFormatters\QuantityFormatter;
9
10
/**
11
 * @covers ValueFormatters\QuantityFormatter
12
 *
13
 * @group ValueFormatters
14
 * @group DataValueExtensions
15
 *
16
 * @license GPL-2.0+
17
 * @author Daniel Kinzler
18
 */
19
class QuantityFormatterTest extends ValueFormatterTestBase {
20
21
	/**
22
	 * @deprecated since DataValues Interfaces 0.2, just use getInstance.
23
	 */
24
	protected function getFormatterClass() {
25
		throw new \LogicException( 'Should not be called, use getInstance' );
26
	}
27
28
	/**
29
	 * @see ValueFormatterTestBase::getInstance
30
	 *
31
	 * @param FormatterOptions|null $options
32
	 *
33
	 * @return QuantityFormatter
34
	 */
35
	protected function getInstance( FormatterOptions $options = null ) {
36
		return $this->getQuantityFormatter( $options );
37
	}
38
39
	/**
40
	 * @param FormatterOptions|null $options
41
	 * @param string|null $quantityWithUnitFormat
42
	 *
43
	 * @return QuantityFormatter
44
	 */
45
	private function getQuantityFormatter(
46
		FormatterOptions $options = null,
47
		$quantityWithUnitFormat = null
48
	) {
49
		$vocabularyUriFormatter = $this->getMock( 'ValueFormatters\ValueFormatter' );
50
		$vocabularyUriFormatter->expects( $this->any() )
51
			->method( 'format' )
52
			->will( $this->returnCallback( function( $unit ) {
53
				return $unit === '1' ? null : $unit;
54
			} ) );
55
56
		return new QuantityFormatter(
57
			$options,
58
			new DecimalFormatter( $options ),
59
			$vocabularyUriFormatter,
60
			$quantityWithUnitFormat
61
		);
62
	}
63
64
	/**
65
	 * @see ValueFormatterTestBase::validProvider
66
	 */
67
	public function validProvider() {
68
		$noMargin = new FormatterOptions( array(
69
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false
70
		) );
71
72
		$withMargin = new FormatterOptions( array(
73
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => true
74
		) );
75
76
		$noRounding = new FormatterOptions( array(
77
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => true,
78
			QuantityFormatter::OPT_APPLY_ROUNDING => false
79
		) );
80
81
		$exactRounding = new FormatterOptions( array(
82
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => true,
83
			QuantityFormatter::OPT_APPLY_ROUNDING => -2
84
		) );
85
86
		$forceSign = new FormatterOptions( array(
87
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false,
88
			DecimalFormatter::OPT_FORCE_SIGN => true,
89
		) );
90
91
		$noUnit = new FormatterOptions( array(
92
			QuantityFormatter::OPT_APPLY_UNIT => false,
93
		) );
94
95
		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...
96
			'+0/nm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $noMargin ),
97
			'+0/wm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $withMargin ),
98
99
			'+0.0/nm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0 °', $noMargin ),
100
			'+0.0/wm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0±0.1 °', $withMargin ),
101
			'+0.0/xr' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.00±0.10 °', $exactRounding ),
102
103
			'-1205/nm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1200 m', $noMargin ),
104
			'-1205/wm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $withMargin ),
105
			'-1205/nr' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $noRounding ),
106
			'-1205/xr' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205.00±100.00 m', $exactRounding ),
107
			'-1205/nu' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100', $noUnit ),
108
109
			'+3.025/nm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025', $noMargin ),
110
			'+3.025/wm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025±0.0039', $withMargin ),
111
			'+3.025/xr' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.03', $exactRounding ), // TODO: never round to 0! See bug #56892
112
113
			'+3.125/nr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.125±0.125', $noRounding ),
114
			'+3.125/xr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.13±0.13', $exactRounding ),
115
116
			'+3.125/fs' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '+3.13', $forceSign ),
117
118
			// Rounding with a fixed +/-1 margin
119
			array( QuantityValue::newFromNumber( '+1.44', '1', '+2.44', '+0.44' ), '1', $noMargin ),
120
			// FIXME: Rounding this up is just wrong.
121
			array( QuantityValue::newFromNumber( '+1.45', '1', '+2.45', '+0.45' ), '2', $noMargin ),
122
			// FIXME: Rounding this up is just wrong.
123
			array( QuantityValue::newFromNumber( '+1.49', '1', '+2.49', '+0.49' ), '2', $noMargin ),
124
			array( QuantityValue::newFromNumber( '+1.50', '1', '+2.50', '+0.50' ), '2', $noMargin ),
125
			array( QuantityValue::newFromNumber( '+2.50', '1', '+3.50', '+1.50' ), '3', $noMargin ),
126
127
			// Rounding with different margins
128
			'1.55+/-0.09' => array( QuantityValue::newFromNumber( '+1.55', '1', '+1.64', '+1.46' ), '1.55', $noMargin ),
129
			'1.55+/-0.1' => array( QuantityValue::newFromNumber( '+1.55', '1', '+1.65', '+1.45' ), '1.6', $noMargin ),
130
			'1.55+/-0.49' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.04', '+1.06' ), '1.6', $noMargin ),
131
			'1.55+/-0.5' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.05', '+1.05' ), '1.6', $noMargin ),
132
			'1.55+/-0.99' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.54', '+0.56' ), '1.6', $noMargin ),
133
			'1.55+/-1' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.55', '+0.55' ), '2', $noMargin ),
134
			// FIXME: We should probably never round to zero as it is confusing.
135
			'1.55+/-10' => array( QuantityValue::newFromNumber( '+1.55', '1', '+11.55', '-8.45' ), '0', $noMargin ),
136
137
			// Do not mess with the value when the margin is rendered
138
			array( QuantityValue::newFromNumber( '+1500', '1', '+2500', '+500' ), '1500±1000' ),
139
			array( QuantityValue::newFromNumber( '+2', '1', '+2.005', '+1.995' ), '2±0.005' ),
140
			array( QuantityValue::newFromNumber( '+1.5', '1', '+2.5', '+0.5' ), '1.5±1' ),
141
			array( QuantityValue::newFromNumber( '+1.0005', '1', '+1.0015', '+0.9995' ), '1.0005±0.001' ),
142
			array( QuantityValue::newFromNumber( '+0.0015', '1', '+0.0025', '+0.0005' ), '0.0015±0.001' ),
143
144
			// Never mess with the margin
145
			array( QuantityValue::newFromNumber( '+2', '1', '+3.5', '+0.5' ), '2±1.5' ),
146
			array( QuantityValue::newFromNumber( '+2', '1', '+2.0015', '+1.9985' ), '2±0.0015' ),
147
			array( QuantityValue::newFromNumber( '+0.0015', '1', '+0.003', '+0' ), '0.0015±0.0015' ),
148
			array( QuantityValue::newFromNumber( '+2.0011', '1', '+2.0022', '+2' ), '2.0011±0.0011' ),
149
			array( QuantityValue::newFromNumber( '+2.0099', '1', '+2.0198', '+2' ), '2.0099±0.0099' ),
150
			array(
151
				QuantityValue::newFromNumber(
152
					'+1.00000000000000015',
153
					'1',
154
					'+1.00000000000000025',
155
					'+1.00000000000000005'
156
				),
157
				'1.00000000000000015±0.0000000000000001'
158
			),
159
		);
160
	}
161
162
	public function testFormatWithFormatString() {
163
		$formatter = $this->getQuantityFormatter( null, '<$2>$1' );
164
		$value = QuantityValue::newFromNumber( '+5', 'USD' );
165
		$formatted = $formatter->format( $value );
166
		$this->assertSame( '<USD>5', $formatted );
167
	}
168
169
}
170