123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <?php
- namespace Maatwebsite\Excel\Tests\Concerns;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\Exportable;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\SkipsUnknownSheets;
- use Maatwebsite\Excel\Concerns\ToArray;
- use Maatwebsite\Excel\Concerns\WithMultipleSheets;
- use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
- use Maatwebsite\Excel\Tests\Data\Stubs\SheetForUsersFromView;
- use Maatwebsite\Excel\Tests\Data\Stubs\SheetWith100Rows;
- use Maatwebsite\Excel\Tests\TestCase;
- use PHPUnit\Framework\Assert;
- class WithMultipleSheetsTest extends TestCase
- {
- /**
- * Setup the test environment.
- */
- protected function setUp(): void
- {
- parent::setUp();
- $this->withFactories(__DIR__ . '/../Data/Stubs/Database/Factories');
- }
- /**
- * @test
- */
- public function can_export_with_multiple_sheets_using_collections()
- {
- $export = new class implements WithMultipleSheets
- {
- use Exportable;
- /**
- * @return SheetWith100Rows[]
- */
- public function sheets(): array
- {
- return [
- new SheetWith100Rows('A'),
- new SheetWith100Rows('B'),
- new SheetWith100Rows('C'),
- ];
- }
- };
- $export->store('from-view.xlsx');
- $this->assertCount(100, $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-view.xlsx', 'Xlsx', 0));
- $this->assertCount(100, $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-view.xlsx', 'Xlsx', 1));
- $this->assertCount(100, $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-view.xlsx', 'Xlsx', 2));
- }
- /**
- * @test
- */
- public function can_export_multiple_sheets_from_view()
- {
- /** @var Collection|User[] $users */
- $users = factory(User::class)->times(300)->make();
- $export = new class($users) implements WithMultipleSheets
- {
- use Exportable;
- /**
- * @var Collection
- */
- protected $users;
- /**
- * @param Collection $users
- */
- public function __construct(Collection $users)
- {
- $this->users = $users;
- }
- /**
- * @return SheetForUsersFromView[]
- */
- public function sheets(): array
- {
- return [
- new SheetForUsersFromView($this->users->forPage(1, 100)),
- new SheetForUsersFromView($this->users->forPage(2, 100)),
- new SheetForUsersFromView($this->users->forPage(3, 100)),
- ];
- }
- };
- $export->store('from-view.xlsx');
- $this->assertCount(101, $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-view.xlsx', 'Xlsx', 0));
- $this->assertCount(101, $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-view.xlsx', 'Xlsx', 1));
- $this->assertCount(101, $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-view.xlsx', 'Xlsx', 2));
- }
- /**
- * @test
- */
- public function unknown_sheet_index_will_throw_sheet_not_found_exception()
- {
- $this->expectException(\Maatwebsite\Excel\Exceptions\SheetNotFoundException::class);
- $this->expectExceptionMessage('Your requested sheet index: 9999 is out of bounds. The actual number of sheets is 2.');
- $import = new class implements WithMultipleSheets
- {
- use Importable;
- public function sheets(): array
- {
- return [
- 9999 => new class {
- },
- ];
- }
- };
- $import->import('import-multiple-sheets.xlsx');
- }
- /**
- * @test
- */
- public function unknown_sheet_name_will_throw_sheet_not_found_exception()
- {
- $this->expectException(\Maatwebsite\Excel\Exceptions\SheetNotFoundException::class);
- $this->expectExceptionMessage('Your requested sheet name [Some Random Sheet Name] is out of bounds.');
- $import = new class implements WithMultipleSheets
- {
- use Importable;
- public function sheets(): array
- {
- return [
- 'Some Random Sheet Name' => new class {
- },
- ];
- }
- };
- $import->import('import-multiple-sheets.xlsx');
- }
- /**
- * @test
- */
- public function unknown_sheet_name_can_be_ignored()
- {
- $import = new class implements WithMultipleSheets, SkipsUnknownSheets
- {
- use Importable;
- public $unknown;
- public function sheets(): array
- {
- return [
- 'Some Random Sheet Name' => new class {
- },
- ];
- }
- /**
- * @param string|int $sheetName
- */
- public function onUnknownSheet($sheetName)
- {
- $this->unknown = $sheetName;
- }
- };
- $import->import('import-multiple-sheets.xlsx');
- $this->assertEquals('Some Random Sheet Name', $import->unknown);
- }
- /**
- * @test
- */
- public function unknown_sheet_indices_can_be_ignored_per_name()
- {
- $import = new class implements WithMultipleSheets
- {
- use Importable;
- public function sheets(): array
- {
- return [
- 'Some Random Sheet Name' => new class implements SkipsUnknownSheets
- {
- /**
- * @param string|int $sheetName
- */
- public function onUnknownSheet($sheetName)
- {
- Assert::assertEquals('Some Random Sheet Name', $sheetName);
- }
- },
- ];
- }
- };
- $import->import('import-multiple-sheets.xlsx');
- }
- /**
- * @test
- */
- public function unknown_sheet_indices_can_be_ignored()
- {
- $import = new class implements WithMultipleSheets, SkipsUnknownSheets
- {
- use Importable;
- public $unknown;
- public function sheets(): array
- {
- return [
- 99999 => new class {
- },
- ];
- }
- /**
- * @param string|int $sheetName
- */
- public function onUnknownSheet($sheetName)
- {
- $this->unknown = $sheetName;
- }
- };
- $import->import('import-multiple-sheets.xlsx');
- $this->assertEquals(99999, $import->unknown);
- }
- /**
- * @test
- */
- public function unknown_sheet_indices_can_be_ignored_per_sheet()
- {
- $import = new class implements WithMultipleSheets
- {
- use Importable;
- public function sheets(): array
- {
- return [
- 99999 => new class implements SkipsUnknownSheets
- {
- /**
- * @param string|int $sheetName
- */
- public function onUnknownSheet($sheetName)
- {
- Assert::assertEquals(99999, $sheetName);
- }
- },
- ];
- }
- };
- $import->import('import-multiple-sheets.xlsx');
- }
- /**
- * @test
- */
- public function can_import_multiple_sheets()
- {
- $import = new class implements WithMultipleSheets
- {
- use Importable;
- public function sheets(): array
- {
- return [
- new class implements ToArray
- {
- public function array(array $array)
- {
- Assert::assertEquals([
- ['1.A1', '1.B1'],
- ['1.A2', '1.B2'],
- ], $array);
- }
- },
- new class implements ToArray
- {
- public function array(array $array)
- {
- Assert::assertEquals([
- ['2.A1', '2.B1'],
- ['2.A2', '2.B2'],
- ], $array);
- }
- },
- ];
- }
- };
- $import->import('import-multiple-sheets.xlsx');
- }
- /**
- * @test
- */
- public function can_import_multiple_sheets_by_sheet_name()
- {
- $import = new class implements WithMultipleSheets
- {
- use Importable;
- public function sheets(): array
- {
- return [
- 'Sheet2' => new class implements ToArray
- {
- public function array(array $array)
- {
- Assert::assertEquals([
- ['2.A1', '2.B1'],
- ['2.A2', '2.B2'],
- ], $array);
- }
- },
- 'Sheet1' => new class implements ToArray
- {
- public function array(array $array)
- {
- Assert::assertEquals([
- ['1.A1', '1.B1'],
- ['1.A2', '1.B2'],
- ], $array);
- }
- },
- ];
- }
- };
- $import->import('import-multiple-sheets.xlsx');
- }
- /**
- * @test
- */
- public function can_import_multiple_sheets_by_sheet_index_and_name()
- {
- $import = new class implements WithMultipleSheets
- {
- use Importable;
- public $sheets = [];
- public function __construct()
- {
- $this->sheets = [
- 0 => new class implements ToArray
- {
- public $called = false;
- public function array(array $array)
- {
- $this->called = true;
- Assert::assertEquals([
- ['1.A1', '1.B1'],
- ['1.A2', '1.B2'],
- ], $array);
- }
- },
- 'Sheet2' => new class implements ToArray
- {
- public $called = false;
- public function array(array $array)
- {
- $this->called = true;
- Assert::assertEquals([
- ['2.A1', '2.B1'],
- ['2.A2', '2.B2'],
- ], $array);
- }
- },
- ];
- }
- public function sheets(): array
- {
- return $this->sheets;
- }
- };
- $import->import('import-multiple-sheets.xlsx');
- foreach ($import->sheets as $sheet) {
- $this->assertTrue($sheet->called);
- }
- }
- /**
- * @test
- */
- public function can_import_multiple_sheets_by_sheet_name_and_index()
- {
- $import = new class implements WithMultipleSheets
- {
- use Importable;
- public $sheets = [];
- public function __construct()
- {
- $this->sheets = [
- 'Sheet1' => new class implements ToArray
- {
- public $called = false;
- public function array(array $array)
- {
- $this->called = true;
- Assert::assertEquals([
- ['1.A1', '1.B1'],
- ['1.A2', '1.B2'],
- ], $array);
- }
- },
- 1 => new class implements ToArray
- {
- public $called = false;
- public function array(array $array)
- {
- $this->called = true;
- Assert::assertEquals([
- ['2.A1', '2.B1'],
- ['2.A2', '2.B2'],
- ], $array);
- }
- },
- ];
- }
- public function sheets(): array
- {
- return $this->sheets;
- }
- };
- $import->import('import-multiple-sheets.xlsx');
- foreach ($import->sheets as $sheet) {
- $this->assertTrue($sheet->called);
- }
- }
- }
|