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

ComplexTest::testCOMPLEXAsFormula()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
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\Spreadsheet;
10
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
11
use PHPUnit\Framework\Attributes\DataProvider;
12
use PHPUnit\Framework\TestCase;
13
14
class ComplexTest extends TestCase
15
{
16
    #[DataProvider('providerCOMPLEX')]
17
    public function testDirectCallToCOMPLEX(mixed $expectedResult, mixed ...$args): void
18
    {
19
        $result = Complex::complex(...$args);
20
        self::assertSame($expectedResult, $result);
21
    }
22
23
    #[DataProvider('providerCOMPLEX')]
24
    public function testCOMPLEXAsFormula(mixed $expectedResult, mixed ...$args): void
25
    {
26
        $arguments = new FormulaArguments(...$args);
27
28
        $calculation = Calculation::getInstance();
29
        $formula = "=COMPLEX({$arguments})";
30
31
        /** @var float|int|string */
32
        $result = $calculation->calculateFormula($formula);
33
        self::assertSame($expectedResult, $result);
34
    }
35
36
    #[DataProvider('providerCOMPLEX')]
37
    public function testCOMPLEXInWorksheet(mixed $expectedResult, mixed ...$args): void
38
    {
39
        $arguments = new FormulaArguments(...$args);
40
41
        $spreadsheet = new Spreadsheet();
42
        $worksheet = $spreadsheet->getActiveSheet();
43
        $argumentCells = $arguments->populateWorksheet($worksheet);
44
        $formula = "=COMPLEX({$argumentCells})";
45
46
        $result = $worksheet->setCellValue('A1', $formula)
47
            ->getCell('A1')
48
            ->getCalculatedValue();
49
        self::assertSame($expectedResult, $result);
50
51
        $spreadsheet->disconnectWorksheets();
52
    }
53
54
    public static function providerCOMPLEX(): array
55
    {
56
        return require 'tests/data/Calculation/Engineering/COMPLEX.php';
57
    }
58
59
    /** @param mixed[] $expectedResult */
60
    #[DataProvider('providerComplexArray')]
61
    public function testComplexArray(array $expectedResult, string $real, string $imaginary): void
62
    {
63
        $calculation = Calculation::getInstance();
64
65
        $formula = "=COMPLEX({$real}, {$imaginary})";
66
        $result = $calculation->calculateFormula($formula);
67
        self::assertEquals($expectedResult, $result);
68
    }
69
70
    public static function providerComplexArray(): array
71
    {
72
        return [
73
            'row/column vector' => [
74
                [
75
                    ['-2.5-2.5i', '-1-2.5i', '-2.5i', '1-2.5i', '2.5-2.5i'],
76
                    ['-2.5-i', '-1-i', '-i', '1-i', '2.5-i'],
77
                    ['-2.5', '-1', '0.0', '1', '2.5'],
78
                    ['-2.5+i', '-1+i', 'i', '1+i', '2.5+i'],
79
                    ['-2.5+2.5i', '-1+2.5i', '2.5i', '1+2.5i', '2.5+2.5i'],
80
                ],
81
                '{-2.5, -1, 0, 1, 2.5}',
82
                '{-2.5; -1; 0; 1; 2.5}',
83
            ],
84
        ];
85
    }
86
}
87