PropertiesTest.php 6.5 KB

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