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\ComplexFunctions; |
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 ImSecTest extends ComplexAssert |
16
|
|
|
{ |
17
|
|
|
#[DataProvider('providerIMSEC')] |
18
|
|
|
public function testDirectCallToIMSEC(string $expectedResult, string $arg): void |
19
|
|
|
{ |
20
|
|
|
$result = ComplexFunctions::IMSEC($arg); |
21
|
|
|
$this->assertComplexEquals($expectedResult, $result); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
#[DataProvider('providerIMSEC')] |
25
|
|
|
public function testIMSECAsFormula(mixed $expectedResult, mixed ...$args): void |
26
|
|
|
{ |
27
|
|
|
$arguments = new FormulaArguments(...$args); |
28
|
|
|
|
29
|
|
|
$calculation = Calculation::getInstance(); |
30
|
|
|
$formula = "=IMSEC({$arguments})"; |
31
|
|
|
|
32
|
|
|
/** @var float|int|string */ |
33
|
|
|
$result = $calculation->calculateFormula($formula); |
34
|
|
|
$this->assertComplexEquals($expectedResult, $result); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
#[DataProvider('providerIMSEC')] |
38
|
|
|
public function testIMSECInWorksheet(mixed $expectedResult, mixed ...$args): void |
39
|
|
|
{ |
40
|
|
|
$arguments = new FormulaArguments(...$args); |
41
|
|
|
|
42
|
|
|
$spreadsheet = new Spreadsheet(); |
43
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
44
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
45
|
|
|
$formula = "=IMSEC({$argumentCells})"; |
46
|
|
|
|
47
|
|
|
$result = $worksheet->setCellValue('A1', $formula) |
48
|
|
|
->getCell('A1') |
49
|
|
|
->getCalculatedValue(); |
50
|
|
|
$this->assertComplexEquals($expectedResult, $result); |
51
|
|
|
|
52
|
|
|
$spreadsheet->disconnectWorksheets(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function providerIMSEC(): array |
56
|
|
|
{ |
57
|
|
|
return require 'tests/data/Calculation/Engineering/IMSEC.php'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
#[DataProvider('providerUnhappyIMSEC')] |
61
|
|
|
public function testIMSECUnhappyPath(string $expectedException, mixed ...$args): void |
62
|
|
|
{ |
63
|
|
|
$arguments = new FormulaArguments(...$args); |
64
|
|
|
|
65
|
|
|
$spreadsheet = new Spreadsheet(); |
66
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
67
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
68
|
|
|
$formula = "=IMSEC({$argumentCells})"; |
69
|
|
|
|
70
|
|
|
$this->expectException(CalculationException::class); |
71
|
|
|
$this->expectExceptionMessage($expectedException); |
72
|
|
|
$worksheet->setCellValue('A1', $formula) |
73
|
|
|
->getCell('A1') |
74
|
|
|
->getCalculatedValue(); |
75
|
|
|
|
76
|
|
|
$spreadsheet->disconnectWorksheets(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function providerUnhappyIMSEC(): array |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
['Formula Error: Wrong number of arguments for IMSEC() function'], |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
#[DataProvider('providerImSecArray')] |
87
|
|
|
public function testImSecArray(array $expectedResult, string $complex): void |
88
|
|
|
{ |
89
|
|
|
$calculation = Calculation::getInstance(); |
90
|
|
|
|
91
|
|
|
$formula = "=IMSEC({$complex})"; |
92
|
|
|
$result = $calculation->calculateFormula($formula); |
93
|
|
|
self::assertEquals($expectedResult, $result); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public static function providerImSecArray(): array |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
'row/column vector' => [ |
100
|
|
|
[ |
101
|
|
|
['0.089798602872122+0.13798100670997i', '0.16307123192998', '0.089798602872122-0.13798100670997i'], |
102
|
|
|
['0.49833703055519+0.59108384172105i', '0.64805427366389', '0.49833703055519-0.59108384172105i'], |
103
|
|
|
['0.49833703055519-0.59108384172105i', '0.64805427366389', '0.49833703055519+0.59108384172105i'], |
104
|
|
|
['0.089798602872122-0.13798100670997i', '0.16307123192998', '0.089798602872122+0.13798100670997i'], |
105
|
|
|
], |
106
|
|
|
'{"-1-2.5i", "-2.5i", "1-2.5i"; "-1-i", "-i", "1-i"; "-1+i", "i", "1+1"; "-1+2.5i", "+2.5i", "1+2.5i"}', |
107
|
|
|
], |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|