Failed Conditions
Pull Request — master (#4328)
by Owen
15:26 queued 04:43
created

AllSetupTeardown   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 45
dl 0
loc 96
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 7 2
A runTestCase() 0 17 2
A setOpenOffice() 0 3 1
A mightHaveException() 0 4 2
A getSheet() 0 8 2
A getSpreadsheet() 0 8 2
A setCell() 0 7 4
A setGnumeric() 0 3 1
A setArrayAsValue() 0 6 1
A setUp() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Logical;
6
7
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
8
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
9
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
10
use PhpOffice\PhpSpreadsheet\Cell\DataType;
11
use PhpOffice\PhpSpreadsheet\Spreadsheet;
12
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
13
use PHPUnit\Framework\TestCase;
14
15
class AllSetupTeardown extends TestCase
16
{
17
    private string $compatibilityMode;
18
19
    private ?Spreadsheet $spreadsheet = null;
20
21
    private ?Worksheet $sheet = null;
22
23
    protected function setUp(): void
24
    {
25
        $this->compatibilityMode = Functions::getCompatibilityMode();
26
    }
27
28
    protected function tearDown(): void
29
    {
30
        Functions::setCompatibilityMode($this->compatibilityMode);
31
        $this->sheet = null;
32
        if ($this->spreadsheet !== null) {
33
            $this->spreadsheet->disconnectWorksheets();
34
            $this->spreadsheet = null;
35
        }
36
    }
37
38
    protected static function setOpenOffice(): void
39
    {
40
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
41
    }
42
43
    protected static function setGnumeric(): void
44
    {
45
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
46
    }
47
48
    protected function mightHaveException(mixed $expectedResult): void
49
    {
50
        if ($expectedResult === 'exception') {
51
            $this->expectException(CalcException::class);
52
        }
53
    }
54
55
    protected function setCell(string $cell, mixed $value): void
56
    {
57
        if ($value !== null) {
58
            if (is_string($value) && is_numeric($value)) {
59
                $this->getSheet()->getCell($cell)->setValueExplicit($value, DataType::TYPE_STRING);
60
            } else {
61
                $this->getSheet()->getCell($cell)->setValue($value);
62
            }
63
        }
64
    }
65
66
    protected function getSpreadsheet(): Spreadsheet
67
    {
68
        if ($this->spreadsheet !== null) {
69
            return $this->spreadsheet;
70
        }
71
        $this->spreadsheet = new Spreadsheet();
72
73
        return $this->spreadsheet;
74
    }
75
76
    protected function getSheet(): Worksheet
77
    {
78
        if ($this->sheet !== null) {
79
            return $this->sheet;
80
        }
81
        $this->sheet = $this->getSpreadsheet()->getActiveSheet();
82
83
        return $this->sheet;
84
    }
85
86
    protected function runTestCase(string $functionName, mixed $expectedResult, mixed ...$args): void
87
    {
88
        $this->mightHaveException($expectedResult);
89
        $sheet = $this->getSheet();
90
        $formula = "=$functionName(";
91
        $comma = '';
92
        $row = 0;
93
        foreach ($args as $arg) {
94
            ++$row;
95
            $cellId = "A$row";
96
            $formula .= "$comma$cellId";
97
            $comma = ',';
98
            $this->setCell($cellId, $arg);
99
        }
100
        $formula .= ')';
101
        $this->setCell('B1', $formula);
102
        self::assertSame($expectedResult, $sheet->getCell('B1')->getCalculatedValue());
103
    }
104
105
    protected function setArrayAsValue(): void
106
    {
107
        $spreadsheet = $this->getSpreadsheet();
108
        $calculation = Calculation::getInstance($spreadsheet);
109
        $calculation->setInstanceArrayReturnType(
110
            Calculation::RETURN_ARRAY_AS_VALUE
111
        );
112
    }
113
}
114