Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/Gnumeric/ArrayFormulaTest.php
T
oleibman ef2b5b9e00 Mostly Docs and Tests
Also support for Xml format.
2024-06-21 21:55:05 -07:00

106 lines
3.2 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Gnumeric;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric;
use PHPUnit\Framework\TestCase;
class ArrayFormulaTest extends TestCase
{
private string $arrayReturnType;
protected function setUp(): void
{
$this->arrayReturnType = Calculation::getArrayReturnType();
}
protected function tearDown(): void
{
Calculation::setArrayReturnType($this->arrayReturnType);
}
/**
* @dataProvider arrayFormulaReaderProvider
*/
public function testArrayFormulaReader(
string $cellAddress,
string $expectedRange,
string $expectedFormula,
array|float $expectedValue
): void {
$filename = 'tests/data/Reader/Gnumeric/ArrayFormulaTest.gnumeric';
$reader = new Gnumeric();
$spreadsheet = $reader->load($filename);
$worksheet = $spreadsheet->getActiveSheet();
$cell = $worksheet->getCell($cellAddress);
self::assertSame(DataType::TYPE_FORMULA, $cell->getDataType());
if (is_array($expectedValue)) {
self::assertSame(['t' => 'array', 'ref' => $expectedRange], $cell->getFormulaAttributes());
} else {
self::assertEmpty($cell->getFormulaAttributes());
}
self::assertSame($expectedFormula, strtoupper($cell->getValue()));
Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_ARRAY);
$worksheet->calculateArrays();
$cell = $worksheet->getCell($cellAddress);
self::assertSame($expectedValue, $cell->getCalculatedValue());
if (is_array($expectedValue)) {
self::assertSame($expectedValue, $worksheet->rangeToArray($expectedRange, formatData: false, reduceArrays: true));
} else {
self::assertSame([[$expectedValue]], $worksheet->rangeToArray($expectedRange, formatData: false, reduceArrays: true));
}
$spreadsheet->disconnectWorksheets();
}
public static function arrayFormulaReaderProvider(): array
{
return [
[
'D1',
'D1:E2',
'=A1:B1*A1:A2',
[[4, 6], [8, 12]],
],
[
'G1',
'G1:J1',
'=SIN({-1,0,1,2})',
[[-0.8414709848078965, 0.0, 0.8414709848078965, 0.9092974268256817]],
],
[
'G3',
'G3:G3',
'=MAX(SIN({-1,0,1,2}))',
0.9092974268256817,
],
[
'D4',
'D4:E5',
'=A4:B4*A4:A5',
[[9, 12], [15, 20]],
],
[
'D7',
'D7:E8',
'=A7:B7*A7:A8',
[[16, 20], [24, 30]],
],
[
'D10',
'D10:E11',
'=A10:B10*A10:A11',
[[25, 30], [35, 42]],
],
[
'D13',
'D13:E14',
'=A13:B13*A13:A14',
[[36, 42], [48, 56]],
],
];
}
}