Passed
Pull Request — master (#3384)
by Mark
16:40 queued 06:29
created

CurrencyTest::testCurrencyLocaleInvalidFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Style\NumberFormat\Wizard;
4
5
use NumberFormatter;
6
use PhpOffice\PhpSpreadsheet\Exception;
7
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\Currency;
8
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\Number;
9
use PHPUnit\Framework\TestCase;
10
11
class CurrencyTest extends TestCase
12
{
13
    /**
14
     * @dataProvider providerCurrency
15
     */
16
    public function testCurrency(
17
        string $expectedResult,
18
        string $currencyCode,
19
        int $decimals,
20
        bool $thousandsSeparator,
21
        bool $currencySymbolPosition,
22
        bool $currencySymbolSpacing
23
    ): void {
24
        $wizard = new Currency($currencyCode, $decimals, $thousandsSeparator, $currencySymbolPosition, $currencySymbolSpacing);
25
        self::assertSame($expectedResult, (string) $wizard);
26
    }
27
28
    public function providerCurrency(): array
29
    {
30
        return [
31
            ["\$\u{a0}0", '$', 0, Number::WITHOUT_THOUSANDS_SEPARATOR, Currency::LEADING_SYMBOL, Currency::SYMBOL_WITH_SPACING],
32
            ["\$\u{a0}#,##0", '$', 0, Number::WITH_THOUSANDS_SEPARATOR, Currency::LEADING_SYMBOL, Currency::SYMBOL_WITH_SPACING],
33
            ['$#,##0', '$', 0, Number::WITH_THOUSANDS_SEPARATOR, Currency::LEADING_SYMBOL, Currency::SYMBOL_WITHOUT_SPACING],
34
            ["0.00\u{a0}€", '€', 2, Number::WITHOUT_THOUSANDS_SEPARATOR, Currency::TRAILING_SYMBOL, Currency::SYMBOL_WITH_SPACING],
35
            ["#,##0.00\u{a0}€", '€', 2, Number::WITH_THOUSANDS_SEPARATOR, Currency::TRAILING_SYMBOL, Currency::SYMBOL_WITH_SPACING],
36
            ['0.00€', '€', 2, Number::WITHOUT_THOUSANDS_SEPARATOR, Currency::TRAILING_SYMBOL, Currency::SYMBOL_WITHOUT_SPACING],
37
        ];
38
    }
39
40
    /**
41
     * @dataProvider providerCurrencyLocale
42
     */
43
    public function testCurrencyLocale(
44
        string $expectedResult,
45
        string $currencyCode,
46
        string $locale
47
    ): void {
48
        if (class_exists(NumberFormatter::class) === false) {
49
            self::markTestSkipped('Intl extension is not available');
50
        }
51
52
        $wizard = new Currency($currencyCode);
53
        $wizard->setLocale($locale);
54
        self::assertSame($expectedResult, (string) $wizard);
55
    }
56
57
    public function providerCurrencyLocale(): array
58
    {
59
        return [
60
            ["[\$€-fy-NL]\u{a0}#,##0.00;[\$€-fy-NL]\u{a0}#,##0.00-", '€', 'fy-NL'], // Trailing negative
61
            ["[\$€-nl-NL]\u{a0}#,##0.00;[\$€-nl-NL]\u{a0}-#,##0.00", '€', 'nl-NL'], // Sign between currency and value
62
            ["[\$€-nl-BE]\u{a0}#,##0.00;[\$€-nl-BE]\u{a0}-#,##0.00", '€', 'NL-BE'], // Sign between currency and value
63
            ["#,##0.00\u{a0}[\$€-fr-BE]", '€', 'fr-be'],   // Trailing currency code
64
            ["#,##0.00\u{a0}[\$€-el-GR]", '€', 'el-gr'],   // Trailing currency code
65
            ['[$$-en-CA]#,##0.00', '$', 'en-ca'],
66
            ["#,##0.00\u{a0}[\$\$-fr-CA]", '$', 'fr-ca'],   // Trailing currency code
67
            ['[$¥-ja-JP]#,##0', '¥', 'ja-JP'], // No decimals
68
            ["#,##0.000\u{a0}[\$د.ب‎-ar-BH]", 'د.ب‎', 'ar-BH'],  // 3 decimals
69
        ];
70
    }
71
72
    /**
73
     * @dataProvider providerCurrencyLocaleNoDecimals
74
     */
75
    public function testCurrencyLocaleNoDecimals(
76
        string $expectedResult,
77
        string $currencyCode,
78
        string $locale
79
    ): void {
80
        if (class_exists(NumberFormatter::class) === false) {
81
            self::markTestSkipped('Intl extension is not available');
82
        }
83
84
        $wizard = new Currency($currencyCode, 0);
85
        $wizard->setLocale($locale);
86
        self::assertSame($expectedResult, (string) $wizard);
87
    }
88
89
    public function providerCurrencyLocaleNoDecimals(): array
90
    {
91
        return [
92
            ["[\$€-fy-NL]\u{a0}#,##0;[\$€-fy-NL]\u{a0}#,##0-", '€', 'fy-NL'], // Trailing negative
93
            ["[\$€-nl-NL]\u{a0}#,##0;[\$€-nl-NL]\u{a0}-#,##0", '€', 'nl-NL'], // Sign between currency and value
94
            ["[\$€-nl-BE]\u{a0}#,##0;[\$€-nl-BE]\u{a0}-#,##0", '€', 'NL-BE'], // Sign between currency and value
95
            ["#,##0\u{a0}[\$€-fr-BE]", '€', 'fr-be'],   // Trailing currency code
96
            ["#,##0\u{a0}[\$€-el-GR]", '€', 'el-gr'],   // Trailing currency code
97
            ['[$$-en-CA]#,##0', '$', 'en-ca'],
98
            ["#,##0\u{a0}[\$\$-fr-CA]", '$', 'fr-ca'],   // Trailing currency code
99
            ['[$¥-ja-JP]#,##0', '¥', 'ja-JP'], // No decimals to truncate
100
            ["#,##0\u{a0}[\$د.ب‎-ar-BH]", 'د.ب‎', 'ar-BH'],  // 3 decimals truncated to none
101
        ];
102
    }
103
104
    public function testCurrencyLocaleInvalidFormat(): void
105
    {
106
        if (class_exists(NumberFormatter::class) === false) {
107
            self::markTestSkipped('Intl extension is not available');
108
        }
109
110
        $locale = 'en-usa';
111
112
        $this->expectException(Exception::class);
113
        $this->expectExceptionMessage("Invalid locale code '{$locale}'");
114
115
        $wizard = new Currency('€');
116
        $wizard->setLocale($locale);
117
    }
118
119
    public function testCurrencyLocaleInvalidCode(): void
120
    {
121
        if (class_exists(NumberFormatter::class) === false) {
122
            self::markTestSkipped('Intl extension is not available');
123
        }
124
125
        $locale = 'nl-GB';
126
127
        $this->expectException(Exception::class);
128
        $this->expectExceptionMessage("Unable to read locale data for '{$locale}'");
129
130
        $wizard = new Currency('€');
131
        $wizard->setLocale($locale);
132
    }
133
}
134