Xls Reader Handle MACCENTRALEUROPE With or Without Hyphen (#2213)

* Xls Reader Handle MACCENTRALEUROPE With or Without Hyphen

Fixes issue #549 and https://github.com/Maatwebsite/Laravel-Excel/issues/989 (which is the source of the new test file). Some systems accept MACCENTRALEUROPE as the name for the appropriate encoding, and some accept MAC-CENTRALEUROPE. I fortunately have access to at least one of each type, and have run the tests on each.

CodePage.php has an array of translations from codepage number to string. I now allow the value to itself be an array; if so, the code will test each in turn to see if it can be used in iconv. I did not go fishing for other similar problems. If such show up, they can be dealt with in the same manner as this one. I don't really expect others, since this is a problem not merely for Xls, but, even then, it applies only to BIFF5 and earlier.

I also moved XlsTest from Reader to Reader/Xls.

* Cache Successful Result For Future Use

Per suggestion from @MarkBaker
This commit is contained in:
oleibman
2021-07-11 18:02:47 -07:00
committed by GitHub
parent 075cecd268
commit 8729a68338
6 changed files with 76 additions and 21 deletions
@@ -1,8 +1,10 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader;
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Shared\CodePage;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
class XlsTest extends AbstractFunctional
@@ -16,6 +18,7 @@ class XlsTest extends AbstractFunctional
$reader = new Xls();
$spreadsheet = $reader->load($filename);
self::assertEquals('Title', $spreadsheet->getSheet(0)->getCell('A1')->getValue());
$spreadsheet->disconnectWorksheets();
}
/**
@@ -42,6 +45,8 @@ class XlsTest extends AbstractFunctional
self::assertEquals($row, $newrow);
self::assertEquals($sheet->getCell('A1')->getFormattedValue(), $newsheet->getCell('A1')->getFormattedValue());
self::assertEquals($sheet->getCell("$col$row")->getFormattedValue(), $newsheet->getCell("$col$row")->getFormattedValue());
$spreadsheet->disconnectWorksheets();
$newspreadsheet->disconnectWorksheets();
}
/**
@@ -71,11 +76,49 @@ class XlsTest extends AbstractFunctional
$rowIterator = $sheet->getRowIterator();
foreach ($rowIterator as $row) {
foreach ($row->getCellIterator() as $cell) {
foreach ($row->getCellIterator() as $cellx) {
/** @var Cell */
$cell = $cellx;
$valOld = $cell->getFormattedValue();
$valNew = $newsheet->getCell($cell->getCoordinate())->getFormattedValue();
self::assertEquals($valOld, $valNew);
}
}
$spreadsheet->disconnectWorksheets();
$newspreadsheet->disconnectWorksheets();
}
/**
* Test load Xls file with MACCENTRALEUROPE encoding, which is implemented
* as MAC-CENTRALEUROPE on some systems. Issue #549.
*/
public function testLoadMacCentralEurope(): void
{
$codePages = CodePage::getEncodings();
self::assertIsArray($codePages[10029]);
$filename = 'tests/data/Reader/XLS/maccentraleurope.xls';
$reader = new Xls();
// When no fix applied, spreadsheet fails to load on some systems
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('Ładowność', $sheet->getCell('I1')->getValue());
$spreadsheet->disconnectWorksheets();
}
/**
* First test changes array entry in CodePage.
* This test confirms new that new entry is okay.
*/
public function testLoadMacCentralEurope2(): void
{
$codePages = CodePage::getEncodings();
self::assertIsString($codePages[10029]);
$filename = 'tests/data/Reader/XLS/maccentraleurope.xls';
$reader = new Xls();
// When no fix applied, spreadsheet fails to load on some systems
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('Ładowność', $sheet->getCell('I1')->getValue());
$spreadsheet->disconnectWorksheets();
}
}
@@ -16,8 +16,15 @@ class CodePageTest extends TestCase
*/
public function testCodePageNumberToName($expectedResult, $codePageIndex): void
{
if ($expectedResult === 'exception') {
$this->expectException(Exception::class);
}
$result = CodePage::numberToName($codePageIndex);
self::assertEquals($expectedResult, $result);
if (is_array($expectedResult)) {
self::assertContains($result, $expectedResult);
} else {
self::assertEquals($expectedResult, $result);
}
}
public function providerCodePage(): array