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

ImLnTest::trimIfQuoted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\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 ImLnTest extends ComplexAssert
16
{
17
    #[DataProvider('providerIMLN')]
18
    public function testDirectCallToIMLN(string $expectedResult, string $arg): void
19
    {
20
        $result = ComplexFunctions::IMLN($arg);
21
        $this->assertComplexEquals($expectedResult, $result);
22
    }
23
24
    #[DataProvider('providerIMLN')]
25
    public function testIMLNAsFormula(mixed $expectedResult, mixed ...$args): void
26
    {
27
        $arguments = new FormulaArguments(...$args);
28
29
        $calculation = Calculation::getInstance();
30
        $formula = "=IMLN({$arguments})";
31
32
        /** @var float|int|string */
33
        $result = $calculation->calculateFormula($formula);
34
        $this->assertComplexEquals($expectedResult, $result);
35
    }
36
37
    #[DataProvider('providerIMLN')]
38
    public function testIMLNInWorksheet(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 = "=IMLN({$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 providerIMLN(): array
56
    {
57
        return require 'tests/data/Calculation/Engineering/IMLN.php';
58
    }
59
60
    #[DataProvider('providerUnhappyIMLN')]
61
    public function testIMLNUnhappyPath(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 = "=IMLN({$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 providerUnhappyIMLN(): array
80
    {
81
        return [
82
            ['Formula Error: Wrong number of arguments for IMLN() function'],
83
        ];
84
    }
85
86
    #[DataProvider('providerImLnArray')]
87
    public function testImLnArray(array $expectedResult, string $complex): void
88
    {
89
        $calculation = Calculation::getInstance();
90
91
        $formula = "=IMLN({$complex})";
92
        $result = $calculation->calculateFormula($formula);
93
        self::assertEquals($expectedResult, $result);
94
    }
95
96
    public static function providerImLnArray(): array
97
    {
98
        return [
99
            'row/column vector' => [
100
                [
101
                    ['0.99050073443329-1.9513027039073i', '0.91629073187416-1.5707963267949i', '0.99050073443329-1.1902899496825i'],
102
                    ['0.34657359027997-2.3561944901923i', '-1.5707963267949i', '0.34657359027997-0.78539816339745i'],
103
                    ['0.34657359027997+2.3561944901923i', '1.5707963267949i', '0.34657359027997+0.78539816339745i'],
104
                    ['0.99050073443329+1.9513027039073i', '0.91629073187416+1.5707963267949i', '0.99050073443329+1.1902899496825i'],
105
                ],
106
                '{"-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"}',
107
            ],
108
        ];
109
    }
110
}
111