|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueFormatters\Test; |
|
4
|
|
|
|
|
5
|
|
|
use DataValues\DecimalValue; |
|
6
|
|
|
use ValueFormatters\DecimalFormatter; |
|
7
|
|
|
use ValueFormatters\FormatterOptions; |
|
8
|
|
|
use ValueFormatters\NumberLocalizer; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers ValueFormatters\DecimalFormatter |
|
12
|
|
|
* |
|
13
|
|
|
* @group ValueFormatters |
|
14
|
|
|
* @group DataValueExtensions |
|
15
|
|
|
* |
|
16
|
|
|
* @license GPL-2.0+ |
|
17
|
|
|
* @author Daniel Kinzler |
|
18
|
|
|
*/ |
|
19
|
|
|
class DecimalFormatterTest extends ValueFormatterTestBase { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @see ValueFormatterTestBase::getInstance |
|
23
|
|
|
* |
|
24
|
|
|
* @param FormatterOptions|null $options |
|
25
|
|
|
* |
|
26
|
|
|
* @return DecimalFormatter |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function getInstance( FormatterOptions $options = null ) { |
|
29
|
|
|
return new DecimalFormatter( $options ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @see ValueFormatterTestBase::validProvider |
|
34
|
|
|
*/ |
|
35
|
|
|
public function validProvider() { |
|
36
|
|
|
$optionsForceSign = new FormatterOptions( [ |
|
37
|
|
|
DecimalFormatter::OPT_FORCE_SIGN => true |
|
38
|
|
|
] ); |
|
39
|
|
|
|
|
40
|
|
|
$decimals = [ |
|
41
|
|
|
'+0' => [ '0', null ], |
|
42
|
|
|
'+0.0' => [ '0.0', null ], |
|
43
|
|
|
'-0.0130' => [ '-0.0130', null ], |
|
44
|
|
|
'+10000.013' => [ '10000.013', null ], |
|
45
|
|
|
'+20000.4' => [ '+20000.4', $optionsForceSign ], |
|
46
|
|
|
'-12' => [ '-12', null ] |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
$argLists = []; |
|
50
|
|
|
foreach ( $decimals as $input => $args ) { |
|
51
|
|
|
$inputValue = new DecimalValue( $input ); |
|
52
|
|
|
|
|
53
|
|
|
$argLists[$input] = array_merge( [ $inputValue ], $args ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $argLists; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testLocalization() { |
|
60
|
|
|
$localizer = $this->getMock( NumberLocalizer::class ); |
|
61
|
|
|
|
|
62
|
|
|
$localizer->expects( $this->once() ) |
|
63
|
|
|
->method( 'localizeNumber' ) |
|
64
|
|
|
->will( $this->returnCallback( function ( $number ) { |
|
65
|
|
|
return "n:$number"; |
|
66
|
|
|
} ) ); |
|
67
|
|
|
|
|
68
|
|
|
$value = new DecimalValue( '+12345' ); |
|
69
|
|
|
$formatter = new DecimalFormatter( null, $localizer ); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertSame( 'n:12345', $formatter->format( $value ) ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|