| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- namespace Maatwebsite\Excel\Tests\Concerns;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\ToArray;
- use Maatwebsite\Excel\Concerns\WithConditionalSheets;
- use Maatwebsite\Excel\Concerns\WithMultipleSheets;
- use Maatwebsite\Excel\Tests\TestCase;
- class WithConditionalSheetsTest extends TestCase
- {
- /**
- * Setup the test environment.
- */
- protected function setUp(): void
- {
- parent::setUp();
- $this->withFactories(__DIR__ . '/../Data/Stubs/Database/Factories');
- }
- /**
- * @test
- */
- public function can_select_which_sheets_will_be_imported()
- {
- $import = new class implements WithMultipleSheets
- {
- use Importable, WithConditionalSheets;
- public $sheets = [];
- public function __construct()
- {
- $this->init();
- }
- public function init()
- {
- $this->sheets = [
- 'Sheet1' => new class implements ToArray
- {
- public $called = false;
- public function array(array $array)
- {
- $this->called = true;
- }
- },
- 'Sheet2' => new class implements ToArray
- {
- public $called = false;
- public function array(array $array)
- {
- $this->called = true;
- }
- },
- ];
- }
- /**
- * @return array
- */
- public function conditionalSheets(): array
- {
- return $this->sheets;
- }
- };
- $import->onlySheets('Sheet1')->import('import-multiple-sheets.xlsx');
- $this->assertTrue($import->sheets['Sheet1']->called);
- $this->assertFalse($import->sheets['Sheet2']->called);
- $import->init();
- $import->onlySheets('Sheet2')->import('import-multiple-sheets.xlsx');
- $this->assertTrue($import->sheets['Sheet2']->called);
- $this->assertFalse($import->sheets['Sheet1']->called);
- $import->init();
- $import->onlySheets(['Sheet1', 'Sheet2'])->import('import-multiple-sheets.xlsx');
- $this->assertTrue($import->sheets['Sheet1']->called);
- $this->assertTrue($import->sheets['Sheet2']->called);
- $import->init();
- $import->onlySheets('Sheet1', 'Sheet2')->import('import-multiple-sheets.xlsx');
- $this->assertTrue($import->sheets['Sheet1']->called);
- $this->assertTrue($import->sheets['Sheet2']->called);
- }
- }
|