123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace Maatwebsite\Excel\Tests\Concerns;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\Exportable;
- use Maatwebsite\Excel\Concerns\FromCollection;
- use Maatwebsite\Excel\Concerns\WithCustomStartCell;
- use Maatwebsite\Excel\Concerns\WithHeadings;
- use Maatwebsite\Excel\Tests\TestCase;
- class WithHeadingsTest extends TestCase
- {
- /**
- * @test
- */
- public function can_export_from_collection_with_heading_row()
- {
- $export = new class implements FromCollection, WithHeadings
- {
- use Exportable;
- /**
- * @return Collection
- */
- public function collection()
- {
- return collect([
- ['A1', 'B1', 'C1'],
- ['A2', 'B2', 'C2'],
- ]);
- }
- /**
- * @return array
- */
- public function headings(): array
- {
- return ['A', 'B', 'C'];
- }
- };
- $response = $export->store('with-heading-store.xlsx');
- $this->assertTrue($response);
- $actual = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/with-heading-store.xlsx', 'Xlsx');
- $expected = [
- ['A', 'B', 'C'],
- ['A1', 'B1', 'C1'],
- ['A2', 'B2', 'C2'],
- ];
- $this->assertEquals($expected, $actual);
- }
- /**
- * @test
- */
- public function can_export_from_collection_with_multiple_heading_rows()
- {
- $export = new class implements FromCollection, WithHeadings
- {
- use Exportable;
- /**
- * @return Collection
- */
- public function collection()
- {
- return collect([
- ['A1', 'B1', 'C1'],
- ['A2', 'B2', 'C2'],
- ]);
- }
- /**
- * @return array
- */
- public function headings(): array
- {
- return [
- ['A', 'B', 'C'],
- ['Aa', 'Bb', 'Cc'],
- ];
- }
- };
- $response = $export->store('with-heading-store.xlsx');
- $this->assertTrue($response);
- $actual = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/with-heading-store.xlsx', 'Xlsx');
- $expected = [
- ['A', 'B', 'C'],
- ['Aa', 'Bb', 'Cc'],
- ['A1', 'B1', 'C1'],
- ['A2', 'B2', 'C2'],
- ];
- $this->assertEquals($expected, $actual);
- }
- /**
- * @test
- */
- public function can_export_from_collection_with_heading_row_with_custom_start_cell()
- {
- $export = new class implements FromCollection, WithHeadings, WithCustomStartCell
- {
- use Exportable;
- /**
- * @return Collection
- */
- public function collection()
- {
- return collect([
- ['A1', 'B1', 'C1'],
- ['A2', 'B2', 'C2'],
- ]);
- }
- /**
- * @return array
- */
- public function headings(): array
- {
- return ['A', 'B', 'C'];
- }
- /**
- * @return string
- */
- public function startCell(): string
- {
- return 'B2';
- }
- };
- $response = $export->store('with-heading-store.xlsx');
- $this->assertTrue($response);
- $actual = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/with-heading-store.xlsx', 'Xlsx');
- $expected = [
- [null, null, null, null],
- [null, 'A', 'B', 'C'],
- [null, 'A1', 'B1', 'C1'],
- [null, 'A2', 'B2', 'C2'],
- ];
- $this->assertEquals($expected, $actual);
- }
- }
|