Passed
Pull Request — master (#3448)
by Mark
22:11
created

ImSubTest::testIMSUBInWorksheet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 18
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Calculation\Engineering\ComplexOperations;
7
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
8
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
9
use PhpOffice\PhpSpreadsheet\Spreadsheet;
10
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
11
use PhpOffice\PhpSpreadsheetTests\Custom\ComplexAssert;
12
use PHPUnit\Framework\TestCase;
13
14
class ImSubTest extends TestCase
15
{
16
    const COMPLEX_PRECISION = 1E-12;
17
18
    /**
19
     * @var ComplexAssert
20
     */
21
    private $complexAssert;
22
23
    protected function setUp(): void
24
    {
25
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
26
        $this->complexAssert = new ComplexAssert();
27
    }
28
29
    /**
30
     * @dataProvider providerIMSUB
31
     *
32
     * @param mixed $expectedResult
33
     */
34
    public function testDirectCallToIMSUB($expectedResult, ...$args): void
35
    {
36
        /** @scrutinizer ignore-call */
37
        $result = ComplexOperations::IMSUB(...$args);
38
        self::assertTrue(
39
            $this->complexAssert->assertComplexEquals($expectedResult, $result, self::COMPLEX_PRECISION),
40
            $this->complexAssert->getErrorMessage()
41
        );
42
    }
43
44
    private function trimIfQuoted(string $value): string
45
    {
46
        return trim($value, '"');
47
    }
48
49
    /**
50
     * @dataProvider providerIMSUB
51
     *
52
     * @param mixed $expectedResult
53
     */
54
    public function testIMSUBAsFormula($expectedResult, ...$args): void
55
    {
56
        $arguments = new FormulaArguments(...$args);
57
58
        $calculation = Calculation::getInstance();
59
        $formula = "=IMSUB({$arguments})";
60
61
        $result = $calculation->_calculateFormulaValue($formula);
62
        self::assertTrue(
63
            $this->complexAssert->assertComplexEquals($expectedResult, $this->trimIfQuoted((string) $result), self::COMPLEX_PRECISION),
64
            $this->complexAssert->getErrorMessage()
65
        );
66
    }
67
68
    /**
69
     * @dataProvider providerIMSUB
70
     *
71
     * @param mixed $expectedResult
72
     */
73
    public function testIMSUBInWorksheet($expectedResult, ...$args): void
74
    {
75
        $arguments = new FormulaArguments(...$args);
76
77
        $spreadsheet = new Spreadsheet();
78
        $worksheet = $spreadsheet->getActiveSheet();
79
        $argumentCells = $arguments->populateWorksheet($worksheet);
80
        $formula = "=IMSUB({$argumentCells})";
81
82
        $result = $worksheet->setCellValue('A1', $formula)
83
            ->getCell('A1')
84
            ->getCalculatedValue();
85
        self::assertTrue(
86
            $this->complexAssert->assertComplexEquals($expectedResult, $result, self::COMPLEX_PRECISION),
87
            $this->complexAssert->getErrorMessage()
88
        );
89
90
        $spreadsheet->disconnectWorksheets();
91
    }
92
93
    public function providerIMSUB(): array
94
    {
95
        return require 'tests/data/Calculation/Engineering/IMSUB.php';
96
    }
97
98
    /**
99
     * @dataProvider providerUnhappyIMSUB
100
     */
101
    public function testIMSUBUnhappyPath(string $expectedException, ...$args): void
102
    {
103
        $arguments = new FormulaArguments(...$args);
104
105
        $spreadsheet = new Spreadsheet();
106
        $worksheet = $spreadsheet->getActiveSheet();
107
        $argumentCells = $arguments->populateWorksheet($worksheet);
108
        $formula = "=IMSUB({$argumentCells})";
109
110
        $this->expectException(CalculationException::class);
111
        $this->expectExceptionMessage($expectedException);
112
        $worksheet->setCellValue('A1', $formula)
113
            ->getCell('A1')
114
            ->getCalculatedValue();
115
116
        $spreadsheet->disconnectWorksheets();
117
    }
118
119
    public function providerUnhappyIMSUB(): array
120
    {
121
        return [
122
            ['Formula Error: Wrong number of arguments for IMSUB() function'],
123
            ['Formula Error: Wrong number of arguments for IMSUB() function', '1.23+4.56i'],
124
        ];
125
    }
126
127
    /**
128
     * @dataProvider providerImSubArray
129
     */
130
    public function testImSubArray(array $expectedResult, string $subidend, string $subisor): void
131
    {
132
        $calculation = Calculation::getInstance();
133
134
        $formula = "=IMSUB({$subidend}, {$subisor})";
135
        $result = $calculation->_calculateFormulaValue($formula);
136
        self::assertEquals($expectedResult, $result);
137
    }
138
139
    public function providerImSubArray(): array
140
    {
141
        return [
142
            'matrix' => [
143
                [
144
                    ['1-7.5i', '-2-2.5i', '-1-4.5i'],
145
                    ['1-6i', '-2-i', '-1-3i'],
146
                    ['1-4i', '-2+i', '-1-i'],
147
                    ['1-2.5i', '-2+2.5i', '-1+0.5i'],
148
                ],
149
                '{"-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"}',
150
                '{"-2+5i", 2, "2+2i"}',
151
            ],
152
        ];
153
    }
154
}
155