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

DayTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 57
dl 0
loc 165
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 5 1
A setUp() 0 5 1
A testDirectCallToDAY() 0 5 1
A testDAYAsFormula() 0 9 1
A testDirectCallToDAYOpenOffice() 0 7 1
A testDAYInWorksheet() 0 15 1
A providerDayArray() 0 6 1
A providerDAYOpenOffice() 0 3 1
A providerDAY() 0 3 1
A providerUnhappyDAY() 0 4 1
A testDAYUnhappyPath() 0 16 1
A testDayArray() 0 7 1
A testDirectCallToDAYWithNull() 0 4 1
A testDAYAsFormulaOpenOffice() 0 11 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\DateParts;
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 PHPUnit\Framework\TestCase;
12
13
class DayTest extends TestCase
14
{
15
    /**
16
     * @var string
17
     */
18
    private $compatibilityMode;
19
20
    protected function setUp(): void
21
    {
22
        parent::setUp();
23
24
        $this->compatibilityMode = Functions::getCompatibilityMode();
25
    }
26
27
    protected function tearDown(): void
28
    {
29
        parent::tearDown();
30
31
        Functions::setCompatibilityMode($this->compatibilityMode);
32
    }
33
34
    /**
35
     * @dataProvider providerDAY
36
     *
37
     * @param mixed $expectedResultExcel
38
     */
39
    public function testDirectCallToDAY($expectedResultExcel, ...$args): void
40
    {
41
        /** @scrutinizer ignore-call */
42
        $result = DateParts::day(...$args);
43
        self::assertSame($expectedResultExcel, $result);
44
    }
45
46
    /**
47
     * @dataProvider providerDAY
48
     *
49
     * @param mixed $expectedResultExcel
50
     */
51
    public function testDAYAsFormula($expectedResultExcel, ...$args): void
52
    {
53
        $arguments = new FormulaArguments(...$args);
54
55
        $calculation = Calculation::getInstance();
56
        $formula = "=DAY({$arguments})";
57
58
        $result = $calculation->_calculateFormulaValue($formula);
59
        self::assertSame($expectedResultExcel, $result);
60
    }
61
62
    /**
63
     * @dataProvider providerDAY
64
     *
65
     * @param mixed $expectedResult
66
     */
67
    public function testDAYInWorksheet($expectedResult, ...$args): void
68
    {
69
        $arguments = new FormulaArguments(...$args);
70
71
        $spreadsheet = new Spreadsheet();
72
        $worksheet = $spreadsheet->getActiveSheet();
73
        $argumentCells = $arguments->populateWorksheet($worksheet);
74
        $formula = "=DAY({$argumentCells})";
75
76
        $result = $worksheet->setCellValue('A1', $formula)
77
            ->getCell('A1')
78
            ->getCalculatedValue();
79
        self::assertSame($expectedResult, $result);
80
81
        $spreadsheet->disconnectWorksheets();
82
    }
83
84
    public function providerDAY(): array
85
    {
86
        return require 'tests/data/Calculation/DateTime/DAY.php';
87
    }
88
89
    /**
90
     * @dataProvider providerDAYOpenOffice
91
     *
92
     * @param mixed $expectedResultOpenOffice
93
     */
94
    public function testDirectCallToDAYOpenOffice($expectedResultOpenOffice, ...$args): void
95
    {
96
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
97
98
        /** @scrutinizer ignore-call */
99
        $result = DateParts::day(...$args);
100
        self::assertSame($expectedResultOpenOffice, $result);
101
    }
102
103
    /**
104
     * @dataProvider providerDAYOpenOffice
105
     *
106
     * @param mixed $expectedResultOpenOffice
107
     */
108
    public function testDAYAsFormulaOpenOffice($expectedResultOpenOffice, ...$args): void
109
    {
110
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
111
112
        $arguments = new FormulaArguments(...$args);
113
114
        $calculation = Calculation::getInstance();
115
        $formula = "=DAY({$arguments})";
116
117
        $result = $calculation->_calculateFormulaValue($formula);
118
        self::assertSame($expectedResultOpenOffice, $result);
119
    }
120
121
    public function providerDAYOpenOffice(): array
122
    {
123
        return require 'tests/data/Calculation/DateTime/DAYOpenOffice.php';
124
    }
125
126
    /**
127
     * @dataProvider providerUnhappyDAY
128
     */
129
    public function testDAYUnhappyPath(string $expectedException, ...$args): void
130
    {
131
        $arguments = new FormulaArguments(...$args);
132
133
        $spreadsheet = new Spreadsheet();
134
        $worksheet = $spreadsheet->getActiveSheet();
135
        $argumentCells = $arguments->populateWorksheet($worksheet);
136
        $formula = "=DAY({$argumentCells})";
137
138
        $this->expectException(CalculationException::class);
139
        $this->expectExceptionMessage($expectedException);
140
        $worksheet->setCellValue('A1', $formula)
141
            ->getCell('A1')
142
            ->getCalculatedValue();
143
144
        $spreadsheet->disconnectWorksheets();
145
    }
146
147
    public function providerUnhappyDAY(): array
148
    {
149
        return [
150
            ['Formula Error: Wrong number of arguments for DAY() function'],
151
        ];
152
    }
153
154
    public function testDirectCallToDAYWithNull(): void
155
    {
156
        $result = DateParts::day(null);
157
        self::assertSame(0, $result);
158
    }
159
160
    /**
161
     * @dataProvider providerDayArray
162
     */
163
    public function testDayArray(array $expectedResult, string $array): void
164
    {
165
        $calculation = Calculation::getInstance();
166
167
        $formula = "=DAY({$array})";
168
        $result = $calculation->_calculateFormulaValue($formula);
169
        self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
170
    }
171
172
    public function providerDayArray(): array
173
    {
174
        return [
175
            'row vector' => [[[1, 12, 22]], '{"2022-01-01", "2022-06-12", "2023-07-22"}'],
176
            'column vector' => [[[1], [3], [6]], '{"2022-01-01"; "2022-01-03"; "2022-01-06"}'],
177
            'matrix' => [[[1, 10], [15, 31]], '{"2022-01-01", "2022-01-10"; "2022-08-15", "2022-12-31"}'],
178
        ];
179
    }
180
}
181