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

ImCosTest::testIMCOSUnhappyPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 17
rs 9.8666
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 ImCosTest extends ComplexAssert
16
{
17
    #[DataProvider('providerIMCOS')]
18
    public function testDirectCallToIMCOS(string $expectedResult, string $arg): void
19
    {
20
        $result = ComplexFunctions::IMCOS($arg);
21
        $this->assertComplexEquals($expectedResult, $result);
22
    }
23
24
    #[DataProvider('providerIMCOS')]
25
    public function testIMCOSAsFormula(mixed $expectedResult, mixed ...$args): void
26
    {
27
        $arguments = new FormulaArguments(...$args);
28
29
        $calculation = Calculation::getInstance();
30
        $formula = "=IMCOS({$arguments})";
31
32
        /** @var float|int|string */
33
        $result = $calculation->calculateFormula($formula);
34
        $this->assertComplexEquals($expectedResult, $result);
35
    }
36
37
    #[DataProvider('providerIMCOS')]
38
    public function testIMCOSInWorksheet(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 = "=IMCOS({$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 providerIMCOS(): array
56
    {
57
        return require 'tests/data/Calculation/Engineering/IMCOS.php';
58
    }
59
60
    #[DataProvider('providerUnhappyIMCOS')]
61
    public function testIMCOSUnhappyPath(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 = "=IMCOS({$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 providerUnhappyIMCOS(): array
80
    {
81
        return [
82
            ['Formula Error: Wrong number of arguments for IMCOS() function'],
83
        ];
84
    }
85
86
    #[DataProvider('providerImCosArray')]
87
    public function testImCosArray(array $expectedResult, string $complex): void
88
    {
89
        $calculation = Calculation::getInstance();
90
91
        $formula = "=IMCOS({$complex})";
92
        $result = $calculation->calculateFormula($formula);
93
        self::assertEquals($expectedResult, $result);
94
    }
95
96
    public static function providerImCosArray(): array
97
    {
98
        return [
99
            'row/column vector' => [
100
                [
101
                    ['3.3132901461132-5.0910715229497i', 6.1322894796637, '3.3132901461132+5.0910715229497i'],
102
                    ['0.83373002513115-0.98889770576287i', 1.5430806348152, '0.83373002513115+0.98889770576287i'],
103
                    ['0.83373002513115+0.98889770576287i', 1.5430806348152, '0.83373002513115-0.98889770576287i'],
104
                    ['3.3132901461132+5.0910715229497i', 6.1322894796637, '3.3132901461132-5.0910715229497i'],
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