forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IOFactory::identify and Custom Reader/Writer
Fix PHPOffice#4357.`identify` returns a type, not a class name, and this result is not always usable for createReader. The docs say that it is usable in that manner. Changing the behavior of `identify` would be a breaking change, but adding an optional parameter (defaulting to current behavior) allowing it to return a class name rather than a type would not. This PR adds that parameter and updates the documentation to suggest its use for the code in question. To complete this change, `createReader` now accepts either a type (current behavior) or class name (new). Although it is not particularly identified as a possible problem by the issue, `createWriter` is similarly changed for consistency's sake.
- Loading branch information
Showing
6 changed files
with
152 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests; | ||
|
||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader; | ||
|
||
// Used in IOFactoryRegister tests | ||
class CustomReader extends XlsxReader | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests; | ||
|
||
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter; | ||
|
||
// Used in IOFactoryRegister tests | ||
class CustomWriter extends HtmlWriter | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests; | ||
|
||
use PhpOffice\PhpSpreadsheet\IOFactory; | ||
use PhpOffice\PhpSpreadsheet\Reader; | ||
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Writer; | ||
use PHPUnit\Framework\Attributes; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
// Separate processes because register arrays are static | ||
#[Attributes\RunTestsInSeparateProcesses] | ||
class IOFactoryRegisterTest extends TestCase | ||
{ | ||
public function testRegisterWriter(): void | ||
{ | ||
IOFactory::registerWriter('Pdf', Writer\Pdf\Mpdf::class); | ||
$spreadsheet = new Spreadsheet(); | ||
$actual = IOFactory::createWriter($spreadsheet, 'Pdf'); | ||
self::assertInstanceOf(Writer\Pdf\Mpdf::class, $actual); | ||
} | ||
|
||
public function testRegisterReader(): void | ||
{ | ||
IOFactory::registerReader('Custom', Reader\Html::class); | ||
$actual = IOFactory::createReader('Custom'); | ||
self::assertInstanceOf(Reader\Html::class, $actual); | ||
} | ||
|
||
public function testRegisterInvalidWriter(): void | ||
{ | ||
$this->expectException(Writer\Exception::class); | ||
$this->expectExceptionMessage('writers must implement'); | ||
IOFactory::registerWriter('foo', 'bar'); // @phpstan-ignore-line | ||
} | ||
|
||
public function testRegisterInvalidReader(): void | ||
{ | ||
$this->expectException(ReaderException::class); | ||
$this->expectExceptionMessage('readers must implement'); | ||
IOFactory::registerReader('foo', 'bar'); // @phpstan-ignore-line | ||
} | ||
|
||
public static function testRegisterCustomReader(): void | ||
{ | ||
IOFactory::registerReader(IOFactory::READER_XLSX, CustomReader::class); | ||
$inputFileName = 'tests/data/Reader/Xlsx/1900_Calendar.xlsx'; | ||
$loadSpreadsheet = IOFactory::load($inputFileName); | ||
$sheet = $loadSpreadsheet->getActiveSheet(); | ||
self::assertSame('2022-01-01', $sheet->getCell('A1')->getFormattedValue()); | ||
$loadSpreadsheet->disconnectWorksheets(); | ||
|
||
$reader = new CustomReader(); | ||
$newSpreadsheet = $reader->load($inputFileName); | ||
$newSheet = $newSpreadsheet->getActiveSheet(); | ||
self::assertSame('2022-01-01', $newSheet->getCell('A1')->getFormattedValue()); | ||
$newSpreadsheet->disconnectWorksheets(); | ||
|
||
$inputFileType = IOFactory::identify($inputFileName, null, true); | ||
$objReader = IOFactory::createReader($inputFileType); | ||
self::assertInstanceOf(CustomReader::class, $objReader); | ||
$objSpreadsheet = $objReader->load($inputFileName); | ||
$objSheet = $objSpreadsheet->getActiveSheet(); | ||
self::assertSame('2022-01-01', $objSheet->getCell('A1')->getFormattedValue()); | ||
$objSpreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public static function testRegisterCustomWriter(): void | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$sheet->setCellValue('A1', 1); | ||
$writer = new CustomWriter($spreadsheet); | ||
$html = $writer->generateHtmlAll(); | ||
self::assertStringContainsString('<td class="column0 style0 n">1</td>', $html); | ||
IOFactory::registerWriter(IOFactory::WRITER_HTML, CustomWriter::class); | ||
$objWriter = IOFactory::createWriter($spreadsheet, CustomWriter::class); | ||
self::assertInstanceOf(CustomWriter::class, $objWriter); | ||
self::assertTrue(method_exists($objWriter, 'generateHtmlAll')); | ||
$html2 = $objWriter->generateHtmlAll(); | ||
self::assertStringContainsString('<td class="column0 style0 n">1</td>', $html2); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters