|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Symfony DataTables Bundle |
| 5 | + * (c) Omines Internetbureau B.V. - https://omines.nl/ |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +declare(strict_types=1); |
| 12 | + |
| 13 | +namespace Tests\Functional\Exporter\Excel; |
| 14 | + |
| 15 | +use PhpOffice\PhpSpreadsheet\IOFactory; |
| 16 | +use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
| 17 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 18 | +use Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 19 | + |
| 20 | +class ExcelOpenSpoutExporterTest extends WebTestCase |
| 21 | +{ |
| 22 | + /** @var KernelBrowser */ |
| 23 | + private $client; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + self::ensureKernelShutdown(); |
| 28 | + $this->client = self::createClient(); |
| 29 | + } |
| 30 | + |
| 31 | + public function testExport(): void |
| 32 | + { |
| 33 | + $this->client->request('POST', '/exporter', ['_dt' => 'dt', '_exporter' => 'excel-openspout']); |
| 34 | + |
| 35 | + $response = $this->client->getResponse(); |
| 36 | + |
| 37 | + // Using PhpSpreadsheet for tests |
| 38 | + $sheet = IOFactory::load($response->getFile()->getPathname())->getActiveSheet(); |
| 39 | + |
| 40 | + static::assertSame('dt.columns.firstName', $sheet->getCell('A1')->getFormattedValue()); |
| 41 | + static::assertSame('dt.columns.lastName', $sheet->getCell('B1')->getFormattedValue()); |
| 42 | + |
| 43 | + static::assertSame('FirstName0', $sheet->getCell('A2')->getFormattedValue()); |
| 44 | + static::assertSame('LastName0', $sheet->getCell('B2')->getFormattedValue()); |
| 45 | + |
| 46 | + static::assertSame('FirstName1', $sheet->getCell('A3')->getFormattedValue()); |
| 47 | + static::assertSame('LastName1', $sheet->getCell('B3')->getFormattedValue()); |
| 48 | + |
| 49 | + static::assertSame('FirstName2', $sheet->getCell('A4')->getFormattedValue()); |
| 50 | + static::assertSame('LastName2', $sheet->getCell('B4')->getFormattedValue()); |
| 51 | + |
| 52 | + static::assertSame('FirstName3', $sheet->getCell('A5')->getFormattedValue()); |
| 53 | + static::assertSame('LastName3', $sheet->getCell('B5')->getFormattedValue()); |
| 54 | + |
| 55 | + static::assertSame('FirstName4', $sheet->getCell('A6')->getFormattedValue()); |
| 56 | + static::assertSame('LastName4', $sheet->getCell('B6')->getFormattedValue()); |
| 57 | + } |
| 58 | + |
| 59 | + public function testEmptyDataTable(): void |
| 60 | + { |
| 61 | + $this->client->request('POST', '/exporter-empty-datatable', ['_dt' => 'dt', '_exporter' => 'excel-openspout']); |
| 62 | + |
| 63 | + /** @var BinaryFileResponse $response */ |
| 64 | + $response = $this->client->getResponse(); |
| 65 | + |
| 66 | + static::assertTrue($response->isSuccessful()); |
| 67 | + |
| 68 | + // Using PhpSpreadsheet for tests |
| 69 | + $sheet = IOFactory::load($response->getFile()->getPathname())->getActiveSheet(); |
| 70 | + |
| 71 | + static::assertSame('dt.columns.firstName', $sheet->getCell('A1')->getFormattedValue()); |
| 72 | + static::assertSame('dt.columns.lastName', $sheet->getCell('B1')->getFormattedValue()); |
| 73 | + |
| 74 | + static::assertEmpty($sheet->getCell('A2')->getFormattedValue()); |
| 75 | + static::assertEmpty($sheet->getCell('B2')->getFormattedValue()); |
| 76 | + } |
| 77 | + |
| 78 | + public function testWithSearch(): void |
| 79 | + { |
| 80 | + $this->client->request('POST', '/exporter', [ |
| 81 | + '_dt' => 'dt', |
| 82 | + '_exporter' => 'excel-openspout', |
| 83 | + 'search' => ['value' => 'FirstName124'], |
| 84 | + ]); |
| 85 | + |
| 86 | + /** @var BinaryFileResponse $response */ |
| 87 | + $response = $this->client->getResponse(); |
| 88 | + |
| 89 | + // Using PhpSpreadsheet for tests |
| 90 | + $sheet = IOFactory::load($response->getFile()->getPathname())->getActiveSheet(); |
| 91 | + |
| 92 | + static::assertSame('dt.columns.firstName', $sheet->getCell('A1')->getFormattedValue()); |
| 93 | + static::assertSame('dt.columns.lastName', $sheet->getCell('B1')->getFormattedValue()); |
| 94 | + |
| 95 | + static::assertSame('FirstName124', $sheet->getCell('A2')->getFormattedValue()); |
| 96 | + static::assertSame('LastName124', $sheet->getCell('B2')->getFormattedValue()); |
| 97 | + |
| 98 | + static::assertEmpty($sheet->getCell('A3')->getFormattedValue()); |
| 99 | + static::assertEmpty($sheet->getCell('B3')->getFormattedValue()); |
| 100 | + } |
| 101 | +} |
0 commit comments