Completed
Push — master ( 5072ff...a24179 )
by Mark
37s queued 28s
created

AdvancedValueBinderTest::fractionProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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