mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-22 16:19:19 +00:00
3eedf9e2f0
Change "mixed" declarations to more accurate types in test members; in particular, change those that would be flagged if we were to run Phpstan at level 9 (we currently run level 8). I may or may not follow up with source code (over 700 level-9 problems remain for src), but, as with strict typing, there is no reason to avoid the effort for test members. It was necessary to update some doc blocks in src to accommodate this change. However, no executable code is touched.
253 lines
8.3 KiB
PHP
253 lines
8.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Shared;
|
|
|
|
use DateTimeInterface;
|
|
use DateTimeZone;
|
|
use PhpOffice\PhpSpreadsheet\Exception;
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class DateTest extends TestCase
|
|
{
|
|
private int $excelCalendar;
|
|
|
|
private ?DateTimeZone $dttimezone;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->dttimezone = Date::getDefaultTimeZoneOrNull();
|
|
$this->excelCalendar = Date::getExcelCalendar();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Date::setDefaultTimeZone($this->dttimezone);
|
|
Date::setExcelCalendar($this->excelCalendar);
|
|
}
|
|
|
|
public function testSetExcelCalendar(): void
|
|
{
|
|
$calendarValues = [
|
|
Date::CALENDAR_MAC_1904,
|
|
Date::CALENDAR_WINDOWS_1900,
|
|
];
|
|
|
|
foreach ($calendarValues as $calendarValue) {
|
|
$result = Date::setExcelCalendar($calendarValue);
|
|
self::assertTrue($result);
|
|
}
|
|
}
|
|
|
|
public function testSetExcelCalendarWithInvalidValue(): void
|
|
{
|
|
$unsupportedCalendar = 2012;
|
|
$result = Date::setExcelCalendar($unsupportedCalendar);
|
|
self::assertFalse($result);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeExcelToTimestamp1900
|
|
*/
|
|
public function testDateTimeExcelToTimestamp1900(float|int $expectedResult, float|int $excelDateTimeValue): void
|
|
{
|
|
if ($expectedResult > PHP_INT_MAX || $expectedResult < PHP_INT_MIN) {
|
|
self::markTestSkipped('Test invalid on 32-bit system.');
|
|
}
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
|
|
|
$result = Date::excelToTimestamp($excelDateTimeValue);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerDateTimeExcelToTimestamp1900(): array
|
|
{
|
|
return require 'tests/data/Shared/Date/ExcelToTimestamp1900.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeTimestampToExcel1900
|
|
*/
|
|
public function testDateTimeTimestampToExcel1900(float|int $expectedResult, float|int|string $unixTimestamp): void
|
|
{
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
|
|
|
$result = Date::timestampToExcel($unixTimestamp);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
|
}
|
|
|
|
public static function providerDateTimeTimestampToExcel1900(): array
|
|
{
|
|
return require 'tests/data/Shared/Date/TimestampToExcel1900.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeDateTimeToExcel
|
|
*/
|
|
public function testDateTimeDateTimeToExcel(float|int $expectedResult, DateTimeInterface $dateTimeObject): void
|
|
{
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
|
|
|
$result = Date::dateTimeToExcel($dateTimeObject);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
|
}
|
|
|
|
public static function providerDateTimeDateTimeToExcel(): array
|
|
{
|
|
return require 'tests/data/Shared/Date/DateTimeToExcel.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeFormattedPHPToExcel1900
|
|
*
|
|
* @param array{0: int, 1: int, 2: int, 3: int, 4: int, 5: float|int} $args Array containing year/month/day/hours/minutes/seconds
|
|
*/
|
|
public function testDateTimeFormattedPHPToExcel1900(mixed $expectedResult, ...$args): void
|
|
{
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
|
|
|
$result = Date::formattedPHPToExcel(...$args); // @phpstan-ignore-line
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
|
}
|
|
|
|
public static function providerDateTimeFormattedPHPToExcel1900(): array
|
|
{
|
|
return require 'tests/data/Shared/Date/FormattedPHPToExcel1900.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeExcelToTimestamp1904
|
|
*/
|
|
public function testDateTimeExcelToTimestamp1904(float|int $expectedResult, float|int $excelDateTimeValue): void
|
|
{
|
|
if ($expectedResult > PHP_INT_MAX || $expectedResult < PHP_INT_MIN) {
|
|
self::markTestSkipped('Test invalid on 32-bit system.');
|
|
}
|
|
Date::setExcelCalendar(Date::CALENDAR_MAC_1904);
|
|
|
|
$result = Date::excelToTimestamp($excelDateTimeValue);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerDateTimeExcelToTimestamp1904(): array
|
|
{
|
|
return require 'tests/data/Shared/Date/ExcelToTimestamp1904.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeTimestampToExcel1904
|
|
*/
|
|
public function testDateTimeTimestampToExcel1904(mixed $expectedResult, float|int|string $unixTimestamp): void
|
|
{
|
|
Date::setExcelCalendar(Date::CALENDAR_MAC_1904);
|
|
|
|
$result = Date::timestampToExcel($unixTimestamp);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
|
}
|
|
|
|
public static function providerDateTimeTimestampToExcel1904(): array
|
|
{
|
|
return require 'tests/data/Shared/Date/TimestampToExcel1904.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerIsDateTimeFormatCode
|
|
*/
|
|
public function testIsDateTimeFormatCode(mixed $expectedResult, string $format): void
|
|
{
|
|
$result = Date::isDateTimeFormatCode($format);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerIsDateTimeFormatCode(): array
|
|
{
|
|
return require 'tests/data/Shared/Date/FormatCodes.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeExcelToTimestamp1900Timezone
|
|
*/
|
|
public function testDateTimeExcelToTimestamp1900Timezone(int $expectedResult, float|int $excelDateTimeValue, string $timezone): void
|
|
{
|
|
if (is_numeric($expectedResult) && ($expectedResult > PHP_INT_MAX || $expectedResult < PHP_INT_MIN)) {
|
|
self::markTestSkipped('Test invalid on 32-bit system.');
|
|
}
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
|
|
|
$result = Date::excelToTimestamp($excelDateTimeValue, $timezone);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerDateTimeExcelToTimestamp1900Timezone(): array
|
|
{
|
|
return require 'tests/data/Shared/Date/ExcelToTimestamp1900Timezone.php';
|
|
}
|
|
|
|
public function testConvertIsoDateError(): void
|
|
{
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
|
|
|
$this->expectException(Exception::class);
|
|
$this->expectExceptionMessage('Non-string value supplied for Iso Date conversion');
|
|
Date::convertIsoDate(false);
|
|
}
|
|
|
|
public function testVarious(): void
|
|
{
|
|
Date::setDefaultTimeZone('UTC');
|
|
self::assertFalse(Date::stringToExcel('2019-02-29'));
|
|
self::assertTrue((bool) Date::stringToExcel('2019-02-28'));
|
|
self::assertTrue((bool) Date::stringToExcel('2019-02-28 11:18'));
|
|
self::assertFalse(Date::stringToExcel('2019-02-28 11:71'));
|
|
|
|
$date = Date::PHPToExcel('2020-01-01');
|
|
self::assertEquals(43831.0, $date);
|
|
|
|
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setCellValue('B1', 'x');
|
|
/** @var float|int|string */
|
|
$val = $sheet->getCell('B1')->getValue();
|
|
self::assertFalse(Date::timestampToExcel($val));
|
|
|
|
$cell = $sheet->getCell('A1');
|
|
self::assertNotNull($cell);
|
|
|
|
$cell->setValue($date);
|
|
$sheet->getStyle('A1')
|
|
->getNumberFormat()
|
|
->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME);
|
|
self::assertTrue(null !== $cell && Date::isDateTime($cell));
|
|
|
|
$cella2 = $sheet->getCell('A2');
|
|
self::assertNotNull($cella2);
|
|
|
|
$cella2->setValue('=A1+2');
|
|
$sheet->getStyle('A2')
|
|
->getNumberFormat()
|
|
->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME);
|
|
self::assertTrue(null !== $cella2 && Date::isDateTime($cella2));
|
|
|
|
$cella3 = $sheet->getCell('A3');
|
|
self::assertNotNull($cella3);
|
|
|
|
$cella3->setValue('=A1+4');
|
|
$sheet->getStyle('A3')
|
|
->getNumberFormat()
|
|
->setFormatCode('0.00E+00');
|
|
self::assertFalse(null !== $cella3 && Date::isDateTime($cella3));
|
|
|
|
$cella4 = $sheet->getCell('A4');
|
|
self::assertNotNull($cella4);
|
|
|
|
$cella4->setValue('= 44 7510557347');
|
|
$sheet->getStyle('A4')
|
|
->getNumberFormat()
|
|
->setFormatCode('yyyy-mm-dd');
|
|
self::assertFalse(Date::isDateTime($cella4));
|
|
}
|
|
}
|