1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Cell; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\CachedObjectStorage\Memory; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Cell; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet; |
12
|
|
|
|
13
|
|
|
class AdvancedValueBinderTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
public function provider() |
16
|
|
|
{ |
17
|
|
|
$currencyUSD = NumberFormat::FORMAT_CURRENCY_USD_SIMPLE; |
18
|
|
|
$currencyEURO = str_replace('$', '€', NumberFormat::FORMAT_CURRENCY_USD_SIMPLE); |
19
|
|
|
|
20
|
|
|
return [ |
21
|
|
|
['10%', 0.1, NumberFormat::FORMAT_PERCENTAGE_00, ',', '.', '$'], |
22
|
|
|
['$10.11', 10.11, $currencyUSD, ',', '.', '$'], |
23
|
|
|
['$1,010.12', 1010.12, $currencyUSD, ',', '.', '$'], |
24
|
|
|
['$20,20', 20.2, $currencyUSD, '.', ',', '$'], |
25
|
|
|
['$2.020,20', 2020.2, $currencyUSD, '.', ',', '$'], |
26
|
|
|
['€2.020,20', 2020.2, $currencyEURO, '.', ',', '€'], |
27
|
|
|
['€ 2.020,20', 2020.2, $currencyEURO, '.', ',', '€'], |
28
|
|
|
['€2,020.22', 2020.22, $currencyEURO, ',', '.', '€'], |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider provider |
34
|
|
|
* |
35
|
|
|
* @param mixed $value |
36
|
|
|
* @param mixed $valueBinded |
37
|
|
|
* @param mixed $format |
38
|
|
|
* @param mixed $thousandsSeparator |
39
|
|
|
* @param mixed $decimalSeparator |
40
|
|
|
* @param mixed $currencyCode |
41
|
|
|
*/ |
42
|
|
|
public function testCurrency($value, $valueBinded, $format, $thousandsSeparator, $decimalSeparator, $currencyCode) |
43
|
|
|
{ |
44
|
|
|
$sheet = $this->getMock(Worksheet::class, ['getStyle', 'getNumberFormat', 'setFormatCode', 'getCellCacheController']); |
45
|
|
|
$cache = $this->getMockBuilder(Memory::class) |
46
|
|
|
->disableOriginalConstructor() |
47
|
|
|
->getMock(); |
48
|
|
|
$cache->expects($this->any()) |
49
|
|
|
->method('getParent') |
50
|
|
|
->will($this->returnValue($sheet)); |
51
|
|
|
|
52
|
|
|
$sheet->expects($this->once()) |
53
|
|
|
->method('getStyle') |
54
|
|
|
->will($this->returnSelf()); |
55
|
|
|
$sheet->expects($this->once()) |
56
|
|
|
->method('getNumberFormat') |
57
|
|
|
->will($this->returnSelf()); |
58
|
|
|
$sheet->expects($this->once()) |
59
|
|
|
->method('setFormatCode') |
60
|
|
|
->with($format) |
61
|
|
|
->will($this->returnSelf()); |
62
|
|
|
$sheet->expects($this->any()) |
63
|
|
|
->method('getCellCacheController') |
64
|
|
|
->will($this->returnValue($cache)); |
65
|
|
|
|
66
|
|
|
StringHelper::setCurrencyCode($currencyCode); |
67
|
|
|
StringHelper::setDecimalSeparator($decimalSeparator); |
68
|
|
|
StringHelper::setThousandsSeparator($thousandsSeparator); |
69
|
|
|
|
70
|
|
|
$cell = new Cell(null, DataType::TYPE_STRING, $sheet); |
71
|
|
|
|
72
|
|
|
$binder = new AdvancedValueBinder(); |
73
|
|
|
$binder->bindValue($cell, $value); |
74
|
|
|
$this->assertEquals($valueBinded, $cell->getValue()); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|