Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/Xlsx/PropertiesTest.php
T
Adrien Crivelli ec4098c8fd Strict mode for all tests
While we might never be able to have 100% of our code strict, we can at
the very least do it for all of our tests. This ensures that our tests
are using our API with the types as intended by the test author, and not
silently be cast to what our API requires.
2023-09-07 17:44:56 +08:00

98 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use DateTimeZone;
use PhpOffice\PhpSpreadsheet\Document\Properties;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
class PropertiesTest extends AbstractFunctional
{
public function testLoadXlsxWorkbookProperties(): void
{
$customPropertySet = [
'Publisher' => ['type' => Properties::PROPERTY_TYPE_STRING, 'value' => 'PHPOffice Suite'],
'Tested' => ['type' => Properties::PROPERTY_TYPE_BOOLEAN, 'value' => true],
'Counter' => ['type' => Properties::PROPERTY_TYPE_INTEGER, 'value' => 15],
'Rate' => ['type' => Properties::PROPERTY_TYPE_FLOAT, 'value' => 1.15],
'Refactor Date' => ['type' => Properties::PROPERTY_TYPE_DATE, 'value' => '2019-06-10'],
];
$filename = 'tests/data/Reader/XLSX/propertyTest.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);
$properties = $spreadsheet->getProperties();
// Core Properties
self::assertSame('Mark Baker', $properties->getCreator());
self::assertSame('Unit Testing', $properties->getTitle());
self::assertSame('Property Test', $properties->getSubject());
// Extended Properties
self::assertSame('PHPOffice', $properties->getCompany());
self::assertSame('The Big Boss', $properties->getManager());
// Custom Properties
$customProperties = $properties->getCustomProperties();
self::assertIsArray($customProperties);
$customProperties = array_flip($customProperties);
self::assertArrayHasKey('Publisher', $customProperties);
foreach ($customPropertySet as $propertyName => $testData) {
self::assertTrue($properties->isCustomPropertySet($propertyName));
self::assertSame($testData['type'], $properties->getCustomPropertyType($propertyName));
$result = $properties->getCustomPropertyValue($propertyName);
if ($properties->getCustomPropertyType($propertyName) == Properties::PROPERTY_TYPE_DATE) {
$result = Date::formattedDateTimeFromTimestamp("$result", 'Y-m-d', new DateTimeZone('UTC'));
}
self::assertSame($testData['value'], $result);
}
}
public function testReloadXlsxWorkbookProperties(): void
{
$customPropertySet = [
'Publisher' => ['type' => Properties::PROPERTY_TYPE_STRING, 'value' => 'PHPOffice Suite'],
'Tested' => ['type' => Properties::PROPERTY_TYPE_BOOLEAN, 'value' => true],
'Counter' => ['type' => Properties::PROPERTY_TYPE_INTEGER, 'value' => 15],
'Rate' => ['type' => Properties::PROPERTY_TYPE_FLOAT, 'value' => 1.15],
'Refactor Date' => ['type' => Properties::PROPERTY_TYPE_DATE, 'value' => '2019-06-10'],
];
$filename = 'tests/data/Reader/XLSX/propertyTest.xlsx';
$reader = new Xlsx();
$spreadsheetOld = $reader->load($filename);
$spreadsheet = $this->writeAndReload($spreadsheetOld, 'Xlsx');
$properties = $spreadsheet->getProperties();
// Core Properties
self::assertSame('Mark Baker', $properties->getCreator());
self::assertSame('Unit Testing', $properties->getTitle());
self::assertSame('Property Test', $properties->getSubject());
// Extended Properties
self::assertSame('PHPOffice', $properties->getCompany());
self::assertSame('The Big Boss', $properties->getManager());
// Custom Properties
$customProperties = $properties->getCustomProperties();
self::assertIsArray($customProperties);
$customProperties = array_flip($customProperties);
self::assertArrayHasKey('Publisher', $customProperties);
foreach ($customPropertySet as $propertyName => $testData) {
self::assertTrue($properties->isCustomPropertySet($propertyName));
self::assertSame($testData['type'], $properties->getCustomPropertyType($propertyName));
$result = $properties->getCustomPropertyValue($propertyName);
if ($properties->getCustomPropertyType($propertyName) == Properties::PROPERTY_TYPE_DATE) {
$result = Date::formattedDateTimeFromTimestamp("$result", 'Y-m-d', new DateTimeZone('UTC'));
}
self::assertSame($testData['value'], $result);
}
}
}