Passed
Pull Request — master (#3234)
by Mark
13:39
created

DAverageTest::testDAverage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Database\DAverage;
6
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7
8
class DAverageTest extends AllSetupTeardown
9
{
10
    /**
11
     * @dataProvider providerDAverage
12
     *
13
     * @param mixed $expectedResult
14
     * @param mixed $database
15
     * @param mixed $field
16
     * @param mixed $criteria
17
     */
18
    public function testDirectCallToDAverage($expectedResult, $database, $field, $criteria): void
19
    {
20
        $result = DAverage::evaluate($database, $field, $criteria);
21
        self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
22
    }
23
24
    /**
25
     * @dataProvider providerDAverage
26
     *
27
     * @param mixed $expectedResult
28
     * @param int|string $field
29
     */
30
    public function testDAverageAsWorksheetFormula($expectedResult, array $database, $field, array $criteria): void
31
    {
32
        $this->prepareWorksheetWithFormula('DAVERAGE', $database, $field, $criteria);
33
34
        $result = $this->getSheet()->getCell(self::RESULT_CELL)->getCalculatedValue();
35
        self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
36
    }
37
38
    public function providerDAverage(): array
39
    {
40
        return [
41
            [
42
                12,
43
                $this->database1(),
44
                'Yield',
45
                [
46
                    ['Tree', 'Height'],
47
                    ['=Apple', '>10'],
48
                ],
49
            ],
50
            [
51
                268333.333333333333,
52
                $this->database2(),
53
                'Sales',
54
                [
55
                    ['Quarter', 'Sales Rep.'],
56
                    ['>1', 'Tina'],
57
                ],
58
            ],
59
            [
60
                372500,
61
                $this->database2(),
62
                'Sales',
63
                [
64
                    ['Quarter', 'Area'],
65
                    ['1', 'South'],
66
                ],
67
            ],
68
            'numeric column, in this case referring to age' => [
69
                13,
70
                $this->database1(),
71
                3,
72
                $this->database1(),
73
            ],
74
            'null field' => [
75
                ExcelError::VALUE(),
76
                $this->database1(),
77
                null,
78
                $this->database1(),
79
            ],
80
            'field unknown column' => [
81
                ExcelError::VALUE(),
82
                $this->database1(),
83
                'xyz',
84
                $this->database1(),
85
            ],
86
            'multiple criteria, omit equal sign' => [
87
                10.5,
88
                $this->database1(),
89
                'Yield',
90
                [
91
                    ['Tree', 'Height'],
92
                    ['=Apple', '>10'],
93
                    ['Pear'],
94
                ],
95
            ],
96
            'multiple criteria for same field' => [
97
                10,
98
                $this->database1(),
99
                'Yield',
100
                [
101
                    ['Tree', 'Height', 'Age', 'Height'],
102
                    ['=Apple', '>10', null, '<16'],
103
                ],
104
            ],
105
            /* Excel seems to return #NAME? when column number
106
               is too high or too low. This makes so little sense
107
               to me that I'm not going to bother coding that up,
108
               content to return #VALUE! as an invalid name would */
109
            'field column number too high' => [
110
                ExcelError::VALUE(),
111
                $this->database1(),
112
                99,
113
                $this->database1(),
114
            ],
115
            'field column number too low' => [
116
                ExcelError::VALUE(),
117
                $this->database1(),
118
                0,
119
                $this->database1(),
120
            ],
121
        ];
122
    }
123
}
124