mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-26 00:32:52 +00:00
57985fde8e
Fix #4696. Fix #917 (which had gone stale but is now reopened). Fix #916. People were receiving unexpected exceptions calling `Date::isDateTime($cell)`. 916 and 917 had been mostly resolved long ago by adding a numeric check on the contents of $cell. However, it turns out that the use of values with very large absolute values can cause Php DateTime to throw an exception, which is the problem with 4696, when it calls `Date::toExcelDateTimeObject` for a cell with a large integer value. `Date::toExcelDateTimeObject` is coded to return a Php DateTime object. I don't see any reasonable non-breaking fix for that. People can always add try/catch if they are worried about that. However, `Date::isDateTime` can add an additional check and return `false` if this situation occurs. All 3 of the issues resolved by this PR mention isDateTime in their discussion; they are all fixed without additional coding changes by this. An additional change is needed for `Cell::getFormattedValue` to avoid this problem. This is fixed in `Style::NumberFormatter::DateFormatter::format`.
63 lines
2.3 KiB
PHP
63 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Shared;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class Issue4696Test extends TestCase
|
|
{
|
|
private ?Spreadsheet $spreadsheet = null;
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if ($this->spreadsheet !== null) {
|
|
$this->spreadsheet->disconnectWorksheets();
|
|
$this->spreadsheet = null;
|
|
}
|
|
}
|
|
|
|
#[DataProvider('providerIsDateTime')]
|
|
public function testTimeOnly(bool $expectedResult, string $expectedFormatted, int|float|string $value): void
|
|
{
|
|
$this->spreadsheet = new Spreadsheet();
|
|
$sheet = $this->spreadsheet->getActiveSheet();
|
|
if (is_string($value) && $value[0] !== '=') {
|
|
$sheet->getCell('A1')->setValueExplicit($value, DataType::TYPE_STRING);
|
|
} else {
|
|
$sheet->getCell('A1')->setValue($value);
|
|
}
|
|
$sheet->getStyle('A1')->getNumberFormat()
|
|
->setFormatCode('yyyy-mm-dd');
|
|
self::assertSame(
|
|
$expectedResult,
|
|
Date::isDateTime($sheet->getCell('A1'))
|
|
);
|
|
self::assertSame(
|
|
$expectedFormatted,
|
|
$sheet->getCell('A1')->getFormattedValue()
|
|
);
|
|
}
|
|
|
|
public static function providerIsDateTime(): array
|
|
{
|
|
return [
|
|
'valid integer' => [true, '1903-12-31', 1461],
|
|
'valid integer stored as string' => [true, '1904-01-01', '1462'],
|
|
'valid integer stored as concatenated string' => [true, '1904-01-01', '="14"&"62"'],
|
|
'valid float' => [true, '1903-12-31', 1461.5],
|
|
'valid float stored as string' => [true, '1903-12-31', '1461.5'],
|
|
'out-of-range integer' => [false, '7000989091802000122', 7000989091802000122],
|
|
'out-of-range float' => [false, '7.000989091802E+18', 7000989091802000122.1],
|
|
'out-of-range float stored as string' => [false, '7000989091802000122.1', '7000989091802000122.1'],
|
|
'non-numeric' => [false, 'xyz', 'xyz'],
|
|
'issue 917' => [false, '5e8630b8-603c-43fe-b038-6154a3f893ab', '5e8630b8-603c-43fe-b038-6154a3f893ab'],
|
|
];
|
|
}
|
|
}
|