mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-22 08:09:15 +00:00
ec4098c8fd
While we might never be able to have 100% of our code strict, we can at the very least do it for all of our tests. This ensures that our tests are using our API with the types as intended by the test author, and not silently be cast to what our API requires.
78 lines
2.1 KiB
PHP
78 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Shared;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Exception;
|
|
use PhpOffice\PhpSpreadsheet\Shared\CodePage;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class CodePageTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider providerCodePage
|
|
*/
|
|
public function testCodePageNumberToName(mixed $expectedResult, mixed $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 static 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.');
|
|
}
|
|
}
|