Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Calculation/ArrayFormulaTest.php
T
oleibman e9cf27354d PhpUnit 10 Compatibility Part 2 (#3526)
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+.
2023-04-20 13:48:00 -07:00

88 lines
2.9 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class ArrayFormulaTest extends TestCase
{
/**
* @dataProvider providerArrayFormulae
*
* @param mixed $expectedResult
*/
public function testArrayFormula(string $formula, $expectedResult): void
{
$result = Calculation::getInstance()->_calculateFormulaValue($formula);
self::assertEquals($expectedResult, $result);
}
public static function providerArrayFormulae(): array
{
return [
[
'=MAX(ABS({-3, 4, -2; 6, -3, -12}))',
12,
],
'unary operator applied to function' => [
'=MAX(-ABS({-3, 4, -2; 6, -3, -12}))',
-2,
],
[
'=SUM(SEQUENCE(3,3,0,1))',
36,
],
[
'=IFERROR({5/2, 5/0}, MAX(ABS({-2,4,-6})))',
[[2.5, 6]],
],
[
'=MAX(IFERROR({5/2, 5/0}, 2.1))',
2.5,
],
[
'=IF(FALSE,{1,2,3},{4,5,6})',
[[4, 5, 6]],
],
[
'=IFS(FALSE, {1,2,3}, TRUE, {4,5,6})',
[[4, 5, 6]],
],
'some invalid values' => [
'=ABS({1,-2,"X3"; "B4",5,6})',
[[1, 2, '#VALUE!'], ['#VALUE!', 5, 6]],
],
'some invalid values with unary minus' => [
'=-({1,-2,"X3"; "B4",5,6})',
[[-1, 2, '#VALUE!'], ['#VALUE!', -5, -6]],
],
];
}
public function testArrayFormulaUsingCells(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A4')->setValue(-3);
$sheet->getCell('B4')->setValue(4);
$sheet->getCell('C4')->setValue(-2);
$sheet->getCell('A5')->setValue(6);
$sheet->getCell('B5')->setValue(-3);
$sheet->getCell('C5')->setValue(-12);
$sheet->getCell('E4')->setValue('=MAX(-ABS(A4:C5))');
self::assertSame(-2, $sheet->getCell('E4')->getCalculatedValue());
$sheet->getCell('C4')->setValue('XYZ');
$sheet->getCell('F4')->setValue('=MAX(-ABS(A4:C5))');
self::assertSame('#VALUE!', $sheet->getCell('F4')->getCalculatedValue());
$sheet->getCell('G4')->setValue('=-C4:E4');
self::assertSame('#VALUE!', $sheet->getCell('G4')->getCalculatedValue());
$sheet->getCell('H4')->setValue('=-A4:B4');
self::assertSame(3, $sheet->getCell('H4')->getCalculatedValue());
$sheet->getCell('I4')->setValue('=25%');
self::assertEqualsWithDelta(0.25, $sheet->getCell('I4')->getCalculatedValue(), 1.0E-8);
$spreadsheet->disconnectWorksheets();
}
}