1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Document; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use DateTimeZone; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Document\Properties; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date; |
11
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class PropertiesTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
private Properties $properties; |
17
|
|
|
|
18
|
|
|
private float $startTime; |
19
|
|
|
|
20
|
|
|
protected function setUp(): void |
21
|
|
|
{ |
22
|
|
|
do { |
23
|
|
|
// loop to avoid rare situation where timestamp changes |
24
|
|
|
$this->startTime = (float) (new DateTime())->format('U'); |
25
|
|
|
$this->properties = new Properties(); |
26
|
|
|
$endTime = (float) (new DateTime())->format('U'); |
27
|
|
|
} while ($this->startTime !== $endTime); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testNewInstance(): void |
31
|
|
|
{ |
32
|
|
|
self::assertSame('Unknown Creator', $this->properties->getCreator()); |
33
|
|
|
self::assertSame('Unknown Creator', $this->properties->getLastModifiedBy()); |
34
|
|
|
self::assertSame('Untitled Spreadsheet', $this->properties->getTitle()); |
35
|
|
|
self::assertSame('', $this->properties->getCompany()); |
36
|
|
|
self::assertEquals($this->startTime, $this->properties->getCreated()); |
37
|
|
|
self::assertEquals($this->startTime, $this->properties->getModified()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testSetCreator(): void |
41
|
|
|
{ |
42
|
|
|
$creator = 'Mark Baker'; |
43
|
|
|
|
44
|
|
|
$this->properties->setCreator($creator); |
45
|
|
|
self::assertSame($creator, $this->properties->getCreator()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
#[DataProvider('providerCreationTime')] |
49
|
|
|
public function testSetCreated(null|int $expectedCreationTime, null|int|string $created): void |
50
|
|
|
{ |
51
|
|
|
if ($expectedCreationTime === null) { |
52
|
|
|
do { |
53
|
|
|
// loop to avoid rare situation where timestamp changes |
54
|
|
|
$expectedCreationTime = (float) (new DateTime())->format('U'); |
55
|
|
|
$this->properties->setCreated($created); |
56
|
|
|
$endTime = (float) (new DateTime())->format('U'); |
57
|
|
|
} while ($expectedCreationTime !== $endTime); |
58
|
|
|
} else { |
59
|
|
|
$this->properties->setCreated($created); |
60
|
|
|
} |
61
|
|
|
self::assertEquals($expectedCreationTime, $this->properties->getCreated()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function providerCreationTime(): array |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
[null, null], |
68
|
|
|
[1615980600, 1615980600], |
69
|
|
|
[1615980600, '1615980600'], |
70
|
|
|
[1615980600, '2021-03-17 11:30:00Z'], |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testSetModifier(): void |
75
|
|
|
{ |
76
|
|
|
$creator = 'Mark Baker'; |
77
|
|
|
|
78
|
|
|
$this->properties->setLastModifiedBy($creator); |
79
|
|
|
self::assertSame($creator, $this->properties->getLastModifiedBy()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
#[DataProvider('providerModifiedTime')] |
83
|
|
|
public function testSetModified(mixed $expectedModifiedTime, null|int|string $modified): void |
84
|
|
|
{ |
85
|
|
|
if ($expectedModifiedTime === null) { |
86
|
|
|
do { |
87
|
|
|
// loop to avoid rare situation where timestamp changes |
88
|
|
|
$expectedModifiedTime = (float) (new DateTime())->format('U'); |
89
|
|
|
$this->properties->setModified($modified); |
90
|
|
|
$endTime = (float) (new DateTime())->format('U'); |
91
|
|
|
} while ($expectedModifiedTime !== $endTime); |
92
|
|
|
} else { |
93
|
|
|
$this->properties->setModified($modified); |
94
|
|
|
} |
95
|
|
|
self::assertEquals($expectedModifiedTime, $this->properties->getModified()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public static function providerModifiedTime(): array |
99
|
|
|
{ |
100
|
|
|
return [ |
101
|
|
|
[null, null], |
102
|
|
|
[1615980600, 1615980600], |
103
|
|
|
[1615980600, '1615980600'], |
104
|
|
|
[1615980600, '2021-03-17 11:30:00Z'], |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testSetTitle(): void |
109
|
|
|
{ |
110
|
|
|
$title = 'My spreadsheet title test'; |
111
|
|
|
|
112
|
|
|
$this->properties->setTitle($title); |
113
|
|
|
self::assertSame($title, $this->properties->getTitle()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testSetDescription(): void |
117
|
|
|
{ |
118
|
|
|
$description = 'A test for spreadsheet description'; |
119
|
|
|
|
120
|
|
|
$this->properties->setDescription($description); |
121
|
|
|
self::assertSame($description, $this->properties->getDescription()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testSetSubject(): void |
125
|
|
|
{ |
126
|
|
|
$subject = 'Test spreadsheet'; |
127
|
|
|
|
128
|
|
|
$this->properties->setSubject($subject); |
129
|
|
|
self::assertSame($subject, $this->properties->getSubject()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testSetKeywords(): void |
133
|
|
|
{ |
134
|
|
|
$keywords = 'Test PHPSpreadsheet Spreadsheet Excel LibreOffice Gnumeric OpenSpreadsheetML OASIS'; |
135
|
|
|
|
136
|
|
|
$this->properties->setKeywords($keywords); |
137
|
|
|
self::assertSame($keywords, $this->properties->getKeywords()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testSetCategory(): void |
141
|
|
|
{ |
142
|
|
|
$category = 'Testing'; |
143
|
|
|
|
144
|
|
|
$this->properties->setCategory($category); |
145
|
|
|
self::assertSame($category, $this->properties->getCategory()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testSetCompany(): void |
149
|
|
|
{ |
150
|
|
|
$company = 'PHPOffice Suite'; |
151
|
|
|
|
152
|
|
|
$this->properties->setCompany($company); |
153
|
|
|
self::assertSame($company, $this->properties->getCompany()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testSetManager(): void |
157
|
|
|
{ |
158
|
|
|
$manager = 'Mark Baker'; |
159
|
|
|
|
160
|
|
|
$this->properties->setManager($manager); |
161
|
|
|
self::assertSame($manager, $this->properties->getManager()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
#[DataProvider('providerCustomProperties')] |
165
|
|
|
public function testSetCustomProperties(mixed $expectedType, mixed $expectedValue, string $propertyName, null|bool|float|int|string $propertyValue, ?string $propertyType = null): void |
166
|
|
|
{ |
167
|
|
|
if ($propertyType === null) { |
168
|
|
|
$this->properties->setCustomProperty($propertyName, $propertyValue); |
169
|
|
|
} else { |
170
|
|
|
$this->properties->setCustomProperty($propertyName, $propertyValue, $propertyType); |
171
|
|
|
} |
172
|
|
|
self::assertTrue($this->properties->isCustomPropertySet($propertyName)); |
173
|
|
|
self::assertSame($expectedType, $this->properties->getCustomPropertyType($propertyName)); |
174
|
|
|
/** @var float|int|string */ |
175
|
|
|
$result = $this->properties->getCustomPropertyValue($propertyName); |
176
|
|
|
if ($expectedType === Properties::PROPERTY_TYPE_DATE) { |
177
|
|
|
$result = Date::formattedDateTimeFromTimestamp("$result", 'Y-m-d', new DateTimeZone('UTC')); |
178
|
|
|
} |
179
|
|
|
self::assertSame($expectedValue, $result); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public static function providerCustomProperties(): array |
183
|
|
|
{ |
184
|
|
|
return [ |
185
|
|
|
[Properties::PROPERTY_TYPE_STRING, null, 'Editor', null], |
186
|
|
|
[Properties::PROPERTY_TYPE_STRING, 'Mark Baker', 'Editor', 'Mark Baker'], |
187
|
|
|
[Properties::PROPERTY_TYPE_FLOAT, 1.17, 'Version', 1.17], |
188
|
|
|
[Properties::PROPERTY_TYPE_INTEGER, 2, 'Revision', 2], |
189
|
|
|
[Properties::PROPERTY_TYPE_BOOLEAN, true, 'Tested', true], |
190
|
|
|
[Properties::PROPERTY_TYPE_DATE, '2021-03-17', 'Test Date', '2021-03-17T00:00:00Z', Properties::PROPERTY_TYPE_DATE], |
191
|
|
|
]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testGetUnknownCustomProperties(): void |
195
|
|
|
{ |
196
|
|
|
$propertyName = 'I DONT EXIST'; |
197
|
|
|
|
198
|
|
|
self::assertFalse($this->properties->isCustomPropertySet($propertyName)); |
199
|
|
|
self::assertNull($this->properties->getCustomPropertyValue($propertyName)); |
200
|
|
|
self::assertNull($this->properties->getCustomPropertyType($propertyName)); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|