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

NetworkDaysTest::testNETWORKDAYS()   B

Complexity

Conditions 10
Paths 144

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 24
nc 144
nop 4
dl 0
loc 32
rs 7.2999
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A NetworkDaysTest::testNETWORKDAYSAsFormula() 0 9 1
A NetworkDaysTest::testDirectCallToNETWORKDAYS() 0 4 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\NetworkDays;
7
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
9
use PHPUnit\Framework\TestCase;
10
11
class NetworkDaysTest extends TestCase
12
{
13
    /**
14
     * @dataProvider providerNETWORKDAYS
15
     *
16
     * @param mixed $expectedResult
17
     */
18
    public function testDirectCallToNETWORKDAYS($expectedResult, ...$args): void
19
    {
20
        $result = NetworkDays::count(...$args);
21
        self::assertSame($expectedResult, $result);
22
    }
23
24
    /**
25
     * @dataProvider providerNETWORKDAYS
26
     *
27
     * @param mixed $expectedResult
28
     */
29
    public function testNETWORKDAYSAsFormula($expectedResult, ...$args): void
30
    {
31
        $arguments = new FormulaArguments(...$args);
32
33
        $calculation = Calculation::getInstance();
34
        $formula = "=NETWORKDAYS({$arguments})";
35
36
        $result = $calculation->_calculateFormulaValue($formula);
37
        self::assertSame($expectedResult, $result);
38
    }
39
40
    /**
41
     * @dataProvider providerNETWORKDAYS
42
     *
43
     * @param mixed $expectedResult
44
     */
45
    public function testNETWORKDAYSInWorksheet($expectedResult, ...$args): void
46
    {
47
        $arguments = new FormulaArguments(...$args);
48
49
        $spreadsheet = new Spreadsheet();
50
        $worksheet = $spreadsheet->getActiveSheet();
51
        $argumentCells = $arguments->populateWorksheet($worksheet);
52
        $formula = "=NETWORKDAYS({$argumentCells})";
53
54
        $result = $worksheet->setCellValue('A1', $formula)
55
            ->getCell('A1')
56
            ->getCalculatedValue();
57
        self::assertSame($expectedResult, $result);
58
59
        $spreadsheet->disconnectWorksheets();
60
    }
61
62
    public function providerNETWORKDAYS(): array
63
    {
64
        return require 'tests/data/Calculation/DateTime/NETWORKDAYS.php';
65
    }
66
67
    /**
68
     * @dataProvider providerUnhappyNETWORKDAYS
69
     */
70
    public function testNETWORKDAYSUnhappyPath(string $expectedException, ...$args): void
71
    {
72
        $arguments = new FormulaArguments(...$args);
73
74
        $spreadsheet = new Spreadsheet();
75
        $worksheet = $spreadsheet->getActiveSheet();
76
        $argumentCells = $arguments->populateWorksheet($worksheet);
77
        $formula = "=NETWORKDAYS({$argumentCells})";
78
79
        $this->expectException(\PhpOffice\PhpSpreadsheet\Calculation\Exception::class);
80
        $this->expectExceptionMessage($expectedException);
81
        $worksheet->setCellValue('A1', $formula)
82
            ->getCell('A1')
83
            ->getCalculatedValue();
84
85
        $spreadsheet->disconnectWorksheets();
86
    }
87
88
    public function providerUnhappyNETWORKDAYS(): array
89
    {
90
        return [
91
            ['Formula Error: Wrong number of arguments for NETWORKDAYS() function'],
92
            ['Formula Error: Wrong number of arguments for NETWORKDAYS() function', '2001-01-01'],
93
        ];
94
    }
95
96
    /**
97
     * @dataProvider providerNetWorkDaysArray
98
     */
99
    public function testNetWorkDaysArray(array $expectedResult, string $startDate, string $endDays, ?string $holidays): void
100
    {
101
        $calculation = Calculation::getInstance();
102
103
        if ($holidays === null) {
104
            $formula = "=NETWORKDAYS({$startDate}, {$endDays})";
105
        } else {
106
            $formula = "=NETWORKDAYS({$startDate}, {$endDays}, {$holidays})";
107
        }
108
        $result = $calculation->_calculateFormulaValue($formula);
109
        self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
110
    }
111
112
    public function providerNetWorkDaysArray(): array
113
    {
114
        return [
115
            'row vector #1' => [[[234, 233, 232]], '{"2022-02-01", "2022-02-02", "2022-02-03"}', '"2022-12-25"', null],
116
            'column vector #1' => [[[234], [233], [232]], '{"2022-02-01"; "2022-02-02"; "2022-02-03"}', '"2022-12-25"', null],
117
            'matrix #1' => [[[234, 233], [232, 231]], '{"2022-02-01", "2022-02-02"; "2022-02-03", "2022-02-04"}', '"2022-12-25"', null],
118
            'row vector #2' => [[[234, -27]], '"2022-02-01"', '{"2022-12-25", "2021-12-25"}', null],
119
            'column vector #2' => [[[234], [-27]], '"2022-02-01"', '{"2022-12-25"; "2021-12-25"}', null],
120
            'row vector with Holiday' => [[[233, -27]], '"2022-02-01"', '{"2022-12-25", "2021-12-25"}', '{"2022-02-02"}'],
121
            'row vector with Holidays' => [[[232, -27]], '"2022-02-01"', '{"2022-12-25", "2021-12-25"}', '{"2022-02-02", "2022-02-03"}'],
122
        ];
123
    }
124
}
125