WithConditionalSheetsTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Maatwebsite\Excel\Concerns\Importable;
  4. use Maatwebsite\Excel\Concerns\ToArray;
  5. use Maatwebsite\Excel\Concerns\WithConditionalSheets;
  6. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  7. use Maatwebsite\Excel\Tests\TestCase;
  8. class WithConditionalSheetsTest extends TestCase
  9. {
  10. /**
  11. * Setup the test environment.
  12. */
  13. protected function setUp(): void
  14. {
  15. parent::setUp();
  16. $this->withFactories(__DIR__ . '/../Data/Stubs/Database/Factories');
  17. }
  18. /**
  19. * @test
  20. */
  21. public function can_select_which_sheets_will_be_imported()
  22. {
  23. $import = new class implements WithMultipleSheets
  24. {
  25. use Importable, WithConditionalSheets;
  26. public $sheets = [];
  27. public function __construct()
  28. {
  29. $this->init();
  30. }
  31. public function init()
  32. {
  33. $this->sheets = [
  34. 'Sheet1' => new class implements ToArray
  35. {
  36. public $called = false;
  37. public function array(array $array)
  38. {
  39. $this->called = true;
  40. }
  41. },
  42. 'Sheet2' => new class implements ToArray
  43. {
  44. public $called = false;
  45. public function array(array $array)
  46. {
  47. $this->called = true;
  48. }
  49. },
  50. ];
  51. }
  52. /**
  53. * @return array
  54. */
  55. public function conditionalSheets(): array
  56. {
  57. return $this->sheets;
  58. }
  59. };
  60. $import->onlySheets('Sheet1')->import('import-multiple-sheets.xlsx');
  61. $this->assertTrue($import->sheets['Sheet1']->called);
  62. $this->assertFalse($import->sheets['Sheet2']->called);
  63. $import->init();
  64. $import->onlySheets('Sheet2')->import('import-multiple-sheets.xlsx');
  65. $this->assertTrue($import->sheets['Sheet2']->called);
  66. $this->assertFalse($import->sheets['Sheet1']->called);
  67. $import->init();
  68. $import->onlySheets(['Sheet1', 'Sheet2'])->import('import-multiple-sheets.xlsx');
  69. $this->assertTrue($import->sheets['Sheet1']->called);
  70. $this->assertTrue($import->sheets['Sheet2']->called);
  71. $import->init();
  72. $import->onlySheets('Sheet1', 'Sheet2')->import('import-multiple-sheets.xlsx');
  73. $this->assertTrue($import->sheets['Sheet1']->called);
  74. $this->assertTrue($import->sheets['Sheet2']->called);
  75. }
  76. }