Failed Conditions
Push — master ( df3a06...5c643a )
by
unknown
22:19 queued 14:24
created

testFilterInvalidLookupArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 16
rs 9.8333
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
6
7
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
8
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
9
use PhpOffice\PhpSpreadsheet\Spreadsheet;
10
use PHPUnit\Framework\TestCase;
11
12
class FilterOnSpreadsheetTest extends TestCase
13
{
14
    public function testFilterByRow(): void
15
    {
16
        $spreadsheet = new Spreadsheet();
17
        Calculation::getInstance($spreadsheet)
18
            ->setInstanceArrayReturnType(
19
                Calculation::RETURN_ARRAY_AS_ARRAY
20
            );
21
        $sheet = $spreadsheet->getActiveSheet();
22
        $criteria = [[true], [false], [false], [false], [true], [false], [false], [false], [false], [false], [false], [true], [false], [false], [false], [true]];
23
        $sheet->fromArray($criteria, null, 'A1', true);
24
        $sheet->fromArray($this->sampleDataForRow(), null, 'C1', true);
25
        $sheet->getCell('H1')->setValue('=FILTER(C1:F16, A1:A16)');
26
        $expectedResult = [
27
            ['East', 'Tom', 'Apple', 6830],
28
            ['East', 'Fritz', 'Apple', 4394],
29
            ['South', 'Sal', 'Apple', 1310],
30
            ['South', 'Hector', 'Apple', 8144],
31
        ];
32
        $result = $sheet->getCell('H1')->getCalculatedValue();
33
        self::assertSame($expectedResult, $result);
34
        $spreadsheet->disconnectWorksheets();
35
    }
36
37
    public function testFilterByColumn(): void
38
    {
39
        $spreadsheet = new Spreadsheet();
40
        Calculation::getInstance($spreadsheet)
41
            ->setInstanceArrayReturnType(
42
                Calculation::RETURN_ARRAY_AS_ARRAY
43
            );
44
        $sheet = $spreadsheet->getActiveSheet();
45
        $criteria = [[false, false, true, false, true, false, false, false, true, true]];
46
        $sheet->fromArray($criteria, null, 'A1', true);
47
        $sheet->fromArray($this->sampleDataForColumn(), null, 'A3', true);
48
        $sheet->getCell('A8')->setValue('=FILTER(A3:J5, A1:J1)');
49
        $expectedResult = [
50
            ['Betty', 'Charlotte', 'Oliver', 'Zoe'],
51
            ['B', 'B', 'B', 'B'],
52
            [1, 2, 4, 8],
53
        ];
54
        $result = $sheet->getCell('A8')->getCalculatedValue();
55
        self::assertSame($expectedResult, $result);
56
        $spreadsheet->disconnectWorksheets();
57
    }
58
59
    public function testFilterInvalidMatchArray(): void
60
    {
61
        $spreadsheet = new Spreadsheet();
62
        Calculation::getInstance($spreadsheet)
63
            ->setInstanceArrayReturnType(
64
                Calculation::RETURN_ARRAY_AS_ARRAY
65
            );
66
        $sheet = $spreadsheet->getActiveSheet();
67
        $sheet->fromArray($this->sampleDataForColumn(), null, 'A3', true);
68
        $sheet->getCell('A12')->setValue('=FILTER(A3:J5, "INVALID")');
69
        $expectedResult = ExcelError::VALUE();
70
        $result = $sheet->getCell('A12')->getCalculatedValue();
71
        self::assertSame($expectedResult, $result);
72
        $spreadsheet->disconnectWorksheets();
73
    }
74
75
    public function testFilterInvalidLookupArray(): void
76
    {
77
        $spreadsheet = new Spreadsheet();
78
        Calculation::getInstance($spreadsheet)
79
            ->setInstanceArrayReturnType(
80
                Calculation::RETURN_ARRAY_AS_ARRAY
81
            );
82
        $sheet = $spreadsheet->getActiveSheet();
83
        $criteria = [[false, false, true, false, true, false, false, false, true, true]];
84
        $sheet->fromArray($criteria, null, 'A1', true);
85
        $sheet->fromArray($this->sampleDataForColumn(), null, 'A3', true);
86
        $sheet->getCell('A14')->setValue('=FILTER("invalid", A1:J1)');
87
        $expectedResult = ExcelError::VALUE();
88
        $result = $sheet->getCell('A14')->getCalculatedValue();
89
        self::assertSame($expectedResult, $result);
90
        $spreadsheet->disconnectWorksheets();
91
    }
92
93
    public function testFilterEmpty(): void
94
    {
95
        $spreadsheet = new Spreadsheet();
96
        Calculation::getInstance($spreadsheet)
97
            ->setInstanceArrayReturnType(
98
                Calculation::RETURN_ARRAY_AS_ARRAY
99
            );
100
        $sheet = $spreadsheet->getActiveSheet();
101
        $criteria = [[false, false, true, false, true, false, false, false, true, true]];
102
        $sheet->fromArray($criteria, null, 'A1', true);
103
        $sheet->fromArray($this->sampleDataForColumn(), null, 'A3', true);
104
        $sheet->getCell('A16')->setValue('=FILTER(A3:B5, A1:B1)');
105
        $expectedResult = ExcelError::CALC();
106
        $result = $sheet->getCell('A16')->getCalculatedValue();
107
        self::assertSame($expectedResult, $result);
108
        $spreadsheet->disconnectWorksheets();
109
    }
110
111
    /** @return array<array{string, string, string, int}> */
112
    protected function sampleDataForRow(): array
113
    {
114
        return [
115
            ['East', 'Tom', 'Apple', 6830],
116
            ['West', 'Fred', 'Grape', 5619],
117
            ['North', 'Amy', 'Pear', 4565],
118
            ['South', 'Sal', 'Banana', 5323],
119
            ['East', 'Fritz', 'Apple', 4394],
120
            ['West', 'Sravan', 'Grape', 7195],
121
            ['North', 'Xi', 'Pear', 5231],
122
            ['South', 'Hector', 'Banana', 2427],
123
            ['East', 'Tom', 'Banana', 4213],
124
            ['West', 'Fred', 'Pear', 3239],
125
            ['North', 'Amy', 'Grape', 6420],
126
            ['South', 'Sal', 'Apple', 1310],
127
            ['East', 'Fritz', 'Banana', 6274],
128
            ['West', 'Sravan', 'Pear', 4894],
129
            ['North', 'Xi', 'Grape', 7580],
130
            ['South', 'Hector', 'Apple', 8144],
131
        ];
132
    }
133
134
    /** @return array<array<int|string>> */
135
    protected function sampleDataForColumn(): array
136
    {
137
        return [
138
            ['Aiden', 'Andrew', 'Betty', 'Caden', 'Charlotte', 'Emma', 'Isabella', 'Mason', 'Oliver', 'Zoe'],
139
            ['A', 'C', 'B', 'A', 'B', 'C', 'A', 'A', 'B', 'B'],
140
            [0, 4, 1, 2, 2, 0, 2, 4, 4, 8],
141
        ];
142
    }
143
}
144