SkipsEmptyRowsTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Collection;
  5. use Maatwebsite\Excel\Concerns\Importable;
  6. use Maatwebsite\Excel\Concerns\OnEachRow;
  7. use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
  8. use Maatwebsite\Excel\Concerns\ToCollection;
  9. use Maatwebsite\Excel\Concerns\ToModel;
  10. use Maatwebsite\Excel\Row;
  11. use Maatwebsite\Excel\Tests\TestCase;
  12. use PHPUnit\Framework\Assert;
  13. class SkipsEmptyRowsTest extends TestCase
  14. {
  15. /**
  16. * @test
  17. */
  18. public function skips_empty_rows_when_importing_to_collection()
  19. {
  20. $import = new class implements ToCollection, SkipsEmptyRows
  21. {
  22. use Importable;
  23. public $called = false;
  24. /**
  25. * @param Collection $collection
  26. */
  27. public function collection(Collection $collection)
  28. {
  29. $this->called = true;
  30. Assert::assertEquals([
  31. ['Test1', 'Test2'],
  32. ['Test3', 'Test4'],
  33. ['Test5', 'Test6'],
  34. ], $collection->toArray());
  35. }
  36. };
  37. $import->import('import-empty-rows.xlsx');
  38. $this->assertTrue($import->called);
  39. }
  40. /**
  41. * @test
  42. */
  43. public function skips_empty_rows_when_importing_on_each_row()
  44. {
  45. $import = new class implements OnEachRow, SkipsEmptyRows
  46. {
  47. use Importable;
  48. public $rows = 0;
  49. /**
  50. * @param Row $row
  51. */
  52. public function onRow(Row $row)
  53. {
  54. Assert::assertFalse($row->isEmpty());
  55. $this->rows++;
  56. }
  57. };
  58. $import->import('import-empty-rows.xlsx');
  59. $this->assertEquals(3, $import->rows);
  60. }
  61. /**
  62. * @test
  63. */
  64. public function skips_empty_rows_when_importing_to_model()
  65. {
  66. $import = new class implements ToModel, SkipsEmptyRows
  67. {
  68. use Importable;
  69. public $rows = 0;
  70. /**
  71. * @param array $row
  72. * @return Model|Model[]|null
  73. */
  74. public function model(array $row)
  75. {
  76. $this->rows++;
  77. return null;
  78. }
  79. };
  80. $import->import('import-empty-rows.xlsx');
  81. $this->assertEquals(3, $import->rows);
  82. }
  83. /**
  84. * @test
  85. */
  86. public function custom_skips_rows_when_importing_to_collection()
  87. {
  88. $import = new class implements SkipsEmptyRows, ToCollection
  89. {
  90. use Importable;
  91. public $called = false;
  92. /**
  93. * @param Collection $collection
  94. */
  95. public function collection(Collection $collection)
  96. {
  97. $this->called = true;
  98. Assert::assertEquals([
  99. ['Test1', 'Test2'],
  100. ['Test3', 'Test4'],
  101. ], $collection->toArray());
  102. }
  103. public function isEmptyWhen(array $row)
  104. {
  105. return $row[0] == 'Test5' && $row[1] == 'Test6';
  106. }
  107. };
  108. $import->import('import-empty-rows.xlsx');
  109. $this->assertTrue($import->called);
  110. }
  111. /**
  112. * @test
  113. */
  114. public function custom_skips_rows_when_importing_to_model()
  115. {
  116. $import = new class implements SkipsEmptyRows, ToModel
  117. {
  118. use Importable;
  119. public $called = false;
  120. /**
  121. * @param array $row
  122. */
  123. public function model(array $row)
  124. {
  125. Assert::assertEquals('Not empty', $row[0]);
  126. }
  127. public function isEmptyWhen(array $row): bool
  128. {
  129. $this->called = true;
  130. return $row[0] === 'Empty';
  131. }
  132. };
  133. $import->import('skip-empty-rows-with-is-empty-when.xlsx');
  134. $this->assertTrue($import->called);
  135. }
  136. /**
  137. * @test
  138. */
  139. public function custom_skips_rows_when_using_oneachrow()
  140. {
  141. $import = new class implements SkipsEmptyRows, OnEachRow
  142. {
  143. use Importable;
  144. public $called = false;
  145. /**
  146. * @param array $row
  147. */
  148. public function onRow(Row $row)
  149. {
  150. Assert::assertEquals('Not empty', $row[0]);
  151. }
  152. public function isEmptyWhen(array $row): bool
  153. {
  154. $this->called = true;
  155. return $row[0] === 'Empty';
  156. }
  157. };
  158. $import->import('skip-empty-rows-with-is-empty-when.xlsx');
  159. $this->assertTrue($import->called);
  160. }
  161. }