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

SetupTeardownDatabases::database2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 20
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6
use PhpOffice\PhpSpreadsheet\Cell\DataType;
7
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
9
use PHPUnit\Framework\TestCase;
10
11
class SetupTeardownDatabases extends TestCase
12
{
13
    protected const RESULT_CELL = 'Z1';
14
15
    /**
16
     * @var ?Spreadsheet
17
     */
18
    private $spreadsheet;
19
20
    /**
21
     * @var ?Worksheet
22
     */
23
    private $sheet;
24
25
    protected function setUp(): void
26
    {
27
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
28
    }
29
30
    protected function tearDown(): void
31
    {
32
        $this->sheet = null;
33
        if ($this->spreadsheet !== null) {
34
            $this->spreadsheet->disconnectWorksheets();
35
            $this->spreadsheet = null;
36
        }
37
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
38
    }
39
40
    protected function database1(): array
41
    {
42
        return [
43
            ['Tree', 'Height', 'Age', 'Yield', 'Profit'],
44
            ['Apple', 18, 20, 14, 105],
45
            ['Pear', 12, 12, 10, 96],
46
            ['Cherry', 13, 14, 9, 105],
47
            ['Apple', 14, 15, 10, 75],
48
            ['Pear', 9, 8, 8, 76.8],
49
            ['Apple', 8, 9, 6, 45],
50
        ];
51
    }
52
53
    protected function database2(): array
54
    {
55
        return [
56
            ['Quarter', 'Area', 'Sales Rep.', 'Sales'],
57
            [1, 'North', 'Jeff', 223000],
58
            [1, 'North', 'Chris', 125000],
59
            [1, 'South', 'Carol', 456000],
60
            [1, 'South', 'Tina', 289000],
61
            [2, 'North', 'Jeff', 322000],
62
            [2, 'North', 'Chris', 340000],
63
            [2, 'South', 'Carol', 198000],
64
            [2, 'South', 'Tina', 222000],
65
            [3, 'North', 'Jeff', 310000],
66
            [3, 'North', 'Chris', 250000],
67
            [3, 'South', 'Carol', 460000],
68
            [3, 'South', 'Tina', 395000],
69
            [4, 'North', 'Jeff', 261000],
70
            [4, 'North', 'Chris', 389000],
71
            [4, 'South', 'Carol', 305000],
72
            [4, 'South', 'Tina', 188000],
73
        ];
74
    }
75
76
    protected function database3(): array
77
    {
78
        return [
79
            ['Name', 'Gender', 'Age', 'Subject', 'Score'],
80
            ['Amy', 'Female', 8, 'Math', 0.63],
81
            ['Amy', 'Female', 8, 'English', 0.78],
82
            ['Amy', 'Female', 8, 'Science', 0.39],
83
            ['Bill', 'Male', 8, 'Math', 0.55],
84
            ['Bill', 'Male', 8, 'English', 0.71],
85
            ['Bill', 'Male', 8, 'Science', 'awaiting'],
86
            ['Sue', 'Female', 9, 'Math', null],
87
            ['Sue', 'Female', 9, 'English', 0.52],
88
            ['Sue', 'Female', 9, 'Science', 0.48],
89
            ['Tom', 'Male', 9, 'Math', 0.78],
90
            ['Tom', 'Male', 9, 'English', 0.69],
91
            ['Tom', 'Male', 9, 'Science', 0.65],
92
        ];
93
    }
94
95
    protected function database3FilledIn(): array
96
    {
97
        // same as database3 except two omitted scores are filled in
98
        return [
99
            ['Name', 'Gender', 'Age', 'Subject', 'Score'],
100
            ['Amy', 'Female', 10, 'Math', 0.63],
101
            ['Amy', 'Female', 10, 'English', 0.78],
102
            ['Amy', 'Female', 10, 'Science', 0.39],
103
            ['Bill', 'Male', 8, 'Math', 0.55],
104
            ['Bill', 'Male', 8, 'English', 0.71],
105
            ['Bill', 'Male', 8, 'Science', 0.51],
106
            ['Sam', 'Male', 9, 'Math', 0.39],
107
            ['Sam', 'Male', 9, 'English', 0.52],
108
            ['Sam', 'Male', 9, 'Science', 0.48],
109
            ['Tom', 'Male', 9, 'Math', 0.78],
110
            ['Tom', 'Male', 9, 'English', 0.69],
111
            ['Tom', 'Male', 9, 'Science', 0.65],
112
        ];
113
    }
114
115
    protected function getSpreadsheet(): Spreadsheet
116
    {
117
        if ($this->spreadsheet !== null) {
118
            return $this->spreadsheet;
119
        }
120
        $this->spreadsheet = new Spreadsheet();
121
122
        return $this->spreadsheet;
123
    }
124
125
    protected function getSheet(): Worksheet
126
    {
127
        if ($this->sheet !== null) {
128
            return $this->sheet;
129
        }
130
        $this->sheet = $this->getSpreadsheet()->getActiveSheet();
131
132
        return $this->sheet;
133
    }
134
135
    /**
136
     * @param int|string $field
137
     */
138
    public function prepareWorksheetWithFormula(string $functionName, array $database, $field, array $criteria): void
139
    {
140
        $sheet = $this->getSheet();
141
        $maxCol = '';
142
        $startCol = 'A';
143
        $maxRow = 0;
144
        $startRow = 1;
145
        $row = $startRow;
146
        foreach ($database as $dataRow) {
147
            $col = $startCol;
148
            foreach ($dataRow as $dataCell) {
149
                $sheet->getCell("$col$row")->setValue($dataCell);
150
                $maxCol = max($col, $maxCol);
151
                ++$col;
152
            }
153
            $maxRow = $row;
154
            ++$row;
155
        }
156
        $databaseCells = "$startCol$startRow:$maxCol$maxRow";
157
        $maxCol = '';
158
        $startCol = 'P';
159
        $maxRow = 0;
160
        $startRow = 1;
161
        $row = $startRow;
162
        foreach ($criteria as $dataRow) {
163
            $col = $startCol;
164
            foreach ($dataRow as $dataCell) {
165
                if ($dataCell !== null) {
166
                    $sheet->getCell("$col$row")->setValueExplicit($dataCell, DataType::TYPE_STRING);
167
                }
168
                $maxCol = max($col, $maxCol);
169
                ++$col;
170
            }
171
            $maxRow = $row;
172
            ++$row;
173
        }
174
        $criteriaCells = "$startCol$startRow:$maxCol$maxRow";
175
        $sheet->getCell('N1')->setValue($field);
176
        $sheet->getCell(self::RESULT_CELL)->setValue("=$functionName($databaseCells, N1, $criteriaCells)");
177
    }
178
}
179