Skip to content

Commit 9545b7a

Browse files
authored
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.
1 parent d593617 commit 9545b7a

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/PhpSpreadsheet/Document/Properties.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function __construct()
115115
// Initialise values
116116
$this->lastModifiedBy = $this->creator;
117117
$this->created = self::intOrFloatTimestamp(null);
118-
$this->modified = self::intOrFloatTimestamp(null);
118+
$this->modified = $this->created;
119119
}
120120

121121
/**

tests/PhpSpreadsheetTests/Document/PropertiesTest.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Document;
44

5+
use DateTime;
56
use DateTimeZone;
67
use PhpOffice\PhpSpreadsheet\Document\Properties;
78
use PhpOffice\PhpSpreadsheet\Shared\Date;
@@ -14,20 +15,27 @@ class PropertiesTest extends TestCase
1415
*/
1516
private $properties;
1617

18+
/** @var float */
19+
private $startTime;
20+
1721
protected function setup(): void
1822
{
19-
$this->properties = new Properties();
23+
do {
24+
// loop to avoid rare situation where timestamp changes
25+
$this->startTime = (float) (new DateTime())->format('U');
26+
$this->properties = new Properties();
27+
$endTime = (float) (new DateTime())->format('U');
28+
} while ($this->startTime !== $endTime);
2029
}
2130

2231
public function testNewInstance(): void
2332
{
24-
$createdTime = $modifiedTime = time();
2533
self::assertSame('Unknown Creator', $this->properties->getCreator());
2634
self::assertSame('Unknown Creator', $this->properties->getLastModifiedBy());
2735
self::assertSame('Untitled Spreadsheet', $this->properties->getTitle());
2836
self::assertSame('', $this->properties->getCompany());
29-
self::assertSame($createdTime, $this->properties->getCreated());
30-
self::assertSame($modifiedTime, $this->properties->getModified());
37+
self::assertEquals($this->startTime, $this->properties->getCreated());
38+
self::assertEquals($this->startTime, $this->properties->getModified());
3139
}
3240

3341
public function testSetCreator(): void
@@ -46,10 +54,10 @@ public function testSetCreator(): void
4654
*/
4755
public function testSetCreated($expectedCreationTime, $created): void
4856
{
49-
$expectedCreationTime = $expectedCreationTime ?? time();
57+
$expectedCreationTime = $expectedCreationTime ?? $this->startTime;
5058

5159
$this->properties->setCreated($created);
52-
self::assertSame($expectedCreationTime, $this->properties->getCreated());
60+
self::assertEquals($expectedCreationTime, $this->properties->getCreated());
5361
}
5462

5563
public function providerCreationTime(): array
@@ -78,10 +86,10 @@ public function testSetModifier(): void
7886
*/
7987
public function testSetModified($expectedModifiedTime, $modified): void
8088
{
81-
$expectedModifiedTime = $expectedModifiedTime ?? time();
89+
$expectedModifiedTime = $expectedModifiedTime ?? $this->startTime;
8290

8391
$this->properties->setModified($modified);
84-
self::assertSame($expectedModifiedTime, $this->properties->getModified());
92+
self::assertEquals($expectedModifiedTime, $this->properties->getModified());
8593
}
8694

8795
public function providerModifiedTime(): array

0 commit comments

Comments
 (0)