Failed Conditions
Push — master ( a2282e...ad0b68 )
by Mark
40s queued 35s
created

CurrencyTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 39
c 1
b 0
f 0
dl 0
loc 89
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A providerCurrencyLocale() 0 12 1
A providerCurrency() 0 9 1
A testCurrencyLocale() 0 12 2
A testCurrencyLocaleInvalidCode() 0 13 2
A testCurrencyLocaleInvalidFormat() 0 13 2
A testCurrency() 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\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
    public function testCurrencyLocaleInvalidFormat(): void
73
    {
74
        if (class_exists(NumberFormatter::class) === false) {
75
            self::markTestSkipped('Intl extension is not available');
76
        }
77
78
        $locale = 'en-usa';
79
80
        self::expectException(Exception::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        self::/** @scrutinizer ignore-call */ 
81
              expectException(Exception::class);
Loading history...
81
        self::expectExceptionMessage("Invalid locale code '{$locale}'");
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCa...xpectExceptionMessage() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        self::/** @scrutinizer ignore-call */ 
82
              expectExceptionMessage("Invalid locale code '{$locale}'");
Loading history...
82
83
        $wizard = new Currency('€');
84
        $wizard->setLocale($locale);
85
    }
86
87
    public function testCurrencyLocaleInvalidCode(): void
88
    {
89
        if (class_exists(NumberFormatter::class) === false) {
90
            self::markTestSkipped('Intl extension is not available');
91
        }
92
93
        $locale = 'nl-GB';
94
95
        self::expectException(Exception::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        self::/** @scrutinizer ignore-call */ 
96
              expectException(Exception::class);
Loading history...
96
        self::expectExceptionMessage("Unable to read locale data for '{$locale}'");
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCa...xpectExceptionMessage() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        self::/** @scrutinizer ignore-call */ 
97
              expectExceptionMessage("Unable to read locale data for '{$locale}'");
Loading history...
97
98
        $wizard = new Currency('€');
99
        $wizard->setLocale($locale);
100
    }
101
}
102