1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Shared; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Csv; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class StringHelperTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
protected function tearDown(): void |
17
|
|
|
{ |
18
|
|
|
StringHelper::setCurrencyCode(null); |
19
|
|
|
StringHelper::setDecimalSeparator(null); |
20
|
|
|
StringHelper::setThousandsSeparator(null); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testGetIsIconvEnabled(): void |
24
|
|
|
{ |
25
|
|
|
$result = StringHelper::getIsIconvEnabled(); |
26
|
|
|
self::assertTrue($result); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testGetDecimalSeparator(): void |
30
|
|
|
{ |
31
|
|
|
$localeconv = localeconv(); |
32
|
|
|
|
33
|
|
|
$expectedResult = (!empty($localeconv['decimal_point'])) ? $localeconv['decimal_point'] : ','; |
34
|
|
|
$result = StringHelper::getDecimalSeparator(); |
35
|
|
|
self::assertEquals($expectedResult, $result); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testSetDecimalSeparator(): void |
39
|
|
|
{ |
40
|
|
|
$expectedResult = ','; |
41
|
|
|
StringHelper::setDecimalSeparator($expectedResult); |
42
|
|
|
|
43
|
|
|
$result = StringHelper::getDecimalSeparator(); |
44
|
|
|
self::assertEquals($expectedResult, $result); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testGetThousandsSeparator(): void |
48
|
|
|
{ |
49
|
|
|
$localeconv = localeconv(); |
50
|
|
|
|
51
|
|
|
$expectedResult = (!empty($localeconv['thousands_sep'])) ? $localeconv['thousands_sep'] : ','; |
52
|
|
|
$result = StringHelper::getThousandsSeparator(); |
53
|
|
|
self::assertEquals($expectedResult, $result); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testSetThousandsSeparator(): void |
57
|
|
|
{ |
58
|
|
|
$expectedResult = ' '; |
59
|
|
|
StringHelper::setThousandsSeparator($expectedResult); |
60
|
|
|
|
61
|
|
|
$result = StringHelper::getThousandsSeparator(); |
62
|
|
|
self::assertEquals($expectedResult, $result); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testGetCurrencyCode(): void |
66
|
|
|
{ |
67
|
|
|
$localeconv = localeconv(); |
68
|
|
|
$expectedResult = (!empty($localeconv['currency_symbol']) ? $localeconv['currency_symbol'] : (!empty($localeconv['int_curr_symbol']) ? $localeconv['int_curr_symbol'] : '$')); |
69
|
|
|
$result = StringHelper::getCurrencyCode(); |
70
|
|
|
self::assertEquals($expectedResult, $result); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testSetCurrencyCode(): void |
74
|
|
|
{ |
75
|
|
|
$expectedResult = '£'; |
76
|
|
|
StringHelper::setCurrencyCode($expectedResult); |
77
|
|
|
|
78
|
|
|
$result = StringHelper::getCurrencyCode(); |
79
|
|
|
self::assertEquals($expectedResult, $result); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testControlCharacterPHP2OOXML(): void |
83
|
|
|
{ |
84
|
|
|
$expectedResult = 'foo_x000B_bar'; |
85
|
|
|
$result = StringHelper::controlCharacterPHP2OOXML('foo' . chr(11) . 'bar'); |
86
|
|
|
|
87
|
|
|
self::assertEquals($expectedResult, $result); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testControlCharacterOOXML2PHP(): void |
91
|
|
|
{ |
92
|
|
|
$expectedResult = 'foo' . chr(11) . 'bar'; |
93
|
|
|
$result = StringHelper::controlCharacterOOXML2PHP('foo_x000B_bar'); |
94
|
|
|
|
95
|
|
|
self::assertEquals($expectedResult, $result); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testSYLKtoUTF8(): void |
99
|
|
|
{ |
100
|
|
|
$expectedResult = 'foo' . chr(11) . 'bar'; |
101
|
|
|
$result = StringHelper::SYLKtoUTF8("foo\x1B ;bar"); |
102
|
|
|
|
103
|
|
|
self::assertEquals($expectedResult, $result); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testIssue3900(): void |
107
|
|
|
{ |
108
|
|
|
StringHelper::setDecimalSeparator('.'); |
109
|
|
|
StringHelper::setThousandsSeparator(''); |
110
|
|
|
|
111
|
|
|
$spreadsheet = new Spreadsheet(); |
112
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
113
|
|
|
$sheet->setCellValue('A1', 1.4); |
114
|
|
|
$sheet->setCellValue('B1', 1004.5); |
115
|
|
|
$sheet->setCellValue('C1', 1000000.5); |
116
|
|
|
|
117
|
|
|
ob_start(); |
118
|
|
|
$ioWriter = new Csv($spreadsheet); |
119
|
|
|
$ioWriter->setDelimiter(';'); |
120
|
|
|
$ioWriter->save('php://output'); |
121
|
|
|
$output = ob_get_clean(); |
122
|
|
|
$spreadsheet->disconnectWorksheets(); |
123
|
|
|
self::assertSame('"1.4";"1004.5";"1000000.5"' . PHP_EOL, $output); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testNonStringable(): void |
127
|
|
|
{ |
128
|
|
|
$dt = new DateTime(); |
129
|
|
|
self::assertSame('', StringHelper::convertToString($dt, false)); |
130
|
|
|
$this->expectException(SpreadsheetException::class); |
131
|
|
|
$this->expectExceptionMessage('Unable to convert to string'); |
132
|
|
|
StringHelper::convertToString($dt); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|