SkipsOnFailureTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Validation\Rule;
  6. use Maatwebsite\Excel\Concerns\Importable;
  7. use Maatwebsite\Excel\Concerns\OnEachRow;
  8. use Maatwebsite\Excel\Concerns\SkipsFailures;
  9. use Maatwebsite\Excel\Concerns\SkipsOnFailure;
  10. use Maatwebsite\Excel\Concerns\ToCollection;
  11. use Maatwebsite\Excel\Concerns\ToModel;
  12. use Maatwebsite\Excel\Concerns\WithBatchInserts;
  13. use Maatwebsite\Excel\Concerns\WithValidation;
  14. use Maatwebsite\Excel\Row;
  15. use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
  16. use Maatwebsite\Excel\Tests\TestCase;
  17. use Maatwebsite\Excel\Validators\Failure;
  18. use PHPUnit\Framework\Assert;
  19. class SkipsOnFailureTest extends TestCase
  20. {
  21. /**
  22. * Setup the test environment.
  23. */
  24. protected function setUp(): void
  25. {
  26. parent::setUp();
  27. $this->loadLaravelMigrations(['--database' => 'testing']);
  28. }
  29. /**
  30. * @test
  31. */
  32. public function can_skip_on_error()
  33. {
  34. $import = new class implements ToModel, WithValidation, SkipsOnFailure
  35. {
  36. use Importable;
  37. public $failures = 0;
  38. /**
  39. * @param array $row
  40. * @return Model|null
  41. */
  42. public function model(array $row)
  43. {
  44. return new User([
  45. 'name' => $row[0],
  46. 'email' => $row[1],
  47. 'password' => 'secret',
  48. ]);
  49. }
  50. /**
  51. * @return array
  52. */
  53. public function rules(): array
  54. {
  55. return [
  56. '1' => Rule::in(['patrick@maatwebsite.nl']),
  57. ];
  58. }
  59. /**
  60. * @param Failure[] $failures
  61. */
  62. public function onFailure(Failure ...$failures)
  63. {
  64. $failure = $failures[0];
  65. Assert::assertEquals(2, $failure->row());
  66. Assert::assertEquals('1', $failure->attribute());
  67. Assert::assertEquals(['The selected 1 is invalid.'], $failure->errors());
  68. Assert::assertEquals(['Taylor Otwell', 'taylor@laravel.com'], $failure->values());
  69. Assert::assertEquals(2, $failure->jsonSerialize()['row']);
  70. Assert::assertEquals('1', $failure->jsonSerialize()['attribute']);
  71. Assert::assertEquals(['The selected 1 is invalid.'], $failure->jsonSerialize()['errors']);
  72. Assert::assertEquals(['Taylor Otwell', 'taylor@laravel.com'], $failure->jsonSerialize()['values']);
  73. $this->failures += \count($failures);
  74. }
  75. };
  76. $import->import('import-users.xlsx');
  77. $this->assertEquals(1, $import->failures);
  78. // Shouldn't have rollbacked other imported rows.
  79. $this->assertDatabaseHas('users', [
  80. 'email' => 'patrick@maatwebsite.nl',
  81. ]);
  82. // Should have skipped inserting
  83. $this->assertDatabaseMissing('users', [
  84. 'email' => 'taylor@laravel.com',
  85. ]);
  86. }
  87. /**
  88. * @test
  89. */
  90. public function skips_only_failed_rows_in_batch()
  91. {
  92. $import = new class implements ToModel, WithValidation, WithBatchInserts, SkipsOnFailure
  93. {
  94. use Importable;
  95. public $failures = 0;
  96. /**
  97. * @param array $row
  98. * @return Model|null
  99. */
  100. public function model(array $row)
  101. {
  102. return new User([
  103. 'name' => $row[0],
  104. 'email' => $row[1],
  105. 'password' => 'secret',
  106. ]);
  107. }
  108. /**
  109. * @return array
  110. */
  111. public function rules(): array
  112. {
  113. return [
  114. '1' => Rule::in(['patrick@maatwebsite.nl']),
  115. ];
  116. }
  117. /**
  118. * @param Failure[] $failures
  119. */
  120. public function onFailure(Failure ...$failures)
  121. {
  122. $failure = $failures[0];
  123. Assert::assertEquals(2, $failure->row());
  124. Assert::assertEquals('1', $failure->attribute());
  125. Assert::assertEquals(['The selected 1 is invalid.'], $failure->errors());
  126. $this->failures += \count($failures);
  127. }
  128. /**
  129. * @return int
  130. */
  131. public function batchSize(): int
  132. {
  133. return 100;
  134. }
  135. };
  136. $import->import('import-users.xlsx');
  137. $this->assertEquals(1, $import->failures);
  138. // Shouldn't have rollbacked/skipped the rest of the batch.
  139. $this->assertDatabaseHas('users', [
  140. 'email' => 'patrick@maatwebsite.nl',
  141. ]);
  142. // Should have skipped inserting
  143. $this->assertDatabaseMissing('users', [
  144. 'email' => 'taylor@laravel.com',
  145. ]);
  146. }
  147. /**
  148. * @test
  149. */
  150. public function can_skip_failures_and_collect_all_failures_at_the_end()
  151. {
  152. $import = new class implements ToModel, WithValidation, SkipsOnFailure
  153. {
  154. use Importable, SkipsFailures;
  155. /**
  156. * @param array $row
  157. * @return Model|null
  158. */
  159. public function model(array $row)
  160. {
  161. return new User([
  162. 'name' => $row[0],
  163. 'email' => $row[1],
  164. 'password' => 'secret',
  165. ]);
  166. }
  167. /**
  168. * @return array
  169. */
  170. public function rules(): array
  171. {
  172. return [
  173. '1' => Rule::in(['patrick@maatwebsite.nl']),
  174. ];
  175. }
  176. };
  177. $import->import('import-users.xlsx');
  178. $this->assertCount(1, $import->failures());
  179. /** @var Failure $failure */
  180. $failure = $import->failures()->first();
  181. $this->assertEquals(2, $failure->row());
  182. $this->assertEquals('1', $failure->attribute());
  183. $this->assertEquals(['The selected 1 is invalid.'], $failure->errors());
  184. // Shouldn't have rollbacked other imported rows.
  185. $this->assertDatabaseHas('users', [
  186. 'email' => 'patrick@maatwebsite.nl',
  187. ]);
  188. // Should have skipped inserting
  189. $this->assertDatabaseMissing('users', [
  190. 'email' => 'taylor@laravel.com',
  191. ]);
  192. }
  193. /**
  194. * @test
  195. */
  196. public function can_validate_using_oneachrow_and_skipsonfailure()
  197. {
  198. $import = new class implements OnEachRow, WithValidation, SkipsOnFailure
  199. {
  200. use Importable, SkipsFailures;
  201. /**
  202. * @param Row $row
  203. * @return Model|null
  204. */
  205. public function onRow(Row $row)
  206. {
  207. $row = $row->toArray();
  208. return User::create([
  209. 'name' => $row[0],
  210. 'email' => $row[1],
  211. 'password' => 'secret',
  212. ]);
  213. }
  214. /**
  215. * @return array
  216. */
  217. public function rules(): array
  218. {
  219. return [
  220. '1' => Rule::in(['patrick@maatwebsite.nl']),
  221. ];
  222. }
  223. };
  224. $this->assertEmpty(User::all());
  225. $import->import('import-users.xlsx');
  226. $this->assertCount(1, $import->failures());
  227. // Shouldn't have rollbacked other imported rows.
  228. $this->assertDatabaseHas('users', [
  229. 'email' => 'patrick@maatwebsite.nl',
  230. ]);
  231. // Should have skipped inserting
  232. $this->assertDatabaseMissing('users', [
  233. 'email' => 'taylor@laravel.com',
  234. ]);
  235. }
  236. /**
  237. * @test
  238. */
  239. public function can_validate_using_tocollection_and_skipsonfailure()
  240. {
  241. $import = new class implements ToCollection, WithValidation, SkipsOnFailure
  242. {
  243. use Importable, SkipsFailures;
  244. /**
  245. * @param Row $row
  246. * @return Model|null
  247. */
  248. public function collection(Collection $rows)
  249. {
  250. $rows = $rows->each(function ($row) {
  251. return User::create([
  252. 'name' => $row[0],
  253. 'email' => $row[1],
  254. 'password' => 'secret',
  255. ]);
  256. });
  257. }
  258. /**
  259. * @return array
  260. */
  261. public function rules(): array
  262. {
  263. return [
  264. '1' => Rule::in(['patrick@maatwebsite.nl']),
  265. ];
  266. }
  267. };
  268. $this->assertEmpty(User::all());
  269. $import->import('import-users.xlsx');
  270. $this->assertCount(1, $import->failures());
  271. // Shouldn't have rollbacked other imported rows.
  272. $this->assertDatabaseHas('users', [
  273. 'email' => 'patrick@maatwebsite.nl',
  274. ]);
  275. // Should have skipped inserting
  276. $this->assertDatabaseMissing('users', [
  277. 'email' => 'taylor@laravel.com',
  278. ]);
  279. }
  280. }