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

ImSinTest::testIMSINInWorksheet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 16
rs 9.9
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\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 ImSinTest extends ComplexAssert
16
{
17
    #[DataProvider('providerIMSIN')]
18
    public function testDirectCallToIMSIN(string $expectedResult, string $arg): void
19
    {
20
        $result = ComplexFunctions::IMSIN($arg);
21
        $this->assertComplexEquals($expectedResult, $result);
22
    }
23
24
    #[DataProvider('providerIMSIN')]
25
    public function testIMSINAsFormula(mixed $expectedResult, mixed ...$args): void
26
    {
27
        $arguments = new FormulaArguments(...$args);
28
29
        $calculation = Calculation::getInstance();
30
        $formula = "=IMSIN({$arguments})";
31
32
        /** @var float|int|string */
33
        $result = $calculation->calculateFormula($formula);
34
        $this->assertComplexEquals($expectedResult, $result);
35
    }
36
37
    #[DataProvider('providerIMSIN')]
38
    public function testIMSINInWorksheet(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 = "=IMSIN({$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 providerIMSIN(): array
56
    {
57
        return require 'tests/data/Calculation/Engineering/IMSIN.php';
58
    }
59
60
    #[DataProvider('providerUnhappyIMSIN')]
61
    public function testIMSINUnhappyPath(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 = "=IMSIN({$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 providerUnhappyIMSIN(): array
80
    {
81
        return [
82
            ['Formula Error: Wrong number of arguments for IMSIN() function'],
83
        ];
84
    }
85
86
    #[DataProvider('providerImSinArray')]
87
    public function testImSinArray(array $expectedResult, string $complex): void
88
    {
89
        $calculation = Calculation::getInstance();
90
91
        $formula = "=IMSIN({$complex})";
92
        $result = $calculation->calculateFormula($formula);
93
        self::assertEquals($expectedResult, $result);
94
    }
95
96
    public static function providerImSinArray(): array
97
    {
98
        return [
99
            'row/column vector' => [
100
                [
101
                    ['-5.1601436675797-3.2689394320795i', '-6.0502044810398i', '5.1601436675797-3.2689394320795i'],
102
                    ['-1.298457581416-0.63496391478474i', '-1.1752011936438i', '1.298457581416-0.63496391478474i'],
103
                    ['-1.298457581416+0.63496391478474i', '1.1752011936438i', '1.298457581416+0.63496391478474i'],
104
                    ['-5.1601436675797+3.2689394320795i', '6.0502044810398i', '5.1601436675797+3.2689394320795i'],
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