OnEachRowTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Maatwebsite\Excel\Concerns\Importable;
  4. use Maatwebsite\Excel\Concerns\OnEachRow;
  5. use Maatwebsite\Excel\Row;
  6. use Maatwebsite\Excel\Tests\TestCase;
  7. use PHPUnit\Framework\Assert;
  8. class OnEachRowTest extends TestCase
  9. {
  10. /**
  11. * @test
  12. */
  13. public function can_import_each_row_individually()
  14. {
  15. $import = new class implements OnEachRow
  16. {
  17. use Importable;
  18. public $called = 0;
  19. /**
  20. * @param Row $row
  21. */
  22. public function onRow(Row $row)
  23. {
  24. foreach ($row->getCellIterator() as $cell) {
  25. Assert::assertEquals('test', $cell->getValue());
  26. }
  27. Assert::assertEquals([
  28. 'test', 'test',
  29. ], $row->toArray());
  30. Assert::assertEquals('test', $row[0]);
  31. $this->called++;
  32. }
  33. };
  34. $import->import('import.xlsx');
  35. $this->assertEquals(2, $import->called);
  36. }
  37. /**
  38. * @test
  39. */
  40. public function it_respects_the_end_column()
  41. {
  42. $import = new class implements OnEachRow
  43. {
  44. use Importable;
  45. /**
  46. * @param Row $row
  47. */
  48. public function onRow(Row $row)
  49. {
  50. // Accessing a row as an array calls toArray() without an end
  51. // column. This saves the row in the cache, so we have to
  52. // invalidate the cache once the end column changes
  53. $row[0];
  54. Assert::assertEquals([
  55. 'test',
  56. ], $row->toArray(null, false, true, 'A'));
  57. }
  58. };
  59. $import->import('import.xlsx');
  60. }
  61. }