ToArrayTest.php 1.6 KB

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