Completed
Push — BoundedQuantityValue ( 1533af )
by Daniel
02:40
created

tests/ValueFormatters/QuantityFormatterTest.php (16 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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(
96
			'+0/nm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $noMargin ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '+0'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
97
			'+0/wm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $withMargin ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '+0'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
98
99
			'+0.0/nm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0 °', $noMargin ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '+0.1'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
			'+0.0/wm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0±0.1 °', $withMargin ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '+0.1'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
101
			'+0.0/xr' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.00±0.10 °', $exactRounding ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '+0.1'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
102
103
			'-1205/nm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1200 m', $noMargin ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '-1105'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
104
			'-1205/wm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1200±100 m', $withMargin ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '-1105'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
105
			'-1205/nr' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $noRounding ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '-1105'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
106
			'-1205/xr' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205.00±100.00 m', $exactRounding ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '-1105'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
107
			'-1205/nu' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1200±100', $noUnit ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '-1105'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
108
109
			'+3.025/nm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025', $noMargin ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '+3.02744'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
110
			'+3.025/wm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025±0.004', $withMargin ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '+3.02744'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '+3.02744'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
112
113
			'+3.125/nr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.125±0.125', $noRounding ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '+3.2'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
114
			'+3.125/xr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.13±0.13', $exactRounding ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '+3.2'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
116
			'+3.125/fs' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '+3.13', $forceSign ),
0 ignored issues
show
The call to QuantityValue::newFromNumber() has too many arguments starting with '+3.2'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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