1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Engineering\ComplexOperations; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
10
|
|
|
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments; |
11
|
|
|
use PhpOffice\PhpSpreadsheetTests\Custom\ComplexAssert; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class ImSumTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
const COMPLEX_PRECISION = 1E-12; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ComplexAssert |
20
|
|
|
*/ |
21
|
|
|
private $complexAssert; |
22
|
|
|
|
23
|
|
|
protected function setUp(): void |
24
|
|
|
{ |
25
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); |
26
|
|
|
$this->complexAssert = new ComplexAssert(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @dataProvider providerIMSUM |
31
|
|
|
* |
32
|
|
|
* @param mixed $expectedResult |
33
|
|
|
*/ |
34
|
|
|
public function testDirectCallToIMSUM($expectedResult, ...$args): void |
35
|
|
|
{ |
36
|
|
|
/** @scrutinizer ignore-call */ |
37
|
|
|
$result = ComplexOperations::IMSUM(...$args); |
38
|
|
|
self::assertTrue( |
39
|
|
|
$this->complexAssert->assertComplexEquals($expectedResult, $result, self::COMPLEX_PRECISION), |
40
|
|
|
$this->complexAssert->getErrorMessage() |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function trimIfQuoted(string $value): string |
45
|
|
|
{ |
46
|
|
|
return trim($value, '"'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @dataProvider providerIMSUM |
51
|
|
|
* |
52
|
|
|
* @param mixed $expectedResult |
53
|
|
|
*/ |
54
|
|
|
public function testIMSUMAsFormula($expectedResult, ...$args): void |
55
|
|
|
{ |
56
|
|
|
$arguments = new FormulaArguments(...$args); |
57
|
|
|
|
58
|
|
|
$calculation = Calculation::getInstance(); |
59
|
|
|
$formula = "=IMSUM({$arguments})"; |
60
|
|
|
|
61
|
|
|
$result = $calculation->_calculateFormulaValue($formula); |
62
|
|
|
self::assertTrue( |
63
|
|
|
$this->complexAssert->assertComplexEquals($expectedResult, $this->trimIfQuoted((string) $result), self::COMPLEX_PRECISION), |
64
|
|
|
$this->complexAssert->getErrorMessage() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @dataProvider providerIMSUM |
70
|
|
|
* |
71
|
|
|
* @param mixed $expectedResult |
72
|
|
|
*/ |
73
|
|
|
public function testIMSUMInWorksheet($expectedResult, ...$args): void |
74
|
|
|
{ |
75
|
|
|
$arguments = new FormulaArguments(...$args); |
76
|
|
|
|
77
|
|
|
$spreadsheet = new Spreadsheet(); |
78
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
79
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
80
|
|
|
$formula = "=IMSUM({$argumentCells})"; |
81
|
|
|
|
82
|
|
|
$result = $worksheet->setCellValue('A1', $formula) |
83
|
|
|
->getCell('A1') |
84
|
|
|
->getCalculatedValue(); |
85
|
|
|
self::assertTrue( |
86
|
|
|
$this->complexAssert->assertComplexEquals($expectedResult, $result, self::COMPLEX_PRECISION), |
87
|
|
|
$this->complexAssert->getErrorMessage() |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$spreadsheet->disconnectWorksheets(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function providerIMSUM(): array |
94
|
|
|
{ |
95
|
|
|
return require 'tests/data/Calculation/Engineering/IMSUM.php'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @dataProvider providerUnhappyIMSUM |
100
|
|
|
*/ |
101
|
|
|
public function testIMSUMUnhappyPath(string $expectedException, ...$args): void |
102
|
|
|
{ |
103
|
|
|
$arguments = new FormulaArguments(...$args); |
104
|
|
|
|
105
|
|
|
$spreadsheet = new Spreadsheet(); |
106
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
107
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
108
|
|
|
$formula = "=IMSUM({$argumentCells})"; |
109
|
|
|
|
110
|
|
|
$this->expectException(CalculationException::class); |
111
|
|
|
$this->expectExceptionMessage($expectedException); |
112
|
|
|
$worksheet->setCellValue('A1', $formula) |
113
|
|
|
->getCell('A1') |
114
|
|
|
->getCalculatedValue(); |
115
|
|
|
|
116
|
|
|
$spreadsheet->disconnectWorksheets(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function providerUnhappyIMSUM(): array |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
['Formula Error: Wrong number of arguments for IMSUM() function'], |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|