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

DProductTest::testDirectCallToDProduct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
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\DProduct;
6
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
7
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
8
9
class DProductTest extends AllSetupTeardown
10
{
11
    /**
12
     * @dataProvider providerDProduct
13
     *
14
     * @param mixed $expectedResult
15
     * @param mixed $database
16
     * @param mixed $field
17
     * @param mixed $criteria
18
     */
19
    public function testDirectCallToDProduct($expectedResult, $database, $field, $criteria): void
20
    {
21
        $result = DProduct::evaluate($database, $field, $criteria);
22
        self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
23
    }
24
25
    /**
26
     * @dataProvider providerDProduct
27
     *
28
     * @param mixed $expectedResult
29
     * @param mixed $database
30
     * @param mixed $field
31
     * @param mixed $criteria
32
     */
33
    public function testDProductAsWorksheetFormula($expectedResult, $database, $field, $criteria): void
34
    {
35
        $this->prepareWorksheetWithFormula('DPRODUCT', $database, $field, $criteria);
36
37
        $result = $this->getSheet()->getCell(self::RESULT_CELL)->getCalculatedValue();
38
        self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
39
    }
40
41
    private function database5(): array
42
    {
43
        return [
44
            ['Name', 'Date', 'Test', 'Score'],
45
            ['Gary', DateTimeExcel\Helpers::getDateValue('01-Jan-2017'), 'Test1', 4],
46
            ['Gary', DateTimeExcel\Helpers::getDateValue('01-Jan-2017'), 'Test2', 4],
47
            ['Gary', DateTimeExcel\Helpers::getDateValue('01-Jan-2017'), 'Test3', 3],
48
            ['Gary', DateTimeExcel\Helpers::getDateValue('05-Jan-2017'), 'Test1', 3],
49
            ['Gary', DateTimeExcel\Helpers::getDateValue('05-Jan-2017'), 'Test2', 4],
50
            ['Gary', DateTimeExcel\Helpers::getDateValue('05-Jan-2017'), 'Test3', 3],
51
            ['Kev', DateTimeExcel\Helpers::getDateValue('02-Jan-2017'), 'Test1', 2],
52
            ['Kev', DateTimeExcel\Helpers::getDateValue('02-Jan-2017'), 'Test2', 3],
53
            ['Kev', DateTimeExcel\Helpers::getDateValue('02-Jan-2017'), 'Test3', 5],
54
            ['Kev', DateTimeExcel\Helpers::getDateValue('05-Jan-2017'), 'Test1', 3],
55
            ['Kev', DateTimeExcel\Helpers::getDateValue('05-Jan-2017'), 'Test2', 2],
56
            ['Kev', DateTimeExcel\Helpers::getDateValue('05-Jan-2017'), 'Test3', 5],
57
        ];
58
    }
59
60
    public function providerDProduct(): array
61
    {
62
        return [
63
            [
64
                800.0,
65
                $this->database1(),
66
                'Yield',
67
                [
68
                    ['Tree', 'Height', 'Height'],
69
                    ['=Apple', '>10', '<16'],
70
                    ['=Pear', null, null],
71
                ],
72
            ],
73
            [
74
                36.0,
75
                $this->database5(),
76
                'Score',
77
                [
78
                    ['Name', 'Date'],
79
                    ['Gary', '05-Jan-2017'],
80
                ],
81
            ],
82
            [
83
                8.0,
84
                $this->database5(),
85
                'Score',
86
                [
87
                    ['Test', 'Date'],
88
                    ['Test1', '<05-Jan-2017'],
89
                ],
90
            ],
91
            'omitted field name' => [
92
                ExcelError::VALUE(),
93
                $this->database1(),
94
                null,
95
                $this->database1(),
96
            ],
97
        ];
98
    }
99
}
100