ToCollectionTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Support\Collection;
  4. use Maatwebsite\Excel\Concerns\Importable;
  5. use Maatwebsite\Excel\Concerns\ToCollection;
  6. use Maatwebsite\Excel\Tests\TestCase;
  7. use PHPUnit\Framework\Assert;
  8. class ToCollectionTest extends TestCase
  9. {
  10. /**
  11. * @test
  12. */
  13. public function can_import_to_collection()
  14. {
  15. $import = new class implements ToCollection
  16. {
  17. use Importable;
  18. public $called = false;
  19. /**
  20. * @param Collection $collection
  21. */
  22. public function collection(Collection $collection)
  23. {
  24. $this->called = true;
  25. Assert::assertEquals([
  26. ['test', 'test'],
  27. ['test', 'test'],
  28. ], $collection->toArray());
  29. }
  30. };
  31. $import->import('import.xlsx');
  32. $this->assertTrue($import->called);
  33. }
  34. /**
  35. * @test
  36. */
  37. public function can_import_multiple_sheets_to_collection()
  38. {
  39. $import = new class implements ToCollection
  40. {
  41. use Importable;
  42. public $called = 0;
  43. /**
  44. * @param Collection $collection
  45. */
  46. public function collection(Collection $collection)
  47. {
  48. $this->called++;
  49. $sheetNumber = $this->called;
  50. Assert::assertEquals([
  51. [$sheetNumber . '.A1', $sheetNumber . '.B1'],
  52. [$sheetNumber . '.A2', $sheetNumber . '.B2'],
  53. ], $collection->toArray());
  54. }
  55. };
  56. $import->import('import-multiple-sheets.xlsx');
  57. $this->assertEquals(2, $import->called);
  58. }
  59. }