Completed
Push — master ( d2bbb6...033ed1 )
by Mark
31:23
created

CalculationTest::testCellWithDdeExpresion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8
use PHPUnit\Framework\TestCase;
9
10
class CalculationTest extends TestCase
11
{
12
    public function setUp()
13
    {
14
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15
    }
16
17
    public function tearDown()
18
    {
19
        $calculation = Calculation::getInstance();
20
        $calculation->setLocale('en_us');
21
    }
22
23
    /**
24
     * @dataProvider providerBinaryComparisonOperation
25
     *
26
     * @param mixed $formula
27
     * @param mixed $expectedResultExcel
28
     * @param mixed $expectedResultOpenOffice
29
     */
30
    public function testBinaryComparisonOperation($formula, $expectedResultExcel, $expectedResultOpenOffice)
31
    {
32
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
33
        $resultExcel = Calculation::getInstance()->_calculateFormulaValue($formula);
34
        self::assertEquals($expectedResultExcel, $resultExcel, 'should be Excel compatible');
35
36
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
37
        $resultOpenOffice = Calculation::getInstance()->_calculateFormulaValue($formula);
38
        self::assertEquals($expectedResultOpenOffice, $resultOpenOffice, 'should be OpenOffice compatible');
39
    }
40
41
    public function providerBinaryComparisonOperation()
42
    {
43
        return require 'data/CalculationBinaryComparisonOperation.php';
44
    }
45
46
    /**
47
     * @dataProvider providerGetFunctions
48
     *
49
     * @param string $category
50
     * @param array|string $functionCall
51
     * @param string $argumentCount
52
     */
53
    public function testGetFunctions($category, $functionCall, $argumentCount)
0 ignored issues
show
Unused Code introduced by
The parameter $argumentCount is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
    public function testGetFunctions($category, $functionCall, /** @scrutinizer ignore-unused */ $argumentCount)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $category is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
    public function testGetFunctions(/** @scrutinizer ignore-unused */ $category, $functionCall, $argumentCount)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55
        self::assertInternalType('callable', $functionCall);
56
    }
57
58
    public function providerGetFunctions()
59
    {
60
        return Calculation::getInstance()->getFunctions();
61
    }
62
63
    public function testIsImplemented()
64
    {
65
        $calculation = Calculation::getInstance();
66
        self::assertFalse($calculation->isImplemented('non-existing-function'));
67
        self::assertFalse($calculation->isImplemented('AREAS'));
68
        self::assertTrue($calculation->isImplemented('coUNt'));
69
        self::assertTrue($calculation->isImplemented('abs'));
70
    }
71
72
    /**
73
     * @dataProvider providerCanLoadAllSupportedLocales
74
     *
75
     * @param string $locale
76
     */
77
    public function testCanLoadAllSupportedLocales($locale)
78
    {
79
        $calculation = Calculation::getInstance();
80
        self::assertTrue($calculation->setLocale($locale));
81
    }
82
83
    public function providerCanLoadAllSupportedLocales()
84
    {
85
        return [
86
            ['bg'],
87
            ['cs'],
88
            ['da'],
89
            ['de'],
90
            ['en_us'],
91
            ['es'],
92
            ['fi'],
93
            ['fr'],
94
            ['hu'],
95
            ['it'],
96
            ['nl'],
97
            ['no'],
98
            ['pl'],
99
            ['pt'],
100
            ['pt_br'],
101
            ['ru'],
102
            ['sv'],
103
            ['tr'],
104
        ];
105
    }
106
107
    public function testDoesHandleXlfnFunctions()
108
    {
109
        $calculation = Calculation::getInstance();
110
111
        $tree = $calculation->parseFormula('=_xlfn.ISFORMULA(A1)');
112
        self::assertCount(3, $tree);
113
        $function = $tree[2];
114
        self::assertEquals('Function', $function['type']);
115
116
        $tree = $calculation->parseFormula('=_xlfn.STDEV.S(A1:B2)');
117
        self::assertCount(5, $tree);
118
        $function = $tree[4];
119
        self::assertEquals('Function', $function['type']);
120
    }
121
122
    public function testFormulaWithOptionalArgumentsAndRequiredCellReferenceShouldPassNullForMissingArguments()
123
    {
124
        $spreadsheet = new Spreadsheet();
125
        $sheet = $spreadsheet->getActiveSheet();
126
127
        $sheet->fromArray(
128
            [
129
                [1, 2, 3],
130
                [4, 5, 6],
131
                [7, 8, 9],
132
            ]
133
        );
134
135
        $cell = $sheet->getCell('E5');
136
        $cell->setValue('=OFFSET(D3, -1, -2, 1, 1)');
137
        self::assertEquals(5, $cell->getCalculatedValue(), 'with all arguments');
138
139
        $cell = $sheet->getCell('F6');
140
        $cell->setValue('=OFFSET(D3, -1, -2)');
141
        self::assertEquals(5, $cell->getCalculatedValue(), 'missing arguments should be filled with null');
142
    }
143
144
    public function testCellSetAsQuotedText()
145
    {
146
        $spreadsheet = new Spreadsheet();
147
        $workSheet = $spreadsheet->getActiveSheet();
148
        $cell = $workSheet->getCell('A1');
149
150
        $cell->setValue("=cmd|'/C calc'!A0");
151
        $cell->getStyle()->setQuotePrefix(true);
152
153
        self::assertEquals("=cmd|'/C calc'!A0", $cell->getCalculatedValue());
154
    }
155
156
    public function testCellWithDdeExpresion()
157
    {
158
        $spreadsheet = new Spreadsheet();
159
        $workSheet = $spreadsheet->getActiveSheet();
160
        $cell = $workSheet->getCell('A1');
161
162
        $cell->setValue("=cmd|'/C calc'!A0");
163
164
        self::assertEquals("=cmd|'/C calc'!A0", $cell->getCalculatedValue());
165
    }
166
}
167