Add tests for the LOOKUP function

We were performing operations to patch for missing $result_vector
even when one was defined, this was causing bugs on edge cases.

Fixes #796
Closes #816
This commit is contained in:
Fräntz Miccoli 2018-12-13 15:32:23 +01:00 committed by Adrien Crivelli
parent f0e69408ca
commit db2621c4fe
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
4 changed files with 136 additions and 4 deletions

View File

@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Improve XLSX parsing speed if no readFilter is applied - [#772](https://github.com/PHPOffice/PhpSpreadsheet/issues/772)
- Fix column names if read filter calls in XLSX reader skip columns - [#777](https://github.com/PHPOffice/PhpSpreadsheet/pull/777)
- Fix LOOKUP function which was breaking on edge cases - [#796](https://github.com/PHPOffice/PhpSpreadsheet/issues/796)
## [1.5.2] - 2018-11-25

View File

@ -811,11 +811,13 @@ class LookupRef
if (!is_array($lookup_vector)) {
return Functions::NA();
}
$hasResultVector = isset($result_vector);
$lookupRows = count($lookup_vector);
$l = array_keys($lookup_vector);
$l = array_shift($l);
$lookupColumns = count($lookup_vector[$l]);
if ((($lookupRows == 1) && ($lookupColumns > 1)) || (($lookupRows == 2) && ($lookupColumns != 2))) {
// we correctly orient our results
if (($lookupRows === 1 && $lookupColumns > 1) || (!$hasResultVector && $lookupRows === 2 && $lookupColumns !== 2)) {
$lookup_vector = self::TRANSPOSE($lookup_vector);
$lookupRows = count($lookup_vector);
$l = array_keys($lookup_vector);
@ -829,18 +831,20 @@ class LookupRef
$l = array_keys($result_vector);
$l = array_shift($l);
$resultColumns = count($result_vector[$l]);
if ((($resultRows == 1) && ($resultColumns > 1)) || (($resultRows == 2) && ($resultColumns != 2))) {
// we correctly orient our results
if ($resultRows === 1 && $resultColumns > 1) {
$result_vector = self::TRANSPOSE($result_vector);
$resultRows = count($result_vector);
$r = array_keys($result_vector);
$resultColumns = count($result_vector[array_shift($r)]);
}
if ($lookupRows == 2) {
if ($lookupRows === 2 && !$hasResultVector) {
$result_vector = array_pop($lookup_vector);
$lookup_vector = array_shift($lookup_vector);
}
if ($lookupColumns != 2) {
if ($lookupColumns !== 2) {
foreach ($lookup_vector as &$value) {
if (is_array($value)) {
$k = array_keys($value);

View File

@ -51,6 +51,22 @@ class LookupRefTest extends TestCase
return require 'data/Calculation/LookupRef/VLOOKUP.php';
}
/**
* @dataProvider providerLOOKUP
*
* @param mixed $expectedResult
*/
public function testLOOKUP($expectedResult, ...$args)
{
$result = LookupRef::LOOKUP(...$args);
self::assertEquals($expectedResult, $result);
}
public function providerLOOKUP()
{
return require 'data/Calculation/LookupRef/LOOKUP.php';
}
/**
* @dataProvider providerMATCH
*

View File

@ -0,0 +1,111 @@
<?php
return [
[ // Office reference example #1
'orange',
4.19,
[
[4.14],
[4.19],
[5.17],
[5.77],
[6,39],
],
[
['red'],
['orange'],
['yellow'],
['green'],
['blue'],
],
],
[ // Office reference example #2
'yellow',
5.75,
[
[4.14],
[4.19],
[5.17],
[5.77],
[6,39],
],
[
['red'],
['orange'],
['yellow'],
['green'],
['blue'],
],
],
[ // Office reference example #3
'blue',
7.66,
[
[4.14],
[4.19],
[5.17],
[5.77],
[6,39],
],
[
['red'],
['orange'],
['yellow'],
['green'],
['blue'],
],
],
[ // Office reference example #4
'#N/A',
0,
[
[4.14],
[4.19],
[5.17],
[5.77],
[6,39],
],
[
['red'],
['orange'],
['yellow'],
['green'],
['blue'],
],
],
[ // Array form test
'orange',
4.2,
[
[4.14, 'red'],
[4.19, 'orange'],
[5.17, 'yellow'],
[5.77, 'green'],
[6,39, 'blue'],
]
],
[
5,
'x',
[
[0, 0, 0, 'x', 'x'],
[1, 2, 3, 4, 5]
]
],
[
'author_100',
100,
[
[100],
[101]
],
[
['author_100'],
['author_101']
]
]
];