Passed
Push — master ( 3c7afe...11f0b5 )
by
unknown
14:12 queued 14s
created

NumberFormatTest::providerNoScientific()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 18
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Style;
6
7
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
8
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
9
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
10
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\NumberFormatter;
11
use PHPUnit\Framework\Attributes\DataProvider;
12
use PHPUnit\Framework\TestCase;
13
14
class NumberFormatTest extends TestCase
15
{
16
    private string $compatibilityMode;
17
18
    protected function setUp(): void
19
    {
20
        StringHelper::setDecimalSeparator('.');
21
        StringHelper::setThousandsSeparator(',');
22
        $this->compatibilityMode = Functions::getCompatibilityMode();
23
    }
24
25
    protected function tearDown(): void
26
    {
27
        StringHelper::setCurrencyCode(null);
28
        StringHelper::setDecimalSeparator(null);
29
        StringHelper::setThousandsSeparator(null);
30
        Functions::setCompatibilityMode($this->compatibilityMode);
31
    }
32
33
    /**
34
     * @param null|bool|float|int|string $args string to be formatted
35
     */
36
    #[DataProvider('providerNumberFormat')]
37
    public function testFormatValueWithMask(mixed $expectedResult, mixed ...$args): void
38
    {
39
        $result = NumberFormat::toFormattedString(...$args);
40
        self::assertSame($expectedResult, $result);
41
    }
42
43
    public static function providerNumberFormat(): array
44
    {
45
        return require 'tests/data/Style/NumberFormat.php';
46
    }
47
48
    /**
49
     * @param null|bool|float|int|string $args string to be formatted
50
     */
51
    #[DataProvider('providerNumberFormatFractions')]
52
    public function testFormatValueWithMaskFraction(mixed $expectedResult, mixed ...$args): void
53
    {
54
        $result = NumberFormat::toFormattedString(...$args);
55
        self::assertEquals($expectedResult, $result);
56
    }
57
58
    public static function providerNumberFormatFractions(): array
59
    {
60
        return require 'tests/data/Style/NumberFormatFractions.php';
61
    }
62
63
    /**
64
     * @param null|bool|float|int|string $args string to be formatted
65
     */
66
    #[DataProvider('providerNumberFormatDates')]
67
    public function testFormatValueWithMaskDate(mixed $expectedResult, mixed ...$args): void
68
    {
69
        $result = NumberFormat::toFormattedString(...$args);
70
        self::assertEquals($expectedResult, $result);
71
    }
72
73
    public static function providerNumberFormatDates(): array
74
    {
75
        return require 'tests/data/Style/NumberFormatDates.php';
76
    }
77
78
    public function testDatesOpenOfficeGnumericNonPositive(): void
79
    {
80
        Functions::setCompatibilityMode(
81
            Functions::COMPATIBILITY_OPENOFFICE
82
        );
83
        $fmt1 = 'yyyy-mm-dd';
84
        $rslt = NumberFormat::toFormattedString(0, $fmt1);
85
        self::assertSame('1899-12-30', $rslt);
86
        $rslt = NumberFormat::toFormattedString(-2, $fmt1);
87
        self::assertSame('1899-12-28', $rslt);
88
        $rslt = NumberFormat::toFormattedString(-2.4, $fmt1);
89
        self::assertSame('1899-12-27', $rslt);
90
        $fmt2 = 'yyyy-mm-dd hh:mm:ss AM/PM';
91
        $rslt = NumberFormat::toFormattedString(-2.4, $fmt2);
92
        self::assertSame('1899-12-27 02:24:00 PM', $rslt);
93
    }
94
95
    public function testCurrencyCode(): void
96
    {
97
        // "Currency symbol" replaces $ in some cases, not in others
98
        $cur = StringHelper::getCurrencyCode();
99
        StringHelper::setCurrencyCode('€');
100
        $fmt1 = '#,##0.000\ [$]';
101
        $rslt = NumberFormat::toFormattedString(12345.679, $fmt1);
102
        self::assertEquals($rslt, '12,345.679 €');
103
        $fmt2 = '$ #,##0.000';
104
        $rslt = NumberFormat::toFormattedString(12345.679, $fmt2);
105
        self::assertEquals($rslt, '$ 12,345.679');
106
        StringHelper::setCurrencyCode($cur);
107
    }
108
109
    #[DataProvider('providerNoScientific')]
110
    public function testNoScientific(string $expectedResult, string $numericString): void
111
    {
112
        $result = NumberFormatter::floatStringConvertScientific($numericString);
113
        self::assertSame($expectedResult, $result);
114
    }
115
116
    public static 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