mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-18 22:16:46 +00:00
d647fe7ee7
With PhpUnit 10 came the ability to use Php attributes rather than doc-block annotations for things like "data provider". PhpUnit 11 deprecates the use of annotations, and PhpUnit 12 will not not permit their use. Since PhpUnit 11 requires Php8.2+, we cannot adopt it as long as we support Php8.1, which will continue to be the case for some time. However, there is no penalty for early adoption. Php-cs-fixer can use: ``` 'php_unit_attributes' => ['keep_annotations' => false], ``` This allows us to run `composer fix` to automate all the needed changes. No manual changes were needed for any of the test members. With this change, PhpUnit 9 can no longer be used with the test suite. File composer.json is updated to reflect that reality, and phpunit9.xml.dist, which has been supplied in case anyone needed to use PhpUnit 9, is no longer required, and is thus deleted. For now, PhpUnit 11 is not being added as a possibility. No source code is changed in this PR.
77 lines
3.2 KiB
PHP
77 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
|
|
class TruncTest extends AllSetupTeardown
|
|
{
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerTRUNC')]
|
|
public function testTRUNC(mixed $expectedResult, string $formula): void
|
|
{
|
|
$this->mightHaveException($expectedResult);
|
|
$sheet = $this->getSheet();
|
|
$sheet->setCellValue('A2', 1.3);
|
|
$sheet->setCellValue('A3', 2.7);
|
|
$sheet->setCellValue('A4', -3.8);
|
|
$sheet->setCellValue('A5', -5.2);
|
|
$sheet->getCell('A1')->setValue("=TRUNC($formula)");
|
|
$result = $sheet->getCell('A1')->getCalculatedValue();
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
|
|
}
|
|
|
|
public static function providerTRUNC(): array
|
|
{
|
|
return require 'tests/data/Calculation/MathTrig/TRUNC.php';
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerTruncArray')]
|
|
public function testTruncArray(array $expectedResult, string $argument1, string $argument2): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
|
|
$formula = "=TRUNC({$argument1}, {$argument2})";
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
|
|
}
|
|
|
|
public static function providerTruncArray(): array
|
|
{
|
|
return [
|
|
'matrix' => [[[3.14, 3.141], [3.14159, 3.14159265]], '3.1415926536', '{2, 3; 5, 8}'],
|
|
];
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerTooMuchPrecision')]
|
|
public function testTooMuchPrecision(mixed $expectedResult, float|int|string $value, int $digits = 1): void
|
|
{
|
|
// This test is pretty screwy. Possibly shouldn't even attempt it.
|
|
// At any rate, these results seem to indicate that PHP
|
|
// maximum precision is PHP_FLOAT_DIG - 1 digits, not PHP_FLOAT_DIG.
|
|
// If that changes, at least one of these tests will have to change.
|
|
$sheet = $this->getSheet();
|
|
$sheet->getCell('E1')->setValue($value);
|
|
$sheet->getCell('E2')->setValue("=TRUNC(E1,$digits)");
|
|
$result = $sheet->getCell('E2')->getCalculatedValueString();
|
|
self::assertSame($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerTooMuchPrecision(): array
|
|
{
|
|
$max64Plus1 = 9223372036854775808;
|
|
$stringMax = (string) $max64Plus1;
|
|
|
|
return [
|
|
'2 digits less than PHP_FLOAT_DIG' => ['1' . str_repeat('0', PHP_FLOAT_DIG - 4) . '1.2', 10.0 ** (PHP_FLOAT_DIG - 3) + 1.2, 1],
|
|
'1 digit less than PHP_FLOAT_DIG' => ['1' . str_repeat('0', PHP_FLOAT_DIG - 3) . '1', 10.0 ** (PHP_FLOAT_DIG - 2) + 1.2, 1],
|
|
'PHP_FLOAT_DIG' => ['1.0E+' . (PHP_FLOAT_DIG - 1), 10.0 ** (PHP_FLOAT_DIG - 1) + 1.2, 1],
|
|
'1 digit more than PHP_FLOAT_DIG' => ['1.0E+' . PHP_FLOAT_DIG, 10.0 ** PHP_FLOAT_DIG + 1.2, 1],
|
|
'32bit exceed int max' => ['3123456780', 3123456789, -1],
|
|
'64bit exceed int max neg decimals' => [$stringMax, $max64Plus1, -1],
|
|
'64bit exceed int max pos decimals' => [$stringMax, $max64Plus1, 1],
|
|
];
|
|
}
|
|
}
|