WithBatchInsertsTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use Maatwebsite\Excel\Concerns\Importable;
  6. use Maatwebsite\Excel\Concerns\ToModel;
  7. use Maatwebsite\Excel\Concerns\WithBatchInserts;
  8. use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group;
  9. use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
  10. use Maatwebsite\Excel\Tests\TestCase;
  11. class WithBatchInsertsTest extends TestCase
  12. {
  13. /**
  14. * Setup the test environment.
  15. */
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->loadLaravelMigrations(['--database' => 'testing']);
  20. $this->loadMigrationsFrom(dirname(__DIR__) . '/Data/Stubs/Database/Migrations');
  21. }
  22. /**
  23. * @test
  24. */
  25. public function can_import_to_model_in_batches()
  26. {
  27. DB::connection()->enableQueryLog();
  28. $import = new class implements ToModel, WithBatchInserts
  29. {
  30. use Importable;
  31. /**
  32. * @param array $row
  33. * @return Model|null
  34. */
  35. public function model(array $row)
  36. {
  37. return new User([
  38. 'name' => $row[0],
  39. 'email' => $row[1],
  40. 'password' => 'secret',
  41. ]);
  42. }
  43. /**
  44. * @return int
  45. */
  46. public function batchSize(): int
  47. {
  48. return 2;
  49. }
  50. };
  51. $import->import('import-users.xlsx');
  52. $this->assertCount(1, DB::getQueryLog());
  53. DB::connection()->disableQueryLog();
  54. $this->assertDatabaseHas('users', [
  55. 'name' => 'Patrick Brouwers',
  56. 'email' => 'patrick@maatwebsite.nl',
  57. ]);
  58. $this->assertDatabaseHas('users', [
  59. 'name' => 'Taylor Otwell',
  60. 'email' => 'taylor@laravel.com',
  61. ]);
  62. }
  63. /**
  64. * @test
  65. */
  66. public function can_import_to_model_in_batches_bigger_file()
  67. {
  68. DB::connection()->enableQueryLog();
  69. $import = new class implements ToModel, WithBatchInserts
  70. {
  71. use Importable;
  72. /**
  73. * @param array $row
  74. * @return Model|null
  75. */
  76. public function model(array $row)
  77. {
  78. return new Group([
  79. 'name' => $row[0],
  80. ]);
  81. }
  82. /**
  83. * @return int
  84. */
  85. public function batchSize(): int
  86. {
  87. return 1000;
  88. }
  89. };
  90. $import->import('import-batches.xlsx');
  91. $this->assertCount(5000 / $import->batchSize(), DB::getQueryLog());
  92. DB::connection()->disableQueryLog();
  93. }
  94. /**
  95. * @test
  96. */
  97. public function can_import_multiple_different_types_of_models_in_single_to_model()
  98. {
  99. DB::connection()->enableQueryLog();
  100. $import = new class implements ToModel, WithBatchInserts
  101. {
  102. use Importable;
  103. /**
  104. * @param array $row
  105. * @return Model|Model[]|null
  106. */
  107. public function model(array $row)
  108. {
  109. $user = new User([
  110. 'name' => $row[0],
  111. 'email' => $row[1],
  112. 'password' => 'secret',
  113. ]);
  114. $group = new Group([
  115. 'name' => $row[0],
  116. ]);
  117. return [$user, $group];
  118. }
  119. /**
  120. * @return int
  121. */
  122. public function batchSize(): int
  123. {
  124. return 2;
  125. }
  126. };
  127. $import->import('import-users.xlsx');
  128. // Expected 2 batch queries, 1 for users, 1 for groups
  129. $this->assertCount(2, DB::getQueryLog());
  130. $this->assertEquals(2, User::count());
  131. $this->assertEquals(2, Group::count());
  132. DB::connection()->disableQueryLog();
  133. }
  134. /**
  135. * @test
  136. */
  137. public function has_timestamps_when_imported_in_batches()
  138. {
  139. $import = new class implements ToModel, WithBatchInserts
  140. {
  141. use Importable;
  142. /**
  143. * @param array $row
  144. * @return Model|Model[]|null
  145. */
  146. public function model(array $row)
  147. {
  148. return new User([
  149. 'name' => $row[0],
  150. 'email' => $row[1],
  151. 'password' => 'secret',
  152. ]);
  153. }
  154. /**
  155. * @return int
  156. */
  157. public function batchSize(): int
  158. {
  159. return 2;
  160. }
  161. };
  162. $import->import('import-users.xlsx');
  163. $user = User::first();
  164. $this->assertNotNull($user->created_at);
  165. $this->assertNotNull($user->updated_at);
  166. }
  167. }