Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Document/EpochTest.php
oleibman 49e97f0914 Correct Some Problems Which Will Show Up for PHP8.1 (#2191)
* Reader/Gnumeric vs. Scrutinizer

Just reviewing Scrutinizer's list of "bugs". There are 19 ascribed to me. For some, I will definitely take no action (e.g. use of bitwise operators in AND, OR, and XOR functions). However, where I can clean things up so that Scrutinizer is satisfied and the resulting code is not too contorted, I will make an attempt.

I believe this is the only one with which will involve more than 2 or 3 changes. It fixes 5 items ascribed to me, and 4 to others.

* Use Strict Checking for in_array

* Correct Some Problems Which Will Show Up for PHP8.1

PHP8.1 wants to issue a message when you use a float where it thinks you ought to be using an int (it wants its implicit casts made explicit). This is causing unit tests to fail. The following corrections are made in this PR:
- Calculation.php tests `isset(self::binaryOperators[$token])`, where token can be a float. No numeric values are members of that array, so we can test for numeric before isset.
- SharedOle.php packs a float, intending it as an int, in 2 places. I simplified the logic here, and added explicit casts to avoid the problem. This is used by Xls Reader and Writer; as added confirmation, I added some timestamps from before 1970 (i.e. negative values) to Document/EpochTest. Because of this, the test suite has been verified for 32-bit PHP as well as PHP 8.1.
- Writer/Xlsx/StringTable tests `isset($aFlippedStringTable[$cellValue])`. This is the same problem as in Calculation, but requires a different solution. The same if statement here also tests that the datatype is string, but it does so after the isset test. Changing the order of these tests avoids the problem.

* Update OLE.php
2021-06-29 19:54:08 +02:00

98 lines
3.7 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Document;
use DateTime;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
class EpochTest extends AbstractFunctional
{
public function providerFormats(): array
{
return [
['Ods', '1921-03-17 11:30:00Z'],
['Ods', '2021-03-17 11:30:00Z'],
['Ods', '2041-03-17 11:30:00Z'],
['Xls', '1921-03-17 11:30:00Z'],
['Xls', '2021-03-17 11:30:00Z'],
['Xls', '2041-03-17 11:30:00Z'],
['Xlsx', '1921-03-17 11:30:00Z'],
['Xlsx', '2021-03-17 11:30:00Z'],
['Xlsx', '2041-03-17 11:30:00Z'],
];
}
/**
* @dataProvider providerFormats
*/
public function testSetCreated(string $format, string $timestamp): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue(1);
$spreadsheet->getProperties()->setCreated($timestamp);
$timestamp2 = preg_replace('/1-/', '2-', $timestamp);
self::AssertNotEquals($timestamp, $timestamp2);
$spreadsheet->getProperties()->setModified($timestamp2);
$timestamp3 = preg_replace('/1-/', '3-', $timestamp);
self::AssertNotEquals($timestamp, $timestamp3);
self::AssertNotEquals($timestamp2, $timestamp3);
$spreadsheet->getProperties()->setCustomProperty('cprop', $timestamp3, 'd');
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
$created = $reloadedSpreadsheet->getProperties()->getCreated();
$dt1 = DateTime::createFromFormat('U', "$created");
$modified = $reloadedSpreadsheet->getProperties()->getModified();
$dt2 = DateTime::createFromFormat('U', "$modified");
if ($dt1 === false || $dt2 === false) {
self::fail('Invalid timestamp for created or modified');
} else {
self::assertSame($timestamp, $dt1->format('Y-m-d H:i:s') . 'Z');
self::assertSame($timestamp2, $dt2->format('Y-m-d H:i:s') . 'Z');
}
if ($format === 'Xlsx' || $format === 'Ods') {
// No custom property support in Xls
$cprop = $reloadedSpreadsheet->getProperties()->getCustomPropertyValue('cprop');
if (!is_numeric($cprop)) {
self::fail('Cannot find custom property');
} else {
$dt3 = DateTime::createFromFormat('U', "$cprop");
if ($dt3 === false) {
self::fail('Invalid timestamp for custom property');
} else {
self::assertSame($timestamp3, $dt3->format('Y-m-d H:i:s') . 'Z');
}
}
}
}
public function providerFormats2(): array
{
return [
['Ods'],
['Xls'],
['Xlsx'],
];
}
/**
* @dataProvider providerFormats2
*/
public function testConsistentTimeStamp(string $format): void
{
$pgmstart = (float) (new DateTime())->format('U');
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue(1);
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
$pgmend = (float) (new DateTime())->format('U');
self::assertLessThanOrEqual($pgmend, $pgmstart);
$created = $reloadedSpreadsheet->getProperties()->getCreated();
$modified = $reloadedSpreadsheet->getProperties()->getModified();
self::assertLessThanOrEqual($pgmend, $created);
self::assertLessThanOrEqual($pgmend, $modified);
self::assertLessThanOrEqual($created, $pgmstart);
self::assertLessThanOrEqual($modified, $pgmstart);
}
}