Passed
Push — master ( bd792e...df3a06 )
by
unknown
15:51 queued 08:04
created

AllSetupTeardown::setArrayAsArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
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
    protected string $compatibilityMode;
18
19
    protected string $arrayReturnType;
20
21
    protected ?Spreadsheet $spreadsheet = null;
22
23
    private ?Worksheet $sheet = null;
24
25
    protected function setUp(): void
26
    {
27
        $this->compatibilityMode = Functions::getCompatibilityMode();
28
        $this->arrayReturnType = Calculation::getArrayReturnType();
29
    }
30
31
    protected function tearDown(): void
32
    {
33
        Functions::setCompatibilityMode($this->compatibilityMode);
34
        Calculation::setArrayReturnType($this->arrayReturnType);
35
        $this->sheet = null;
36
        if ($this->spreadsheet !== null) {
37
            $this->spreadsheet->disconnectWorksheets();
38
            $this->spreadsheet = null;
39
        }
40
    }
41
42
    protected static function setOpenOffice(): void
43
    {
44
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
45
    }
46
47
    protected static function setGnumeric(): void
48
    {
49
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
50
    }
51
52
    protected function mightHaveException(mixed $expectedResult): void
53
    {
54
        if ($expectedResult === 'exception') {
55
            $this->expectException(CalcException::class);
56
        }
57
    }
58
59
    protected function setCell(string $cell, mixed $value): void
60
    {
61
        if ($value !== null) {
62
            if (is_string($value) && is_numeric($value)) {
63
                $this->getSheet()->getCell($cell)->setValueExplicit($value, DataType::TYPE_STRING);
64
            } else {
65
                $this->getSheet()->getCell($cell)->setValue($value);
66
            }
67
        }
68
    }
69
70
    protected function getSpreadsheet(): Spreadsheet
71
    {
72
        if ($this->spreadsheet !== null) {
73
            return $this->spreadsheet;
74
        }
75
        $this->spreadsheet = new Spreadsheet();
76
77
        return $this->spreadsheet;
78
    }
79
80
    protected function getSheet(): Worksheet
81
    {
82
        if ($this->sheet !== null) {
83
            return $this->sheet;
84
        }
85
        $this->sheet = $this->getSpreadsheet()->getActiveSheet();
86
87
        return $this->sheet;
88
    }
89
90
    protected function setArrayAsValue(): void
91
    {
92
        $spreadsheet = $this->getSpreadsheet();
93
        $calculation = Calculation::getInstance($spreadsheet);
94
        $calculation->setInstanceArrayReturnType(
95
            Calculation::RETURN_ARRAY_AS_VALUE
96
        );
97
    }
98
99
    protected function setArrayAsArray(): void
100
    {
101
        $spreadsheet = $this->getSpreadsheet();
102
        $calculation = Calculation::getInstance($spreadsheet);
103
        $calculation->setInstanceArrayReturnType(
104
            Calculation::RETURN_ARRAY_AS_ARRAY
105
        );
106
    }
107
}
108