mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-27 09:08:36 +00:00
d27b6a672a
Scrutinizer had previously suggested annotations for 3 LookupRef tests, but it no longer accepts its own annotation for those cases. This PR cleans them up. ColumnsTest and RowsTest are extremely straightforward. IndexTest is a bit more complicated, but only because, unlike the other two, it had no test which executed in the context of a spreadsheet. And, when I added those, I discovered a couple of bugs. INDEX always requires at least 2 parameters (row# is always required), but its entry in the function table specified 1-4 parameters, now changed to 2-4. And, omitting col# is not handled the same way as specifying 0 for col#, though the code had treated them identically. (The same would have been true for row# but, because it is now required, ...)
72 lines
2.0 KiB
PHP
72 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Matrix;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class IndexTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider providerINDEX
|
|
*
|
|
* @param mixed $expectedResult
|
|
* @param mixed $matrix
|
|
* @param mixed $rowNum
|
|
* @param mixed $colNum
|
|
*/
|
|
public function testINDEX($expectedResult, $matrix, $rowNum = null, $colNum = null): void
|
|
{
|
|
if ($rowNum === null) {
|
|
$result = Matrix::index($matrix);
|
|
} elseif ($colNum === null) {
|
|
$result = Matrix::index($matrix, $rowNum);
|
|
} else {
|
|
$result = Matrix::index($matrix, $rowNum, $colNum);
|
|
}
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public function providerINDEX(): array
|
|
{
|
|
return require 'tests/data/Calculation/LookupRef/INDEX.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerIndexArray
|
|
*/
|
|
public function testIndexArray(array $expectedResult, string $matrix, string $rows, string $columns): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
|
|
$formula = "=INDEX({$matrix}, {$rows}, {$columns})";
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public function providerIndexArray(): array
|
|
{
|
|
return [
|
|
'row/column vectors' => [
|
|
[[2, 3], [5, 6]],
|
|
'{1, 2, 3; 4, 5, 6; 7, 8, 9}',
|
|
'{1; 2}',
|
|
'{2, 3}',
|
|
],
|
|
'return row' => [
|
|
[1 => [4, 5, 6]],
|
|
'{1, 2, 3; 4, 5, 6; 7, 8, 9}',
|
|
'2',
|
|
'0',
|
|
],
|
|
'return column' => [
|
|
[[2], [5], [8]],
|
|
'{1, 2, 3; 4, 5, 6; 7, 8, 9}',
|
|
'0',
|
|
'2',
|
|
],
|
|
];
|
|
}
|
|
}
|