Completed
Push — master ( ee11b3...9e15b9 )
by adam
02:46
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 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
 * @licence GNU GPL v2+
17
 * @author Daniel Kinzler
18
 */
19
class QuantityFormatterTest extends ValueFormatterTestBase {
20
21
	/**
22
	 * @deprecated since 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' => ... '+3.13', $forceSign)); (array<string,array<strin...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...
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±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' ), '-1200±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' ), '-1200±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.004', $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
	}
119
120
	public function testFormatWithFormatString() {
121
		$formatter = $this->getQuantityFormatter( null, '<$2>$1' );
122
		$value = QuantityValue::newFromNumber( '+5', 'USD' );
123
		$formatted = $formatter->format( $value );
124
		$this->assertSame( '<USD>5', $formatted );
125
	}
126
127
}
128