mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-22 01:13:40 +00:00
7d71a7ca54
* New Error Reported with Phpstan 1.3 Dependabot opened a number of PRs. Most are successful, but this change is necessary to allow PR #2477 to complete successfully, and that is apparently a necessity for PR #2479. Phpstan 1.3 objected to: ```php trigger_error($errorMessage, E_USER_ERROR); return false; ``` It claims the return statement is unreachable. This isn't precisely true - an error handler might allow the return to be reached. At any rate, I have slightly restructured the code so that Phpstan will not object either with 1.2 or 1.3, which should allow the blocked PRs to succeed. There had been no previous tests for what happens when there is a formula error when suppressFormulaErrors is true. * Scrutinizer Didn't like effect of changes which Phpstan liked. Hopefully this will work better. * Improvement Eliminate added function.
55 lines
1.6 KiB
PHP
55 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Throwable;
|
|
|
|
class CalculationErrorTest extends TestCase
|
|
{
|
|
public function testCalculationException(): void
|
|
{
|
|
$this->expectException(CalcException::class);
|
|
$this->expectExceptionMessage('Formula Error:');
|
|
$calculation = Calculation::getInstance();
|
|
$result = $calculation->_calculateFormulaValue('=SUM(');
|
|
self::assertFalse($result);
|
|
}
|
|
|
|
public function testCalculationError(): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
$calculation->suppressFormulaErrors = true;
|
|
$error = false;
|
|
|
|
try {
|
|
$calculation->_calculateFormulaValue('=SUM(');
|
|
} catch (Throwable $e) {
|
|
self::assertSame("Formula Error: Expecting ')'", $e->getMessage());
|
|
self::assertSame('PHPUnit\\Framework\\Error\\Error', get_class($e));
|
|
$error = true;
|
|
}
|
|
self::assertTrue($error);
|
|
}
|
|
|
|
/**
|
|
* @param mixed $args
|
|
*/
|
|
public static function errhandler2(...$args): bool
|
|
{
|
|
return $args[0] === E_USER_ERROR;
|
|
}
|
|
|
|
public function testCalculationErrorTrulySuppressed(): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
$calculation->suppressFormulaErrors = true;
|
|
set_error_handler([self::class, 'errhandler2']);
|
|
$result = $calculation->_calculateFormulaValue('=SUM(');
|
|
restore_error_handler();
|
|
self::assertFalse($result);
|
|
}
|
|
}
|