Passed
Pull Request — master (#3339)
by Mark
13:02
created

NumberFormatTest::providerNumberFormatFractions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Style;
4
5
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
6
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
7
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\NumberFormatter;
8
use PHPUnit\Framework\TestCase;
9
10
class NumberFormatTest extends TestCase
11
{
12
    /**
13
     * @var string
14
     */
15
    private $currencyCode;
16
17
    /**
18
     * @var string
19
     */
20
    private $decimalSeparator;
21
22
    /**
23
     * @var string
24
     */
25
    private $thousandsSeparator;
26
27
    protected function setUp(): void
28
    {
29
        $this->currencyCode = StringHelper::getCurrencyCode();
30
        $this->decimalSeparator = StringHelper::getDecimalSeparator();
31
        $this->thousandsSeparator = StringHelper::getThousandsSeparator();
32
        StringHelper::setDecimalSeparator('.');
33
        StringHelper::setThousandsSeparator(',');
34
    }
35
36
    protected function tearDown(): void
37
    {
38
        StringHelper::setCurrencyCode($this->currencyCode);
39
        StringHelper::setDecimalSeparator($this->decimalSeparator);
40
        StringHelper::setThousandsSeparator($this->thousandsSeparator);
41
    }
42
43
    /**
44
     * @dataProvider providerNumberFormat
45
     *
46
     * @param mixed $expectedResult
47
     */
48
    public function testFormatValueWithMask($expectedResult, ...$args): void
49
    {
50
        $result = NumberFormat::toFormattedString(...$args);
51
        self::assertSame($expectedResult, $result);
52
    }
53
54
    public function providerNumberFormat(): array
55
    {
56
        return require 'tests/data/Style/NumberFormat.php';
57
    }
58
59
    /**
60
     * @dataProvider providerNumberFormatFractions
61
     *
62
     * @param mixed $expectedResult
63
     * @param mixed $args
64
     */
65
    public function testFormatValueWithMaskFraction($expectedResult, ...$args): void
66
    {
67
        $result = NumberFormat::toFormattedString(...$args);
68
        self::assertEquals($expectedResult, $result);
69
    }
70
71
    public function providerNumberFormatFractions(): array
72
    {
73
        return require 'tests/data/Style/NumberFormatFractions.php';
74
    }
75
76
    /**
77
     * @dataProvider providerNumberFormatDates
78
     *
79
     * @param mixed $expectedResult
80
     * @param mixed $args
81
     */
82
    public function testFormatValueWithMaskDate($expectedResult, ...$args): void
83
    {
84
        $result = NumberFormat::toFormattedString(...$args);
85
        self::assertEquals($expectedResult, $result);
86
    }
87
88
    public function providerNumberFormatDates(): array
89
    {
90
        return require 'tests/data/Style/NumberFormatDates.php';
91
    }
92
93
    public function testCurrencyCode(): void
94
    {
95
        // "Currency symbol" replaces $ in some cases, not in others
96
        $cur = StringHelper::getCurrencyCode();
97
        StringHelper::setCurrencyCode('€');
98
        $fmt1 = '#,##0.000\ [$]';
99
        $rslt = NumberFormat::toFormattedString(12345.679, $fmt1);
100
        self::assertEquals($rslt, '12,345.679 €');
101
        $fmt2 = '$ #,##0.000';
102
        $rslt = NumberFormat::toFormattedString(12345.679, $fmt2);
103
        self::assertEquals($rslt, '$ 12,345.679');
104
        StringHelper::setCurrencyCode($cur);
105
    }
106
107
    /**
108
     * @dataProvider providerNoScientific
109
     */
110
    public function testNoScientific(string $expectedResult, string $numericString): void
111
    {
112
        $result = NumberFormatter::floatStringConvertScientific($numericString);
113
        self::assertSame($expectedResult, $result);
114
    }
115
116
    public function providerNoScientific(): array
117
    {
118
        return [
119
            'large number' => ['92' . str_repeat('0', 16), '9.2E+17'],
120
            'no decimal portion' => ['16', '1.6E1'],
121
            'retain decimal 0 if supplied in string' => ['16.0', '1.60E1'],
122
            'exponent 0' => ['2.3', '2.3E0'],
123
            'whole and decimal' => ['16.5', '1.65E1'],
124
            'plus signs' => ['165000', '+1.65E+5'],
125
            'e2 one decimal' => ['489.7', '4.897E2'],
126
            'e2 no decimal' => ['-489', '-4.89E2'],
127
            'e2 fill units position' => ['480', '4.8E+2'],
128
            'no scientific notation' => ['3.14159', '3.14159'],
129
            'non-zero in first decimal' => ['0.165', '1.65E-1'],
130
            'one leading zero in decimal' => ['0.0165', '1.65E-2'],
131
            'four leading zeros in decimal' => ['-0.0000165', '-1.65E-5'],
132
            'small number' => ['0.' . str_repeat('0', 16) . '1', '1E-17'],
133
            'very small number' => ['0.' . str_repeat('0', 69) . '1', '1E-70'],
134
        ];
135
    }
136
}
137