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); |
|
|
|
|
81
|
|
|
self::expectExceptionMessage("Invalid locale code '{$locale}'"); |
|
|
|
|
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); |
|
|
|
|
96
|
|
|
self::expectExceptionMessage("Unable to read locale data for '{$locale}'"); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$wizard = new Currency('€'); |
99
|
|
|
$wizard->setLocale($locale); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|