Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/Xml/XmlTest.php
T
oleibman 00191100ba Deprecate Worksheet::getHashInt and Spreadsheet::getId
Php8.5 *may* deprecate the use of `__wakeup` (it is planned, but not yet implemented, and it is convtroversial). We use it only twice. In the first instance, it just throws an exception to prevent a security exploit. This can be trivially replaced with `__unserialize`.

The other instance is merely to initialize a Worksheet instance variable. Converting this use to `__unserialize` is *not* trivial (one of the reasons for the controversy). However, I see no useful purpose for that variable. Since it has no use, there is no real need for `__wakeup`, so we will just remove the routine altogether, and deprecate the variable's getter (there is no setter).

A similar instance variable in Spreadsheet also serves no useful purpose, so we will deprecate its getter as well (again no setter).
2025-09-10 00:27:22 -07:00

106 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xml;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
use PhpOffice\PhpSpreadsheet\Reader\Xml;
use PHPUnit\Framework\TestCase;
class XmlTest extends TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('providerInvalidSimpleXML')]
public function testInvalidSimpleXML(string $filename): void
{
$xmlReader = new Xml();
$this->expectException(ReaderException::class);
$this->expectExceptionMessage('Invalid Spreadsheet file');
$xmlReader->load($filename);
}
public static function providerInvalidSimpleXML(): array
{
$tests = [];
$glob = glob('tests/data/Reader/Xml/XEETestInvalidSimpleXML*.xml');
self::assertNotFalse($glob);
foreach ($glob as $file) {
$tests[basename($file)] = [realpath($file)];
}
return $tests;
}
/**
* Check if it can read XML Hyperlink correctly.
*/
public function testHyperlinksAltCharset(): void
{
$reader = new Xml();
$spreadsheet = $reader->load('tests/data/Reader/Xml/excel2003.iso8859-1.xml');
$firstSheet = $spreadsheet->getSheet(0);
self::assertSame('Voilà', $spreadsheet->getActiveSheet()->getCell('A1')->getValue());
$hyperlink = $firstSheet->getCell('A2');
self::assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType());
self::assertEquals('PhpSpreadsheet', $hyperlink->getValue());
self::assertEquals('https://phpspreadsheet.readthedocs.io', $hyperlink->getHyperlink()->getUrl());
$spreadsheet->disconnectWorksheets();
}
public function testLoadCorruptedFile(): void
{
$this->expectException(ReaderException::class);
$this->expectExceptionMessage('Cannot load invalid XML file');
$xmlReader = new Xml();
$spreadsheet = @$xmlReader->load('tests/data/Reader/Xml/CorruptedXmlFile.xml');
}
public function testListWorksheetNamesCorruptedFile(): void
{
$this->expectException(ReaderException::class);
$this->expectExceptionMessage('Problem reading');
$xmlReader = new Xml();
$names = @$xmlReader->listWorksheetNames('tests/data/Reader/Xml/CorruptedXmlFile.xml');
self::assertNotEmpty($names);
}
public function testListWorksheetInfoCorruptedFile(): void
{
$this->expectException(ReaderException::class);
$this->expectExceptionMessage('Problem reading');
$xmlReader = new Xml();
$info = @$xmlReader->listWorksheetInfo('tests/data/Reader/Xml/CorruptedXmlFile.xml');
self::assertNotEmpty($info);
}
public function testInvalidXMLFromString(): void
{
$xmlReader = new Xml();
$this->expectException(ReaderException::class);
$this->expectExceptionMessage('Cannot load invalid XML string: 0');
$xmlReader->loadSpreadsheetFromString('0');
}
public function testInvalidXMLFromEmptyString(): void
{
$xmlReader = new Xml();
$this->expectException(ReaderException::class);
$this->expectExceptionMessage('Cannot load invalid XML string: ');
$xmlReader->loadSpreadsheetFromString('');
}
public function testEmptyFilename(): void
{
$xmlReader = new Xml();
$this->expectException(ReaderException::class);
$this->expectExceptionMessage('File "" does not exist');
$xmlReader->load('');
}
}