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:
parent
d593617287
commit
9545b7a0d1
|
|
@ -115,7 +115,7 @@ class Properties
|
|||
// Initialise values
|
||||
$this->lastModifiedBy = $this->creator;
|
||||
$this->created = self::intOrFloatTimestamp(null);
|
||||
$this->modified = self::intOrFloatTimestamp(null);
|
||||
$this->modified = $this->created;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Document;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
use PhpOffice\PhpSpreadsheet\Document\Properties;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
|
@ -14,20 +15,27 @@ class PropertiesTest extends TestCase
|
|||
*/
|
||||
private $properties;
|
||||
|
||||
/** @var float */
|
||||
private $startTime;
|
||||
|
||||
protected function setup(): void
|
||||
{
|
||||
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
|
||||
{
|
||||
$createdTime = $modifiedTime = time();
|
||||
self::assertSame('Unknown Creator', $this->properties->getCreator());
|
||||
self::assertSame('Unknown Creator', $this->properties->getLastModifiedBy());
|
||||
self::assertSame('Untitled Spreadsheet', $this->properties->getTitle());
|
||||
self::assertSame('', $this->properties->getCompany());
|
||||
self::assertSame($createdTime, $this->properties->getCreated());
|
||||
self::assertSame($modifiedTime, $this->properties->getModified());
|
||||
self::assertEquals($this->startTime, $this->properties->getCreated());
|
||||
self::assertEquals($this->startTime, $this->properties->getModified());
|
||||
}
|
||||
|
||||
public function testSetCreator(): void
|
||||
|
|
@ -46,10 +54,10 @@ class PropertiesTest extends TestCase
|
|||
*/
|
||||
public function testSetCreated($expectedCreationTime, $created): void
|
||||
{
|
||||
$expectedCreationTime = $expectedCreationTime ?? time();
|
||||
$expectedCreationTime = $expectedCreationTime ?? $this->startTime;
|
||||
|
||||
$this->properties->setCreated($created);
|
||||
self::assertSame($expectedCreationTime, $this->properties->getCreated());
|
||||
self::assertEquals($expectedCreationTime, $this->properties->getCreated());
|
||||
}
|
||||
|
||||
public function providerCreationTime(): array
|
||||
|
|
@ -78,10 +86,10 @@ class PropertiesTest extends TestCase
|
|||
*/
|
||||
public function testSetModified($expectedModifiedTime, $modified): void
|
||||
{
|
||||
$expectedModifiedTime = $expectedModifiedTime ?? time();
|
||||
$expectedModifiedTime = $expectedModifiedTime ?? $this->startTime;
|
||||
|
||||
$this->properties->setModified($modified);
|
||||
self::assertSame($expectedModifiedTime, $this->properties->getModified());
|
||||
self::assertEquals($expectedModifiedTime, $this->properties->getModified());
|
||||
}
|
||||
|
||||
public function providerModifiedTime(): array
|
||||
|
|
|
|||
Loading…
Reference in New Issue