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\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments; |
11
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
12
|
|
|
|
13
|
|
|
class ImArgumentTest extends AllSetupTeardown |
14
|
|
|
{ |
15
|
|
|
const COMPLEX_PRECISION = 1E-12; |
16
|
|
|
|
17
|
|
|
#[DataProvider('providerIMARGUMENT')] |
18
|
|
|
public function testDirectCallToIMARGUMENT(float|int|string $expectedResult, string $arg): void |
19
|
|
|
{ |
20
|
|
|
$result = ComplexFunctions::IMARGUMENT($arg); |
21
|
|
|
self::assertEqualsWithDelta($expectedResult, $result, self::COMPLEX_PRECISION); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
#[DataProvider('providerIMARGUMENT')] |
25
|
|
|
public function testIMARGUMENTAsFormula(mixed $expectedResult, mixed ...$args): void |
26
|
|
|
{ |
27
|
|
|
$arguments = new FormulaArguments(...$args); |
28
|
|
|
|
29
|
|
|
$calculation = Calculation::getInstance(); |
30
|
|
|
$formula = "=IMARGUMENT({$arguments})"; |
31
|
|
|
|
32
|
|
|
$result = $calculation->calculateFormula($formula); |
33
|
|
|
self::assertEqualsWithDelta($expectedResult, $result, self::COMPLEX_PRECISION); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
#[DataProvider('providerIMARGUMENT')] |
37
|
|
|
public function testIMARGUMENTInWorksheet(mixed $expectedResult, mixed ...$args): void |
38
|
|
|
{ |
39
|
|
|
$arguments = new FormulaArguments(...$args); |
40
|
|
|
|
41
|
|
|
$worksheet = $this->getSheet(); |
42
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
43
|
|
|
$formula = "=IMARGUMENT({$argumentCells})"; |
44
|
|
|
|
45
|
|
|
$result = $worksheet->setCellValue('A1', $formula) |
46
|
|
|
->getCell('A1') |
47
|
|
|
->getCalculatedValue(); |
48
|
|
|
self::assertEqualsWithDelta($expectedResult, $result, self::COMPLEX_PRECISION); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function providerIMARGUMENT(): array |
52
|
|
|
{ |
53
|
|
|
return require 'tests/data/Calculation/Engineering/IMARGUMENT.php'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
#[DataProvider('providerUnhappyIMARGUMENT')] |
57
|
|
|
public function testIMARGUMENTUnhappyPath(string $expectedException, mixed ...$args): void |
58
|
|
|
{ |
59
|
|
|
$arguments = new FormulaArguments(...$args); |
60
|
|
|
|
61
|
|
|
$worksheet = $this->getSheet(); |
62
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
63
|
|
|
$formula = "=IMARGUMENT({$argumentCells})"; |
64
|
|
|
|
65
|
|
|
$this->expectException(CalculationException::class); |
66
|
|
|
$this->expectExceptionMessage($expectedException); |
67
|
|
|
$worksheet->setCellValue('A1', $formula) |
68
|
|
|
->getCell('A1') |
69
|
|
|
->getCalculatedValue(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function providerUnhappyIMARGUMENT(): array |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
['Formula Error: Wrong number of arguments for IMARGUMENT() function'], |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function providerImArgumentArray(): array |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
'row/column vector' => [ |
83
|
|
|
[ |
84
|
|
|
[-1.9513027039072615, -1.5707963267948966, -1.1902899496825317], |
85
|
|
|
[-2.356194490192345, -1.5707963267948966, -0.7853981633974483], |
86
|
|
|
[2.356194490192345, 1.5707963267948966, 0.7853981633974483], |
87
|
|
|
[1.9513027039072615, 1.5707963267948966, 1.1902899496825317], |
88
|
|
|
], |
89
|
|
|
'{"-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"}', |
90
|
|
|
], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|