Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/Xlsx/PropertiesTest.php
T
oleibman 3eedf9e2f0 Better Typing in Test Members
Change "mixed" declarations to more accurate types in test members; in particular, change those that would be flagged if we were to run Phpstan at level 9 (we currently run level 8). I may or may not follow up with source code (over 700 level-9 problems remain for src), but, as with strict typing, there is no reason to avoid the effort for test members.

It was necessary to update some doc blocks in src to accommodate this change. However, no executable code is touched.
2024-01-05 01:43:50 -08:00

100 lines
4.5 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));
/** @var float|int */
$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));
/** @var float|int */
$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);
}
}
}