-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Basic OpenSpout Excel exporter * ExcelOpenSpoutExporter: test case for exporter collection --------- Co-authored-by: Maarten Visscher <[email protected]>
- Loading branch information
Showing
5 changed files
with
206 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* Symfony DataTables Bundle | ||
* (c) Omines Internetbureau B.V. - https://omines.nl/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Omines\DataTablesBundle\Exporter\Excel; | ||
|
||
use Omines\DataTablesBundle\Exporter\DataTableExporterInterface; | ||
use OpenSpout\Common\Entity\Row; | ||
use OpenSpout\Common\Entity\Style\Style; | ||
use OpenSpout\Writer\XLSX\Writer; | ||
|
||
/** | ||
* Excel exporter using OpenSpout. | ||
*/ | ||
class ExcelOpenSpoutExporter implements DataTableExporterInterface | ||
{ | ||
public function export(array $columnNames, \Iterator $data): \SplFileInfo | ||
{ | ||
$filePath = sys_get_temp_dir() . '/' . uniqid('dt') . '.xlsx'; | ||
|
||
$writer = new Writer(); | ||
$writer->openToFile($filePath); | ||
|
||
// Write header | ||
$boldStyle = (new Style())->setFontBold(); | ||
$writer->addRow(Row::fromValues($columnNames, $boldStyle)); | ||
|
||
// Write data | ||
foreach ($data as $row) { | ||
// Remove HTML tags | ||
$values = array_map('strip_tags', $row); | ||
|
||
$writer->addRow(Row::fromValues($values)); | ||
} | ||
|
||
$writer->close(); | ||
|
||
return new \SplFileInfo($filePath); | ||
} | ||
|
||
public function getMimeType(): string | ||
{ | ||
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'excel-openspout'; | ||
} | ||
} |
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
101 changes: 101 additions & 0 deletions
101
tests/Functional/Exporter/Excel/ExcelOpenSpoutExporterTest.php
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,101 @@ | ||
<?php | ||
|
||
/* | ||
* Symfony DataTables Bundle | ||
* (c) Omines Internetbureau B.V. - https://omines.nl/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Functional\Exporter\Excel; | ||
|
||
use PhpOffice\PhpSpreadsheet\IOFactory; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
|
||
class ExcelOpenSpoutExporterTest extends WebTestCase | ||
{ | ||
/** @var KernelBrowser */ | ||
private $client; | ||
|
||
protected function setUp(): void | ||
{ | ||
self::ensureKernelShutdown(); | ||
$this->client = self::createClient(); | ||
} | ||
|
||
public function testExport(): void | ||
{ | ||
$this->client->request('POST', '/exporter', ['_dt' => 'dt', '_exporter' => 'excel-openspout']); | ||
|
||
$response = $this->client->getResponse(); | ||
|
||
// Using PhpSpreadsheet for tests | ||
$sheet = IOFactory::load($response->getFile()->getPathname())->getActiveSheet(); | ||
|
||
static::assertSame('dt.columns.firstName', $sheet->getCell('A1')->getFormattedValue()); | ||
static::assertSame('dt.columns.lastName', $sheet->getCell('B1')->getFormattedValue()); | ||
|
||
static::assertSame('FirstName0', $sheet->getCell('A2')->getFormattedValue()); | ||
static::assertSame('LastName0', $sheet->getCell('B2')->getFormattedValue()); | ||
|
||
static::assertSame('FirstName1', $sheet->getCell('A3')->getFormattedValue()); | ||
static::assertSame('LastName1', $sheet->getCell('B3')->getFormattedValue()); | ||
|
||
static::assertSame('FirstName2', $sheet->getCell('A4')->getFormattedValue()); | ||
static::assertSame('LastName2', $sheet->getCell('B4')->getFormattedValue()); | ||
|
||
static::assertSame('FirstName3', $sheet->getCell('A5')->getFormattedValue()); | ||
static::assertSame('LastName3', $sheet->getCell('B5')->getFormattedValue()); | ||
|
||
static::assertSame('FirstName4', $sheet->getCell('A6')->getFormattedValue()); | ||
static::assertSame('LastName4', $sheet->getCell('B6')->getFormattedValue()); | ||
} | ||
|
||
public function testEmptyDataTable(): void | ||
{ | ||
$this->client->request('POST', '/exporter-empty-datatable', ['_dt' => 'dt', '_exporter' => 'excel-openspout']); | ||
|
||
/** @var BinaryFileResponse $response */ | ||
$response = $this->client->getResponse(); | ||
|
||
static::assertTrue($response->isSuccessful()); | ||
|
||
// Using PhpSpreadsheet for tests | ||
$sheet = IOFactory::load($response->getFile()->getPathname())->getActiveSheet(); | ||
|
||
static::assertSame('dt.columns.firstName', $sheet->getCell('A1')->getFormattedValue()); | ||
static::assertSame('dt.columns.lastName', $sheet->getCell('B1')->getFormattedValue()); | ||
|
||
static::assertEmpty($sheet->getCell('A2')->getFormattedValue()); | ||
static::assertEmpty($sheet->getCell('B2')->getFormattedValue()); | ||
} | ||
|
||
public function testWithSearch(): void | ||
{ | ||
$this->client->request('POST', '/exporter', [ | ||
'_dt' => 'dt', | ||
'_exporter' => 'excel-openspout', | ||
'search' => ['value' => 'FirstName124'], | ||
]); | ||
|
||
/** @var BinaryFileResponse $response */ | ||
$response = $this->client->getResponse(); | ||
|
||
// Using PhpSpreadsheet for tests | ||
$sheet = IOFactory::load($response->getFile()->getPathname())->getActiveSheet(); | ||
|
||
static::assertSame('dt.columns.firstName', $sheet->getCell('A1')->getFormattedValue()); | ||
static::assertSame('dt.columns.lastName', $sheet->getCell('B1')->getFormattedValue()); | ||
|
||
static::assertSame('FirstName124', $sheet->getCell('A2')->getFormattedValue()); | ||
static::assertSame('LastName124', $sheet->getCell('B2')->getFormattedValue()); | ||
|
||
static::assertEmpty($sheet->getCell('A3')->getFormattedValue()); | ||
static::assertEmpty($sheet->getCell('B3')->getFormattedValue()); | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
/* | ||
* Symfony DataTables Bundle | ||
* (c) Omines Internetbureau B.V. - https://omines.nl/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Unit\Exporter; | ||
|
||
use Omines\DataTablesBundle\Exporter\DataTableExporterCollection; | ||
use Omines\DataTablesBundle\Exporter\Excel\ExcelOpenSpoutExporter; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
class ExcelOpenSpoutExporterTest extends KernelTestCase | ||
{ | ||
/** @var DataTableExporterCollection */ | ||
private $exporterCollection; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->bootKernel(); | ||
|
||
$this->exporterCollection = $this->getContainer()->get(DataTableExporterCollection::class); | ||
} | ||
|
||
public function testTag(): void | ||
{ | ||
$this->assertInstanceOf(ExcelOpenSpoutExporter::class, $this->exporterCollection->getByName('excel-openspout')); | ||
} | ||
|
||
public function testName(): void | ||
{ | ||
$this->assertSame('excel-openspout', $this->exporterCollection->getByName('excel-openspout')->getName()); | ||
} | ||
} |