1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Cell; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\IValueBinder; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class AdvancedValueBinderTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
const AVB_PRECISION = 1.0E-8; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $currencyCode; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $decimalSeparator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $thousandsSeparator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var IValueBinder |
33
|
|
|
*/ |
34
|
|
|
private $valueBinder; |
35
|
|
|
|
36
|
|
|
protected function setUp(): void |
37
|
|
|
{ |
38
|
|
|
$this->currencyCode = StringHelper::getCurrencyCode(); |
39
|
|
|
$this->decimalSeparator = StringHelper::getDecimalSeparator(); |
40
|
|
|
$this->thousandsSeparator = StringHelper::getThousandsSeparator(); |
41
|
|
|
$this->valueBinder = Cell::getValueBinder(); |
42
|
|
|
Cell::setValueBinder(new AdvancedValueBinder()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function tearDown(): void |
46
|
|
|
{ |
47
|
|
|
StringHelper::setCurrencyCode($this->currencyCode); |
48
|
|
|
StringHelper::setDecimalSeparator($this->decimalSeparator); |
49
|
|
|
StringHelper::setThousandsSeparator($this->thousandsSeparator); |
50
|
|
|
Cell::setValueBinder($this->valueBinder); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testNullValue(): void |
54
|
|
|
{ |
55
|
|
|
$spreadsheet = new Spreadsheet(); |
56
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
57
|
|
|
|
58
|
|
|
$sheet->getCell('A1')->setValue(null); |
59
|
|
|
self::assertNull($sheet->getCell('A1')->getValue()); |
60
|
|
|
|
61
|
|
|
$spreadsheet->disconnectWorksheets(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testBoolean(): void |
65
|
|
|
{ |
66
|
|
|
$spreadsheet = new Spreadsheet(); |
67
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
68
|
|
|
|
69
|
|
|
$sheet->getCell('A1')->setValue(true); |
70
|
|
|
self::assertTrue($sheet->getCell('A1')->getValue()); |
71
|
|
|
|
72
|
|
|
$sheet->getCell('A2')->setValue(false); |
73
|
|
|
self::assertFalse($sheet->getCell('A2')->getValue()); |
74
|
|
|
|
75
|
|
|
$spreadsheet->disconnectWorksheets(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @dataProvider currencyProvider |
80
|
|
|
* |
81
|
|
|
* @param mixed $value |
82
|
|
|
* @param mixed $valueBinded |
83
|
|
|
* @param mixed $thousandsSeparator |
84
|
|
|
* @param mixed $decimalSeparator |
85
|
|
|
* @param mixed $currencyCode |
86
|
|
|
*/ |
87
|
|
|
public function testCurrency($value, $valueBinded, $thousandsSeparator, $decimalSeparator, $currencyCode): void |
88
|
|
|
{ |
89
|
|
|
StringHelper::setCurrencyCode($currencyCode); |
90
|
|
|
StringHelper::setDecimalSeparator($decimalSeparator); |
91
|
|
|
StringHelper::setThousandsSeparator($thousandsSeparator); |
92
|
|
|
|
93
|
|
|
$spreadsheet = new Spreadsheet(); |
94
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
95
|
|
|
|
96
|
|
|
$sheet->getCell('A1')->setValue($value); |
97
|
|
|
self::assertEquals($valueBinded, $sheet->getCell('A1')->getValue()); |
98
|
|
|
|
99
|
|
|
$spreadsheet->disconnectWorksheets(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function currencyProvider(): array |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
['$10.11', 10.11, ',', '.', '$'], |
106
|
|
|
['$1,010.12', 1010.12, ',', '.', '$'], |
107
|
|
|
['$20,20', 20.2, '.', ',', '$'], |
108
|
|
|
['$2.020,20', 2020.2, '.', ',', '$'], |
109
|
|
|
['€2.020,20', 2020.2, '.', ',', '€'], |
110
|
|
|
['€ 2.020,20', 2020.2, '.', ',', '€'], |
111
|
|
|
['€2,020.22', 2020.22, ',', '.', '€'], |
112
|
|
|
['$10.11', 10.11, ',', '.', '€'], |
113
|
|
|
['€2,020.20', 2020.2, ',', '.', '$'], |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @dataProvider fractionProvider |
119
|
|
|
* |
120
|
|
|
* @param mixed $value |
121
|
|
|
* @param mixed $valueBinded |
122
|
|
|
*/ |
123
|
|
|
public function testFractions($value, $valueBinded): void |
124
|
|
|
{ |
125
|
|
|
$spreadsheet = new Spreadsheet(); |
126
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
127
|
|
|
|
128
|
|
|
$sheet->getCell('A1')->setValue($value); |
129
|
|
|
self::assertEquals($valueBinded, $sheet->getCell('A1')->getValue()); |
130
|
|
|
|
131
|
|
|
$spreadsheet->disconnectWorksheets(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function fractionProvider(): array |
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
|
|
['1/5', 0.2], |
138
|
|
|
['-1/5', -0.2], |
139
|
|
|
['12/5', 2.4], |
140
|
|
|
['2/100', 0.02], |
141
|
|
|
['15/12', 1.25], |
142
|
|
|
['20/100', 0.2], |
143
|
|
|
['1 3/5', 1.6], |
144
|
|
|
['-1 3/5', -1.6], |
145
|
|
|
['1 4/20', 1.2], |
146
|
|
|
['1 16/20', 1.8], |
147
|
|
|
['12 20/100', 12.2], |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @dataProvider percentageProvider |
153
|
|
|
* |
154
|
|
|
* @param mixed $value |
155
|
|
|
* @param mixed $valueBinded |
156
|
|
|
*/ |
157
|
|
|
public function testPercentages($value, $valueBinded): void |
158
|
|
|
{ |
159
|
|
|
$spreadsheet = new Spreadsheet(); |
160
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
161
|
|
|
|
162
|
|
|
$sheet->getCell('A1')->setValue($value); |
163
|
|
|
self::assertEquals($valueBinded, $sheet->getCell('A1')->getValue()); |
164
|
|
|
|
165
|
|
|
$spreadsheet->disconnectWorksheets(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function percentageProvider(): array |
169
|
|
|
{ |
170
|
|
|
return [ |
171
|
|
|
['10%', 0.1], |
172
|
|
|
['-12%', -0.12], |
173
|
|
|
['120%', 1.2], |
174
|
|
|
['12.5%', 0.125], |
175
|
|
|
]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @dataProvider timeProvider |
180
|
|
|
* |
181
|
|
|
* @param mixed $value |
182
|
|
|
* @param mixed $valueBinded |
183
|
|
|
*/ |
184
|
|
|
public function testTimes($value, $valueBinded): void |
185
|
|
|
{ |
186
|
|
|
$spreadsheet = new Spreadsheet(); |
187
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
188
|
|
|
|
189
|
|
|
$sheet->getCell('A1')->setValue($value); |
190
|
|
|
self::assertEqualsWithDelta($valueBinded, $sheet->getCell('A1')->getValue(), self::AVB_PRECISION); |
191
|
|
|
|
192
|
|
|
$spreadsheet->disconnectWorksheets(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function timeProvider(): array |
196
|
|
|
{ |
197
|
|
|
return [ |
198
|
|
|
['1:20', 0.05555555556], |
199
|
|
|
['09:17', 0.386805555556], |
200
|
|
|
['15:00', 0.625], |
201
|
|
|
['17:12:35', 0.71707175926], |
202
|
|
|
['23:58:20', 0.99884259259], |
203
|
|
|
]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @dataProvider stringProvider |
208
|
|
|
*/ |
209
|
|
|
public function testStringWrapping(string $value): void |
210
|
|
|
{ |
211
|
|
|
$spreadsheet = new Spreadsheet(); |
212
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
213
|
|
|
|
214
|
|
|
$sheet->getCell('A1')->setValue($value); |
215
|
|
|
self::assertEquals($value, $sheet->getCell('A1')->getValue()); |
216
|
|
|
|
217
|
|
|
$spreadsheet->disconnectWorksheets(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function stringProvider(): array |
221
|
|
|
{ |
222
|
|
|
return [ |
223
|
|
|
['Hello World', false], |
224
|
|
|
["Hello\nWorld", true], |
225
|
|
|
]; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|