Failed Conditions
Push — master ( f65b0a...9a193f )
by
unknown
30s queued 21s
created

ImaginaryTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 45
dl 0
loc 88
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A providerIMAGINARY() 0 3 1
A testIMAGINARYUnhappyPath() 0 14 1
A testIMAGINARYInWorksheet() 0 13 1
A providerUnhappyIMAGINARY() 0 4 1
A testIMAGINARYAsFormula() 0 10 1
A testImaginaryArray() 0 8 1
A testDirectCallToIMAGINARY() 0 5 1
A providerImaginaryArray() 0 11 1
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\Complex;
9
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
10
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
11
use PHPUnit\Framework\Attributes\DataProvider;
12
13
class ImaginaryTest extends AllSetupTeardown
14
{
15
    const COMPLEX_PRECISION = 1E-12;
16
17
    #[DataProvider('providerIMAGINARY')]
18
    public function testDirectCallToIMAGINARY(float|int|string $expectedResult, float|int|string $arg): void
19
    {
20
        $result = Complex::IMAGINARY((string) $arg);
21
        self::assertEqualsWithDelta($expectedResult, $result, self::COMPLEX_PRECISION);
22
    }
23
24
    #[DataProvider('providerIMAGINARY')]
25
    public function testIMAGINARYAsFormula(mixed $expectedResult, mixed ...$args): void
26
    {
27
        $arguments = new FormulaArguments(...$args);
28
29
        $calculation = Calculation::getInstance();
30
        $formula = "=IMAGINARY({$arguments})";
31
32
        $result = $calculation->calculateFormula($formula);
33
        self::assertEqualsWithDelta($expectedResult, $result, self::COMPLEX_PRECISION);
34
    }
35
36
    #[DataProvider('providerIMAGINARY')]
37
    public function testIMAGINARYInWorksheet(mixed $expectedResult, mixed ...$args): void
38
    {
39
        $arguments = new FormulaArguments(...$args);
40
41
        $worksheet = $this->getSheet();
42
        $argumentCells = $arguments->populateWorksheet($worksheet);
43
        $formula = "=IMAGINARY({$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 providerIMAGINARY(): array
52
    {
53
        return require 'tests/data/Calculation/Engineering/IMAGINARY.php';
54
    }
55
56
    #[DataProvider('providerUnhappyIMAGINARY')]
57
    public function testIMAGINARYUnhappyPath(string $expectedException, mixed ...$args): void
58
    {
59
        $arguments = new FormulaArguments(...$args);
60
61
        $worksheet = $this->getSheet();
62
        $argumentCells = $arguments->populateWorksheet($worksheet);
63
        $formula = "=IMAGINARY({$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 providerUnhappyIMAGINARY(): array
73
    {
74
        return [
75
            ['Formula Error: Wrong number of arguments for IMAGINARY() function'],
76
        ];
77
    }
78
79
    /** @param mixed[] $expectedResult */
80
    #[DataProvider('providerImaginaryArray')]
81
    public function testImaginaryArray(array $expectedResult, string $complex): void
82
    {
83
        $calculation = Calculation::getInstance();
84
85
        $formula = "=IMAGINARY({$complex})";
86
        $result = $calculation->calculateFormula($formula);
87
        self::assertEquals($expectedResult, $result);
88
    }
89
90
    public static function providerImaginaryArray(): array
91
    {
92
        return [
93
            'row/column vector' => [
94
                [
95
                    [-2.5, -2.5, -2.5],
96
                    [-1.0, -1.0, -1.0],
97
                    [1.0, 1.0, 1.0],
98
                    [2.5, 2.5, 2.5],
99
                ],
100
                '{"-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"}',
101
            ],
102
        ];
103
    }
104
}
105