Passed
Push — master ( e78bca...f65b0a )
by
unknown
14:57 queued 07:09
created

ImSqrtTest::testDirectCallToIMSQRT()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 ImSqrtTest extends ComplexAssert
16
{
17
    #[DataProvider('providerIMSQRT')]
18
    public function testDirectCallToIMSQRT(string $expectedResult, string $arg): void
19
    {
20
        $result = ComplexFunctions::IMSQRT($arg);
21
        $this->assertComplexEquals($expectedResult, $result);
22
    }
23
24
    #[DataProvider('providerIMSQRT')]
25
    public function testIMSQRTAsFormula(mixed $expectedResult, mixed ...$args): void
26
    {
27
        $arguments = new FormulaArguments(...$args);
28
29
        $calculation = Calculation::getInstance();
30
        $formula = "=IMSQRT({$arguments})";
31
32
        /** @var float|int|string */
33
        $result = $calculation->calculateFormula($formula);
34
        $this->assertComplexEquals($expectedResult, $result);
35
    }
36
37
    #[DataProvider('providerIMSQRT')]
38
    public function testIMSQRTInWorksheet(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 = "=IMSQRT({$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 providerIMSQRT(): array
56
    {
57
        return require 'tests/data/Calculation/Engineering/IMSQRT.php';
58
    }
59
60
    #[DataProvider('providerUnhappyIMSQRT')]
61
    public function testIMSQRTUnhappyPath(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 = "=IMSQRT({$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 providerUnhappyIMSQRT(): array
80
    {
81
        return [
82
            ['Formula Error: Wrong number of arguments for IMSQRT() function'],
83
        ];
84
    }
85
86
    /** @param mixed[] $expectedResult */
87
    #[DataProvider('providerImSqrtArray')]
88
    public function testImSqrtArray(array $expectedResult, string $complex): void
89
    {
90
        $calculation = Calculation::getInstance();
91
92
        $formula = "=IMSQRT({$complex})";
93
        $result = $calculation->calculateFormula($formula);
94
        self::assertEquals($expectedResult, $result);
95
    }
96
97
    public static function providerImSqrtArray(): array
98
    {
99
        return [
100
            'row/column vector' => [
101
                [
102
                    ['0.9199408686343-1.3587829855366i', '1.1180339887499-1.1180339887499i', '1.3587829855366-0.9199408686343i'],
103
                    ['0.45508986056223-1.0986841134678i', '0.70710678118655-0.70710678118655i', '1.0986841134678-0.45508986056223i'],
104
                    ['0.45508986056223+1.0986841134678i', '0.70710678118655+0.70710678118655i', '1.0986841134678+0.45508986056223i'],
105
                    ['0.9199408686343+1.3587829855366i', '1.1180339887499+1.1180339887499i', '1.3587829855366+0.9199408686343i'],
106
                ],
107
                '{"-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"}',
108
            ],
109
        ];
110
    }
111
}
112