First step extracting INDIRECT() and OFFSET() to their own classes (#1921)

* First step extracting INDIRECT() and OFFSET() to their own classes
* Start building unit tests for OFFSET() and INDEX()
* Named ranges should be handled by the Calculation Engine, not by the implementation of the Excel INDIRECT() function
* When calling the calculation engine to get the range of cells to return, INDIRECT() and OFFSET() should use the instance of the calculation engine for the current workbook to benefit from cached results in that range

There's a couple of minor bugfixes in here; but it's basically just refactoring of the INDIRECT() and OFFSET() Excel functions into their own classes - still needs a lot of work on unit testing; and there's a lot more that could be improved in the code itself (including handling of the a1 flag for R1C1 format in INDIRECT()
This commit is contained in:
Mark Baker
2021-03-14 19:58:10 +01:00
committed by GitHub
parent af9253d9e0
commit ed62526aca
10 changed files with 426 additions and 121 deletions
@@ -0,0 +1,55 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PHPUnit\Framework\TestCase;
class IndirectTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerINDIRECT
*
* @param mixed $expectedResult
* @param null|mixed $cellReference
*/
public function testINDIRECT($expectedResult, $cellReference = null): void
{
// $calculation = $this->getMockBuilder(Calculation::class)
// ->setMethods(['getInstance', 'extractCellRange'])
// ->disableOriginalConstructor()
// ->getMock();
// $calculation->method('getInstance')
// ->willReturn($calculation);
// $calculation->method('extractCellRange')
// ->willReturn([]);
//
// $worksheet = $this->getMockBuilder(Cell::class)
// ->setMethods(['getParent'])
// ->disableOriginalConstructor()
// ->getMock();
//
// $cell = $this->getMockBuilder(Cell::class)
// ->setMethods(['getWorksheet'])
// ->disableOriginalConstructor()
// ->getMock();
// $cell->method('getWorksheet')
// ->willReturn($worksheet);
$result = LookupRef::INDIRECT($cellReference);
self::assertSame($expectedResult, $result);
}
public function providerINDIRECT()
{
return require 'tests/data/Calculation/LookupRef/INDIRECT.php';
}
}
@@ -0,0 +1,32 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
use PHPUnit\Framework\TestCase;
class OffsetTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerOFFSET
*
* @param mixed $expectedResult
* @param null|mixed $cellReference
*/
public function testOFFSET($expectedResult, $cellReference = null): void
{
$result = LookupRef::OFFSET($cellReference);
self::assertSame($expectedResult, $result);
}
public function providerOFFSET()
{
return require 'tests/data/Calculation/LookupRef/OFFSET.php';
}
}
@@ -0,0 +1,16 @@
<?php
return [
[
'#REF!',
null,
],
[
'#REF!',
'InvalidCellAddress',
],
[
'#REF!',
'C2:InvalidCellAddress',
],
];
@@ -0,0 +1,8 @@
<?php
return [
[
0,
null,
],
];