Passed
Push — master ( 66b95b...1192d3 )
by Mark
21:21 queued 10:15
created

AccountingTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 54
c 1
b 0
f 0
dl 0
loc 121
rs 10

8 Methods

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