Passed
Pull Request — master (#3448)
by Mark
22:11
created

ImSqrtTest::testIMSQRTInWorksheet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 18
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Calculation\Engineering\ComplexFunctions;
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 ImSqrtTest 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 providerIMSQRT
31
     *
32
     * @param mixed $expectedResult
33
     */
34
    public function testDirectCallToIMSQRT($expectedResult, ...$args): void
35
    {
36
        /** @scrutinizer ignore-call */
37
        $result = ComplexFunctions::IMSQRT(...$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 providerIMSQRT
51
     *
52
     * @param mixed $expectedResult
53
     */
54
    public function testIMSQRTAsFormula($expectedResult, ...$args): void
55
    {
56
        $arguments = new FormulaArguments(...$args);
57
58
        $calculation = Calculation::getInstance();
59
        $formula = "=IMSQRT({$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 providerIMSQRT
70
     *
71
     * @param mixed $expectedResult
72
     */
73
    public function testIMSQRTInWorksheet($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 = "=IMSQRT({$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 providerIMSQRT(): array
94
    {
95
        return require 'tests/data/Calculation/Engineering/IMSQRT.php';
96
    }
97
98
    /**
99
     * @dataProvider providerUnhappyIMSQRT
100
     */
101
    public function testIMSQRTUnhappyPath(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 = "=IMSQRT({$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 providerUnhappyIMSQRT(): array
120
    {
121
        return [
122
            ['Formula Error: Wrong number of arguments for IMSQRT() function'],
123
        ];
124
    }
125
126
    /**
127
     * @dataProvider providerImSqrtArray
128
     */
129
    public function testImSqrtArray(array $expectedResult, string $complex): void
130
    {
131
        $calculation = Calculation::getInstance();
132
133
        $formula = "=IMSQRT({$complex})";
134
        $result = $calculation->_calculateFormulaValue($formula);
135
        self::assertEquals($expectedResult, $result);
136
    }
137
138
    public function providerImSqrtArray(): array
139
    {
140
        return [
141
            'row/column vector' => [
142
                [
143
                    ['0.9199408686343-1.3587829855366i', '1.1180339887499-1.1180339887499i', '1.3587829855366-0.9199408686343i'],
144
                    ['0.45508986056223-1.0986841134678i', '0.70710678118655-0.70710678118655i', '1.0986841134678-0.45508986056223i'],
145
                    ['0.45508986056223+1.0986841134678i', '0.70710678118655+0.70710678118655i', '1.0986841134678+0.45508986056223i'],
146
                    ['0.9199408686343+1.3587829855366i', '1.1180339887499+1.1180339887499i', '1.3587829855366+0.9199408686343i'],
147
                ],
148
                '{"-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"}',
149
            ],
150
        ];
151
    }
152
}
153