Failed Conditions
Pull Request — master (#4571)
by Owen
15:04
created

ImRealTest::testIMREALAsFormula()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
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\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 ImRealTest extends AllSetupTeardown
14
{
15
    const COMPLEX_PRECISION = 1E-12;
16
17
    #[DataProvider('providerIMREAL')]
18
    public function testDirectCallToIMREAL(float|int|string $expectedResult, float|int|string $arg): void
19
    {
20
        $result = Complex::IMREAL((string) $arg);
21
        self::assertEqualsWithDelta($expectedResult, $result, self::COMPLEX_PRECISION);
22
    }
23
24
    #[DataProvider('providerIMREAL')]
25
    public function testIMREALAsFormula(mixed $expectedResult, mixed ...$args): void
26
    {
27
        $arguments = new FormulaArguments(...$args);
28
29
        $calculation = Calculation::getInstance();
30
        $formula = "=IMREAL({$arguments})";
31
32
        $result = $calculation->calculateFormula($formula);
33
        self::assertEqualsWithDelta($expectedResult, $result, self::COMPLEX_PRECISION);
34
    }
35
36
    #[DataProvider('providerIMREAL')]
37
    public function testIMREALInWorksheet(mixed $expectedResult, mixed ...$args): void
38
    {
39
        $arguments = new FormulaArguments(...$args);
40
41
        $worksheet = $this->getSheet();
42
        $argumentCells = $arguments->populateWorksheet($worksheet);
43
        $formula = "=IMREAL({$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 providerIMREAL(): array
52
    {
53
        return require 'tests/data/Calculation/Engineering/IMREAL.php';
54
    }
55
56
    #[DataProvider('providerUnhappyIMREAL')]
57
    public function testIMREALUnhappyPath(string $expectedException, mixed ...$args): void
58
    {
59
        $arguments = new FormulaArguments(...$args);
60
61
        $worksheet = $this->getSheet();
62
        $argumentCells = $arguments->populateWorksheet($worksheet);
63
        $formula = "=IMREAL({$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 providerUnhappyIMREAL(): array
73
    {
74
        return [
75
            ['Formula Error: Wrong number of arguments for IMREAL() function'],
76
        ];
77
    }
78
79
    #[DataProvider('providerImRealArray')]
80
    public function testImRealArray(array $expectedResult, string $complex): void
81
    {
82
        $calculation = Calculation::getInstance();
83
84
        $formula = "=IMREAL({$complex})";
85
        $result = $calculation->calculateFormula($formula);
86
        self::assertEquals($expectedResult, $result);
87
    }
88
89
    public static function providerImRealArray(): array
90
    {
91
        return [
92
            'row/column vector' => [
93
                [
94
                    [-1.0, 0.0, 1.0],
95
                    [-1.0, 0.0, 1.0],
96
                    [-1.0, 0.0, 1.0],
97
                    [-1.0, 0.0, 1.0],
98
                ],
99
                '{"-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"}',
100
            ],
101
        ];
102
    }
103
}
104