Completed
Pull Request — master (#66)
by Daniel
05:43 queued 02:32
created

QuantityFormatterTest::validProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 74
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 1 Features 1
Metric Value
c 9
b 1
f 1
dl 0
loc 74
rs 9.0335
cc 1
eloc 52
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
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' => ...'+3.125', $forceSign)); (array<string,array<DataV...ters\FormatterOptions>>) 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±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' ), '-1200±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' ), '-1200±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.004', $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
	}
142
143
	public function testFormatWithFormatString() {
144
		$formatter = $this->getQuantityFormatter( null, '<$2>$1' );
145
		$value = UnboundedQuantityValue::newFromNumber( '+5', 'USD' );
146
		$formatted = $formatter->format( $value );
147
		$this->assertSame( '<USD>5', $formatted );
148
	}
149
150
}
151