Completed
Branch develop (b42baf)
by Adrien
11:23
created

AdvancedValueBinderTest::provider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 19
rs 9.4285
1
<?php
2
3
namespace PhpSpreadsheetTests\Cell;
4
5
use PhpSpreadsheet\CachedObjectStorage\Memory;
6
use PhpSpreadsheet\Cell;
7
use PhpSpreadsheet\Cell\AdvancedValueBinder;
8
use PhpSpreadsheet\Cell\DataType;
9
use PhpSpreadsheet\Shared\StringHelper;
10
use PhpSpreadsheet\Style\NumberFormat;
11
use PhpSpreadsheet\Worksheet;
12
13
class AdvancedValueBinderTest extends \PHPUnit_Framework_TestCase
14
{
15 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
    {
17
        if (!defined('PHPSPREADSHEET_ROOT')) {
18
            define('PHPSPREADSHEET_ROOT', APPLICATION_PATH . '/');
19
        }
20
        require_once PHPSPREADSHEET_ROOT . '/Bootstrap.php';
21
    }
22
23
    public function provider()
24
    {
25
        if (!class_exists(NumberFormat::class)) {
26
            $this->setUp();
27
        }
28
        $currencyUSD = NumberFormat::FORMAT_CURRENCY_USD_SIMPLE;
29
        $currencyEURO = str_replace('$', '€', NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
30
31
        return [
32
            ['10%', 0.1, NumberFormat::FORMAT_PERCENTAGE_00, ',', '.', '$'],
33
            ['$10.11', 10.11, $currencyUSD, ',', '.', '$'],
34
            ['$1,010.12', 1010.12, $currencyUSD, ',', '.', '$'],
35
            ['$20,20', 20.2, $currencyUSD, '.', ',', '$'],
36
            ['$2.020,20', 2020.2, $currencyUSD, '.', ',', '$'],
37
            ['€2.020,20', 2020.2, $currencyEURO, '.', ',', '€'],
38
            ['€ 2.020,20', 2020.2, $currencyEURO, '.', ',', '€'],
39
            ['€2,020.22', 2020.22, $currencyEURO, ',', '.', '€'],
40
        ];
41
    }
42
43
    /**
44
     * @dataProvider provider
45
     */
46
    public function testCurrency($value, $valueBinded, $format, $thousandsSeparator, $decimalSeparator, $currencyCode)
47
    {
48
        $sheet = $this->getMock(
49
            Worksheet::class,
50
            ['getStyle', 'getNumberFormat', 'setFormatCode', 'getCellCacheController']
51
        );
52
        $cache = $this->getMockBuilder(Memory::class)
53
            ->disableOriginalConstructor()
54
            ->getMock();
55
        $cache->expects($this->any())
56
                 ->method('getParent')
57
                 ->will($this->returnValue($sheet));
58
59
        $sheet->expects($this->once())
60
                 ->method('getStyle')
61
                 ->will($this->returnSelf());
62
        $sheet->expects($this->once())
63
                 ->method('getNumberFormat')
64
                 ->will($this->returnSelf());
65
        $sheet->expects($this->once())
66
                 ->method('setFormatCode')
67
                 ->with($format)
68
                 ->will($this->returnSelf());
69
        $sheet->expects($this->any())
70
                 ->method('getCellCacheController')
71
                 ->will($this->returnValue($cache));
72
73
        StringHelper::setCurrencyCode($currencyCode);
74
        StringHelper::setDecimalSeparator($decimalSeparator);
75
        StringHelper::setThousandsSeparator($thousandsSeparator);
76
77
        $cell = new Cell(null, DataType::TYPE_STRING, $sheet);
78
79
        $binder = new AdvancedValueBinder();
80
        $binder->bindValue($cell, $value);
81
        $this->assertEquals($valueBinded, $cell->getValue());
82
    }
83
}
84