Skip to content

Commit 6602dd6

Browse files
author
MarkBaker
committed
More unit tests
1 parent fe1e0d2 commit 6602dd6

File tree

2 files changed

+49
-3
lines changed
  • src/PhpSpreadsheet/Calculation/LookupRef
  • tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef

2 files changed

+49
-3
lines changed

src/PhpSpreadsheet/Calculation/LookupRef/Filter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function filter($lookupArray, $matchArray, $ifEmpty = null)
2929
return $ifEmpty ?? ExcelError::CALC();
3030
}
3131

32-
return array_values($result);
32+
return array_values(array_map('array_values', $result));
3333
}
3434

3535
private static function enumerateArrayKeys(array $sortArray): array

tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/FilterTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
56
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
67
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Filter;
8+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
79
use PHPUnit\Framework\TestCase;
810

911
class FilterTest extends TestCase
@@ -15,7 +17,7 @@ public function testFilterByRow(): void
1517
['East', 'Tom', 'Apple', 6830],
1618
['East', 'Fritz', 'Apple', 4394],
1719
['South', 'Sal', 'Apple', 1310],
18-
['South', 'Hector', 'Apple', 98144],
20+
['South', 'Hector', 'Apple', 8144],
1921
];
2022
$result = Filter::filter($this->sampleDataForRow(), $criteria);
2123
self::assertSame($expectedResult, $result);
@@ -52,6 +54,50 @@ public function testFilterEmpty(): void
5254
self::assertSame($expectedResult, $result);
5355
}
5456

57+
public function testFilterWithAndLogic(): void
58+
{
59+
$expectedResult = [
60+
['East', 'Tom', 'Apple', 6830],
61+
['East', 'Fritz', 'Banana', 6274],
62+
];
63+
64+
$spreadsheet = new Spreadsheet();
65+
$worksheet = $spreadsheet->getActiveSheet();
66+
$worksheet->fromArray($this->sampleDataForRow(), null, 'C3', true);
67+
68+
// East AND >6,000
69+
$formula = '=FILTER(C3:F18,(C3:C18="East")*(F3:F18>6000))';
70+
$worksheet->setCellValue('H1', $formula, true, 'H1:K2');
71+
$result = $worksheet->getCell('H1')->getCalculatedValue(true);
72+
73+
self::assertSame($expectedResult, $result);
74+
}
75+
76+
public function testFilterWithOrLogic(): void
77+
{
78+
$expectedResult = [
79+
['East', 'Tom', 'Apple', 6830],
80+
['East', 'Fritz', 'Apple', 4394],
81+
['West', 'Sravan', 'Grape', 7195],
82+
['East', 'Tom', 'Banana', 4213],
83+
['North', 'Amy', 'Grape', 6420],
84+
['East', 'Fritz', 'Banana', 6274],
85+
['North', 'Xi', 'Grape', 7580],
86+
['South', 'Hector', 'Apple', 8144],
87+
];
88+
89+
$spreadsheet = new Spreadsheet();
90+
$worksheet = $spreadsheet->getActiveSheet();
91+
$worksheet->fromArray($this->sampleDataForRow(), null, 'C3', true);
92+
93+
// East OR >6,000
94+
$formula = '=FILTER(C3:F18,(C3:C18="East")+(F3:F18>6000))';
95+
$worksheet->setCellValue('H1', $formula, true, 'H1:K8');
96+
$result = $worksheet->getCell('H1')->getCalculatedValue(true);
97+
98+
self::assertSame($expectedResult, $result);
99+
}
100+
55101
protected function sampleDataForRow(): array
56102
{
57103
return [
@@ -70,7 +116,7 @@ protected function sampleDataForRow(): array
70116
['East', 'Fritz', 'Banana', 6274],
71117
['West', 'Sravan', 'Pear', 4894],
72118
['North', 'Xi', 'Grape', 7580],
73-
['South', 'Hector', 'Apple', 98144],
119+
['South', 'Hector', 'Apple', 8144],
74120
];
75121
}
76122

0 commit comments

Comments
 (0)