mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-31 12:40:00 +00:00
31174c1c90
Fix #954, which went stale 6 years ago and which I have now reopened. The `setLocale` method in `Calculation` and `Settings` does not affect the 3 localizable properties in `StringHelper` - `currencyCode`, `decimalSeparator`, and `thousandsSeparator`. One way to work around this problem is to set those properties separately; this is probably how most people handle it. Another way is to call Php's own `setlocale` function; this does not require any foreknowledge of what the values need to be, but it comes with baggage (data is maintained at process level rather than thread level), so its use is discouraged. This PR adds a new `setLocale` method to `StringHelper`. It sets the 3 properties and the `Calculation` language all at once. It depends on the `Intl` extension, which is a recommendation but not a formal requirement for PhpSpreadsheet; the method will return `false` if `Intl` is not available or it thinks the supplied locale is not valid.
127 lines
4.2 KiB
PHP
127 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xml;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
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)] = [(string) 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('');
|
|
}
|
|
|
|
/**
|
|
* Ensures that a PHP warning for `Undefined array key "x"` is not triggered.
|
|
*
|
|
* Relies on PHPUnit's conversion of PHP warnings, deprecations, and notices to exceptions.
|
|
* If that warning occurs, the test should fail.
|
|
*/
|
|
public function testLoadXlsBug4669(): void
|
|
{
|
|
$filename = 'tests/data/Reader/Xml/bug4669.xml';
|
|
|
|
$reader = IOFactory::createReaderForFile($filename);
|
|
$reader->setReadDataOnly(true);
|
|
|
|
$spreadsheet = $reader->load($filename);
|
|
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
self::assertSame('Report Date', $sheet->getCell('A1')->getValue());
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
}
|