Minor Improvements to Calculation Coverage

This commit is contained in:
oleibman
2025-09-01 00:28:46 -07:00
parent b79b77cf0f
commit f375d9a69b
4 changed files with 198 additions and 2 deletions
@@ -888,7 +888,7 @@ class Calculation extends CalculationLocale
}
}
if ($matrix1Rows < $matrix2Rows) {
$x = ($matrix1Rows === 1) ? $matrix1[0] : array_fill(0, $matrix1Columns, null);
$x = ($matrix1Rows === 1) ? $matrix1[0] : array_fill(0, $matrix2Columns, null);
for ($i = $matrix1Rows; $i < $matrix2Rows; ++$i) {
$matrix1[$i] = $x;
}
@@ -0,0 +1,137 @@
<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
use PhpOffice\PhpSpreadsheet\Calculation\ExceptionHandler;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\Attributes;
use PHPUnit\Framework\TestCase;
class CalculationCoverageTest extends TestCase
{
public function testClone(): void
{
$this->expectException(CalcException::class);
$this->expectExceptionMessage('Cloning the calculation engine is not allowed!');
$calc = Calculation::getInstance();
$clone = clone $calc;
$clone->flushInstance();
}
public function testBadInstanceArray(): void
{
$spreadsheet = new Spreadsheet();
$calc = Calculation::getInstance($spreadsheet);
$type = $calc->getInstanceArrayReturnType();
self::assertFalse($calc->setInstanceArrayReturnType('bad'));
self::assertSame($type, $calc->getInstanceArrayReturnType());
$spreadsheet->disconnectWorksheets();
}
public function testCalculate(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$calc = Calculation::getInstance($spreadsheet);
$sheet->getCell('A1')->setValue('=2+3');
$result = $calc->calculate($sheet->getCell('A1'));
self::assertSame(5, $result);
self::assertSame('', Calculation::boolToString(null));
$spreadsheet->disconnectWorksheets();
}
public function testCalculateBad(): void
{
$this->expectException(CalcException::class);
$this->expectExceptionMessage('Formula Error');
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$calc = Calculation::getInstance($spreadsheet);
$sheet->getCell('A1')->setValue('=SUM(');
$result = $calc->calculate($sheet->getCell('A1'));
self::assertSame(5, $result);
$spreadsheet->disconnectWorksheets();
}
public function testParse(): void
{
$calc = Calculation::getInstance();
self::assertSame([], $calc->parseFormula('2+3'), 'no leading =');
self::assertSame([], $calc->parseFormula('='), 'leading = but no other text');
}
public function testExtractNamedRange(): void
{
$spreadsheet = new Spreadsheet();
$calc = Calculation::getInstance($spreadsheet);
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('mysheet');
$sheet->setCellValue('A1', 1);
$sheet->setCellValue('B1', 2);
$sheet->setCellValue('A2', 3);
$sheet->setCellValue('B2', 4);
$spreadsheet->addNamedRange(
new NamedRange('Whatever', $sheet, '$A$1:$B$2')
);
$range = 'Whatever';
$result = $calc->extractNamedRange($range, $sheet);
self::assertSame('$A$1:$B$2', $range);
self::assertSame([1 => ['A' => 1, 'B' => 2], 2 => ['A' => 3, 'B' => 4]], $result);
$range = 'mysheet!Whatever';
$result = $calc->extractNamedRange($range, $sheet);
self::assertSame('$A$1:$B$2', $range);
self::assertSame([1 => ['A' => 1, 'B' => 2], 2 => ['A' => 3, 'B' => 4]], $result);
$range = 'mysheet!Whateverx';
$result = $calc->extractNamedRange($range, $sheet);
self::assertSame('Whateverx', $range);
self::assertSame('#REF!', $result);
$range = 'Why';
$result = $calc->extractNamedRange($range, $sheet);
self::assertSame('Why', $range);
self::assertSame('#REF!', $result);
$spreadsheet->addNamedRange(
new NamedRange('OneCell', $sheet, '$A$1')
);
$range = 'OneCell';
$result = $calc->extractNamedRange($range, $sheet);
self::assertSame('$A$1', $range);
self::assertSame([1 => ['A' => 1]], $result);
$spreadsheet->addNamedRange(
new NamedRange('NoSuchCell', $sheet, '$Z$1')
);
$range = 'NoSuchCell';
$result = $calc->extractNamedRange($range, $sheet);
self::assertSame('$Z$1', $range);
self::assertSame([1 => ['Z' => null]], $result);
$spreadsheet->addNamedRange(
new NamedRange('SomeCells', $sheet, '$B$1:$C$2')
);
$range = 'SomeCells';
$result = $calc->extractNamedRange($range, $sheet);
self::assertSame('$B$1:$C$2', $range);
self::assertSame([1 => ['B' => 2, 'C' => null], 2 => ['B' => 4, 'C' => null]], $result);
$spreadsheet->disconnectWorksheets();
}
// separate process because it sets its own handler
#[Attributes\RunInSeparateProcess]
public function testExceptionHandler(): void
{
$this->expectException(CalcException::class);
$this->expectExceptionMessage('hello');
$handler = new ExceptionHandler();
trigger_error('hello');
self::assertNotNull($handler); // @phpstan-ignore-line
}
}
@@ -30,6 +30,15 @@ class CustomFunctionTest extends TestCase
self::assertTrue(Calculation::removeFunction($key));
self::assertSame('#NAME?', $calculation->calculateFormula('=FOURTHPOWER(3)'));
self::assertFalse(Calculation::removeFunction('WHATEVER'));
$key = 'NATIVECOS';
$value = [
'category' => 'custom',
'functionCall' => 'cos',
'argumentCount' => '1',
];
self::assertTrue(Calculation::addFunction($key, $value));
self::assertSame(1.0, $calculation->calculateFormula('=NATIVECOS(0)'));
self::assertTrue(Calculation::removeFunction($key));
}
public static function testReplaceDummyFunction(): void
@@ -11,7 +11,7 @@ use ReflectionMethod;
class Issue4451Test extends TestCase
{
public static function testReflect(): void
public static function testReflectExtend1(): void
{
// Sample matrices to test with
$matrix1 = [[1], [3]];
@@ -27,6 +27,56 @@ class Issue4451Test extends TestCase
self::assertSame([[1], [3], [null]], $matrix1); //* @phpstan-ignore-line
}
public static function testReflectExtend2(): void
{
// Sample matrices to test with
$matrix1 = [[1], [3]];
$matrix2 = [[5, 6], [8, 9], [11, 12]];
// Use reflection to make the protected method accessible
$calculation = new Calculation();
$reflectionMethod = new ReflectionMethod(Calculation::class, 'resizeMatricesExtend');
// Call the method using reflection
$reflectionMethod->invokeArgs($calculation, [&$matrix1, &$matrix2, count($matrix1), 1, count($matrix2), 2]);
self::assertSame([[1, 1], [3, 3], [null, null]], $matrix1); //* @phpstan-ignore-line
}
public static function testReflectShrink1(): void
{
// Sample matrices to test with
$matrix1 = [[10, 20], [30, 40]];
$matrix2 = [[50, 60, 70], [80, 90, 100], [110, 120, 130]];
// Use reflection to make the protected method accessible
$calculation = new Calculation();
$reflectionMethod = new ReflectionMethod(Calculation::class, 'resizeMatricesShrink');
// Call the method using reflection
$reflectionMethod->invokeArgs($calculation, [&$matrix1, &$matrix2, count($matrix1), count($matrix1), count($matrix2), count($matrix2)]);
self::assertSame([[10, 20], [30, 40]], $matrix1); //* @phpstan-ignore-line
self::assertSame([[50, 60], [80, 90]], $matrix2); //* @phpstan-ignore-line
}
public static function testReflectShrink2(): void
{
// Sample matrices to test with
$matrix2 = [[10, 20], [30, 40]];
$matrix1 = [[50, 60, 70], [80, 90, 100], [110, 120, 130]];
// Use reflection to make the protected method accessible
$calculation = new Calculation();
$reflectionMethod = new ReflectionMethod(Calculation::class, 'resizeMatricesShrink');
// Call the method using reflection
$reflectionMethod->invokeArgs($calculation, [&$matrix1, &$matrix2, count($matrix1), count($matrix1), count($matrix2), count($matrix2)]);
self::assertSame([[10, 20], [30, 40]], $matrix2); //* @phpstan-ignore-line
self::assertSame([[50, 60], [80, 90]], $matrix1); //* @phpstan-ignore-line
}
/**
* These 2 tests are contrived. They prove that method
* works as desired, but Excel will actually return