mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-12 02:56:44 +00:00
e9cf27354d
Successor to PR #3523. There are 494 single-line changes (`public function provider` to `public static function provider`) in this PR. None of these were made manually; they were all created with the following script (adapted from https://stackoverflow.com/questions/25909820/how-to-recursively-iterate-through-files-in-php): ```php $dir = 'C:/git/unit10prep2/tests/PhpSpreadsheetTests'; $it = new RecursiveDirectoryIterator($dir); // Loop through files foreach(new RecursiveIteratorIterator($it) as $file) { if ($file->getExtension() === 'php') { $contents = file_get_contents($file); $new = preg_replace('/public function (\\w*)([Pp])rovider/', 'public static function $1$2rovider', $contents); if ($new !== $contents) { echo "changing $file\n"; file_put_contents($file, $new); } } } ``` After this PR, there will be one more, with a small number of test changes, and enabling PhpUnit 10 for Php 8.1+.
81 lines
2.9 KiB
PHP
81 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class LookupTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider providerLOOKUP
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testLOOKUP($expectedResult, ...$args): void
|
|
{
|
|
$result = LookupRef\Lookup::lookup(...$args);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerLOOKUP(): array
|
|
{
|
|
return require 'tests/data/Calculation/LookupRef/LOOKUP.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerLookupArray
|
|
*/
|
|
public function testLookupArray(array $expectedResult, string $values, string $lookup, string $return): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
|
|
$formula = "=LOOKUP({$values}, {$lookup}, {$return})";
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerLookupArray(): array
|
|
{
|
|
return [
|
|
'row vector' => [
|
|
[['Orange', 'Green', 'Red']],
|
|
'{4.19, 5.77, 4.14}',
|
|
'{4.14; 4.19; 5.17; 5.77; 6.39}',
|
|
'{"Red"; "Orange"; "Yellow"; "Green"; "Blue"}',
|
|
],
|
|
];
|
|
}
|
|
|
|
public function testBoolsAsInt(): void
|
|
{
|
|
// issue 3396 not handling math operation for bool in array
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->getCell('A59')->setValue('start');
|
|
$sheet->getCell('B59')->setValue('end');
|
|
$sheet->getCell('C59')->setValue('percent');
|
|
$sheet->getCell('A60')->setValue('=DATEVALUE("1950-01-01")');
|
|
$sheet->getCell('B60')->setValue('=DATEVALUE("2016-06-03")');
|
|
$sheet->getCell('C60')->setValue(0.05);
|
|
$sheet->getCell('A61')->setValue('=DATEVALUE("2016-06-04")');
|
|
$sheet->getCell('B61')->setValue('=DATEVALUE("2021-01-05")');
|
|
$sheet->getCell('C61')->setValue(0.08);
|
|
$sheet->getCell('A62')->setValue('=DATEVALUE("2021-01-16")');
|
|
$sheet->getCell('B62')->setValue('=DATEVALUE("2022-04-08")');
|
|
$sheet->getCell('C62')->setValue(0.03);
|
|
$sheet->getCell('A63')->setValue('=DATEVALUE("2022-04-09")');
|
|
$sheet->getCell('B63')->setValue('=DATEVALUE("2500-12-31")');
|
|
$sheet->getCell('C63')->setValue(0.04);
|
|
|
|
$sheet->getCell('D5')->setValue(5);
|
|
$sheet->getCell('E5')->setValue('=DATEVALUE("2023-01-01")');
|
|
$sheet->getCell('D7')->setValue('=IF(E5<>"",LOOKUP(2,1/($A$60:$A$63<=E5)/($B$60:$B$63>=E5),$C$60:$C$63)*D5,"")');
|
|
|
|
self::assertSame(0.2, $sheet->getCell('D7')->getCalculatedValue());
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
}
|