DecimalFormatterTest::getInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ValueFormatters\Test;
4
5
use DataValues\DecimalValue;
6
use PHPUnit\Framework\TestCase;
7
use ValueFormatters\DecimalFormatter;
8
use ValueFormatters\FormatterOptions;
9
use ValueFormatters\NumberLocalizer;
10
11
/**
12
 * @covers ValueFormatters\DecimalFormatter
13
 *
14
 * @group ValueFormatters
15
 * @group DataValueExtensions
16
 *
17
 * @license GPL-2.0-or-later
18
 * @author Daniel Kinzler
19
 */
20
class DecimalFormatterTest extends TestCase {
21
22
	/**
23
	 * @see ValueFormatterTestBase::getInstance
24
	 *
25
	 * @param FormatterOptions|null $options
26
	 *
27
	 * @return DecimalFormatter
28
	 */
29
	protected function getInstance( FormatterOptions $options = null ) {
30
		return new DecimalFormatter( $options );
31
	}
32
33
	/**
34
	 * @see ValueFormatterTestBase::validProvider
35
	 */
36
	public function validProvider() {
37
		$optionsForceSign = new FormatterOptions( [
38
			DecimalFormatter::OPT_FORCE_SIGN => true
39
		] );
40
41
		$decimals = [
42
			'+0' => [ '0', null ],
43
			'+0.0' => [ '0.0', null ],
44
			'-0.0130' => [ '-0.0130', null ],
45
			'+10000.013' => [ '10000.013', null ],
46
			'+20000.4' => [ '+20000.4', $optionsForceSign ],
47
			'-12' => [ '-12', null ]
48
		];
49
50
		$argLists = [];
51
		foreach ( $decimals as $input => $args ) {
52
			$inputValue = new DecimalValue( $input );
53
54
			$argLists[$input] = array_merge( [ $inputValue ], $args );
55
		}
56
57
		return $argLists;
58
	}
59
60
	public function testLocalization() {
61
		$localizer = $this->createMock( NumberLocalizer::class );
62
63
		$localizer->expects( $this->once() )
64
			->method( 'localizeNumber' )
65
			->will( $this->returnCallback( function ( $number ) {
66
				return "n:$number";
67
			} ) );
68
69
		$value = new DecimalValue( '+12345' );
70
		$formatter = new DecimalFormatter( null, $localizer );
0 ignored issues
show
Documentation introduced by
$localizer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<ValueFormatters\NumberLocalizer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
72
		$this->assertSame( 'n:12345', $formatter->format( $value ) );
73
	}
74
75
}
76