Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Shared/CodePageTest.php
oleibman 8729a68338 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
2021-07-12 03:02:47 +02:00

79 lines
2.1 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Shared;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Shared\CodePage;
use PHPUnit\Framework\TestCase;
class CodePageTest extends TestCase
{
/**
* @dataProvider providerCodePage
*
* @param mixed $expectedResult
* @param mixed $codePageIndex
*/
public function testCodePageNumberToName($expectedResult, $codePageIndex): void
{
if ($expectedResult === 'exception') {
$this->expectException(Exception::class);
}
$result = CodePage::numberToName($codePageIndex);
if (is_array($expectedResult)) {
self::assertContains($result, $expectedResult);
} else {
self::assertEquals($expectedResult, $result);
}
}
public function providerCodePage(): array
{
return require 'tests/data/Shared/CodePage.php';
}
public function testCoverage(): void
{
$covered = [];
$expected = CodePage::getEncodings();
foreach ($expected as $key => $val) {
$covered[$key] = 0;
}
$tests = $this->providerCodePage();
foreach ($tests as $test) {
$covered[$test[1]] = 1;
}
foreach ($covered as $key => $val) {
self::assertEquals(1, $val, "Codepage $key not tested");
}
}
public function testNumberToNameWithInvalidCodePage(): void
{
$invalidCodePage = 12345;
try {
CodePage::numberToName($invalidCodePage);
} catch (Exception $e) {
self::assertEquals($e->getMessage(), 'Unknown codepage: 12345');
return;
}
self::fail('An expected exception has not been raised.');
}
public function testNumberToNameWithUnsupportedCodePage(): void
{
$unsupportedCodePage = 720;
try {
CodePage::numberToName($unsupportedCodePage);
} catch (Exception $e) {
self::assertEquals($e->getMessage(), 'Code page 720 not supported.');
return;
}
self::fail('An expected exception has not been raised.');
}
}