Fix Fluke Failure in Document Properties Test (#2738)

PR #2720 failed because a timestamp in Document Properties Test was off by 1. This was due to one of two possible reasons. The constructor for Properties set the Created and Modified times using separate calls to the time function; if those happened to occur in different seconds, the test would fail. The test might also fail if the Created and Modified times used the same timestamp, but the time used to compare against those was calculated in a different second. It is surprising that this failure hasn't shown up before. Regardless, this PR corrects both possible problems.
This commit is contained in:
oleibman 2022-04-17 08:44:29 -07:00 committed by GitHub
parent d593617287
commit 9545b7a0d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 9 deletions

View File

@ -115,7 +115,7 @@ class Properties
// Initialise values // Initialise values
$this->lastModifiedBy = $this->creator; $this->lastModifiedBy = $this->creator;
$this->created = self::intOrFloatTimestamp(null); $this->created = self::intOrFloatTimestamp(null);
$this->modified = self::intOrFloatTimestamp(null); $this->modified = $this->created;
} }
/** /**

View File

@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Document; namespace PhpOffice\PhpSpreadsheetTests\Document;
use DateTime;
use DateTimeZone; use DateTimeZone;
use PhpOffice\PhpSpreadsheet\Document\Properties; use PhpOffice\PhpSpreadsheet\Document\Properties;
use PhpOffice\PhpSpreadsheet\Shared\Date; use PhpOffice\PhpSpreadsheet\Shared\Date;
@ -14,20 +15,27 @@ class PropertiesTest extends TestCase
*/ */
private $properties; private $properties;
/** @var float */
private $startTime;
protected function setup(): void protected function setup(): void
{ {
$this->properties = new Properties(); do {
// loop to avoid rare situation where timestamp changes
$this->startTime = (float) (new DateTime())->format('U');
$this->properties = new Properties();
$endTime = (float) (new DateTime())->format('U');
} while ($this->startTime !== $endTime);
} }
public function testNewInstance(): void public function testNewInstance(): void
{ {
$createdTime = $modifiedTime = time();
self::assertSame('Unknown Creator', $this->properties->getCreator()); self::assertSame('Unknown Creator', $this->properties->getCreator());
self::assertSame('Unknown Creator', $this->properties->getLastModifiedBy()); self::assertSame('Unknown Creator', $this->properties->getLastModifiedBy());
self::assertSame('Untitled Spreadsheet', $this->properties->getTitle()); self::assertSame('Untitled Spreadsheet', $this->properties->getTitle());
self::assertSame('', $this->properties->getCompany()); self::assertSame('', $this->properties->getCompany());
self::assertSame($createdTime, $this->properties->getCreated()); self::assertEquals($this->startTime, $this->properties->getCreated());
self::assertSame($modifiedTime, $this->properties->getModified()); self::assertEquals($this->startTime, $this->properties->getModified());
} }
public function testSetCreator(): void public function testSetCreator(): void
@ -46,10 +54,10 @@ class PropertiesTest extends TestCase
*/ */
public function testSetCreated($expectedCreationTime, $created): void public function testSetCreated($expectedCreationTime, $created): void
{ {
$expectedCreationTime = $expectedCreationTime ?? time(); $expectedCreationTime = $expectedCreationTime ?? $this->startTime;
$this->properties->setCreated($created); $this->properties->setCreated($created);
self::assertSame($expectedCreationTime, $this->properties->getCreated()); self::assertEquals($expectedCreationTime, $this->properties->getCreated());
} }
public function providerCreationTime(): array public function providerCreationTime(): array
@ -78,10 +86,10 @@ class PropertiesTest extends TestCase
*/ */
public function testSetModified($expectedModifiedTime, $modified): void public function testSetModified($expectedModifiedTime, $modified): void
{ {
$expectedModifiedTime = $expectedModifiedTime ?? time(); $expectedModifiedTime = $expectedModifiedTime ?? $this->startTime;
$this->properties->setModified($modified); $this->properties->setModified($modified);
self::assertSame($expectedModifiedTime, $this->properties->getModified()); self::assertEquals($expectedModifiedTime, $this->properties->getModified());
} }
public function providerModifiedTime(): array public function providerModifiedTime(): array