1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Engineering\ComplexOperations; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
11
|
|
|
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments; |
12
|
|
|
use PhpOffice\PhpSpreadsheetTests\Custom\ComplexAssert; |
13
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
14
|
|
|
|
15
|
|
|
class ImSumTest extends ComplexAssert |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param string ...$args variadic arguments |
19
|
|
|
*/ |
20
|
|
|
#[DataProvider('providerIMSUM')] |
21
|
|
|
public function testDirectCallToIMSUM(mixed $expectedResult, ...$args): void |
22
|
|
|
{ |
23
|
|
|
$result = ComplexOperations::IMSUM(...$args); |
24
|
|
|
$this->assertComplexEquals($expectedResult, $result); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
#[DataProvider('providerIMSUM')] |
28
|
|
|
public function testIMSUMAsFormula(mixed $expectedResult, mixed ...$args): void |
29
|
|
|
{ |
30
|
|
|
$arguments = new FormulaArguments(...$args); |
31
|
|
|
|
32
|
|
|
$calculation = Calculation::getInstance(); |
33
|
|
|
$formula = "=IMSUM({$arguments})"; |
34
|
|
|
|
35
|
|
|
/** @var float|int|string */ |
36
|
|
|
$result = $calculation->calculateFormula($formula); |
37
|
|
|
$this->assertComplexEquals($expectedResult, $result); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
#[DataProvider('providerIMSUM')] |
41
|
|
|
public function testIMSUMInWorksheet(mixed $expectedResult, mixed ...$args): void |
42
|
|
|
{ |
43
|
|
|
$arguments = new FormulaArguments(...$args); |
44
|
|
|
|
45
|
|
|
$spreadsheet = new Spreadsheet(); |
46
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
47
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
48
|
|
|
$formula = "=IMSUM({$argumentCells})"; |
49
|
|
|
|
50
|
|
|
$result = $worksheet->setCellValue('A1', $formula) |
51
|
|
|
->getCell('A1') |
52
|
|
|
->getCalculatedValue(); |
53
|
|
|
$this->assertComplexEquals($expectedResult, $result); |
54
|
|
|
|
55
|
|
|
$spreadsheet->disconnectWorksheets(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function providerIMSUM(): array |
59
|
|
|
{ |
60
|
|
|
return require 'tests/data/Calculation/Engineering/IMSUM.php'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
#[DataProvider('providerUnhappyIMSUM')] |
64
|
|
|
public function testIMSUMUnhappyPath(string $expectedException, mixed ...$args): void |
65
|
|
|
{ |
66
|
|
|
$arguments = new FormulaArguments(...$args); |
67
|
|
|
|
68
|
|
|
$spreadsheet = new Spreadsheet(); |
69
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
70
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
71
|
|
|
$formula = "=IMSUM({$argumentCells})"; |
72
|
|
|
|
73
|
|
|
$this->expectException(CalculationException::class); |
74
|
|
|
$this->expectExceptionMessage($expectedException); |
75
|
|
|
$worksheet->setCellValue('A1', $formula) |
76
|
|
|
->getCell('A1') |
77
|
|
|
->getCalculatedValue(); |
78
|
|
|
|
79
|
|
|
$spreadsheet->disconnectWorksheets(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function providerUnhappyIMSUM(): array |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
['Formula Error: Wrong number of arguments for IMSUM() function'], |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|