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

DCountTest::testDCountAsWorksheetFormula()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Database\DCount;
6
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7
8
class DCountTest extends AllSetupTeardown
9
{
10
    /**
11
     * @dataProvider providerDCount
12
     *
13
     * @param mixed $expectedResult
14
     * @param mixed $database
15
     * @param mixed $field
16
     * @param mixed $criteria
17
     */
18
    public function testDirectCallToDCount($expectedResult, $database, $field, $criteria): void
19
    {
20
        $result = DCount::evaluate($database, $field, $criteria);
21
        self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
22
    }
23
24
    /**
25
     * @dataProvider providerDCount
26
     *
27
     * @param mixed $expectedResult
28
     * @param mixed $database
29
     * @param mixed $field
30
     * @param mixed $criteria
31
     */
32
    public function testDCountAsWorksheetFormula($expectedResult, $database, $field, $criteria): void
33
    {
34
        $this->prepareWorksheetWithFormula('DCOUNT', $database, $field, $criteria);
35
36
        $result = $this->getSheet()->getCell(self::RESULT_CELL)->getCalculatedValue();
37
        self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
38
    }
39
40
    private function database4(): array
41
    {
42
        return [
43
            ['Status', 'Value'],
44
            [false, 1],
45
            [true, 2],
46
            [true, 4],
47
            [false, 8],
48
            [true, 16],
49
            [false, 32],
50
            [false, 64],
51
            [false, 128],
52
        ];
53
    }
54
55
    public function providerDCount(): array
56
    {
57
        return [
58
            [
59
                1,
60
                $this->database1(),
61
                'Age',
62
                [
63
                    ['Tree', 'Height', 'Height'],
64
                    ['=Apple', '>10', '<16'],
65
                ],
66
            ],
67
            [
68
                1,
69
                $this->database3(),
70
                'Score',
71
                [
72
                    ['Subject', 'Gender'],
73
                    ['Science', 'Male'],
74
                ],
75
            ],
76
            [
77
                1,
78
                $this->database3(),
79
                'Score',
80
                [
81
                    ['Subject', 'Gender'],
82
                    ['Math', 'Female'],
83
                ],
84
            ],
85
            [
86
                3,
87
                $this->database4(),
88
                'Value',
89
                [
90
                    ['Status'],
91
                    [true],
92
                ],
93
            ],
94
            [
95
                5,
96
                $this->database4(),
97
                'Value',
98
                [
99
                    ['Status'],
100
                    ['<>true'],
101
                ],
102
            ],
103
            'field column number okay' => [
104
                0,
105
                $this->database1(),
106
                1,
107
                $this->database1(),
108
            ],
109
            'omitted field name' => [
110
                ExcelError::VALUE(),
111
                $this->database3(),
112
                null,
113
                [
114
                    ['Subject', 'Score'],
115
                    ['English', '>63%'],
116
                ],
117
            ],
118
            /* Excel seems to return #NAME? when column number
119
               is too high or too low. This makes so little sense
120
               to me that I'm not going to bother coding that up,
121
               content to return #VALUE! as an invalid name would */
122
            'field column number too high' => [
123
                ExcelError::VALUE(),
124
                $this->database1(),
125
                99,
126
                $this->database1(),
127
            ],
128
            'field column number too low' => [
129
                ExcelError::VALUE(),
130
                $this->database1(),
131
                0,
132
                $this->database1(),
133
            ],
134
        ];
135
    }
136
}
137