ToModelTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Faker\Factory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. use Maatwebsite\Excel\Concerns\Importable;
  7. use Maatwebsite\Excel\Concerns\ToModel;
  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 ToModelTest 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_each_row_to_model()
  26. {
  27. DB::connection()->enableQueryLog();
  28. $import = new class implements ToModel
  29. {
  30. use Importable;
  31. /**
  32. * @param array $row
  33. * @return Model|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. $import->import('import-users.xlsx');
  45. $this->assertCount(2, DB::getQueryLog());
  46. DB::connection()->disableQueryLog();
  47. $this->assertDatabaseHas('users', [
  48. 'name' => 'Patrick Brouwers',
  49. 'email' => 'patrick@maatwebsite.nl',
  50. ]);
  51. $this->assertDatabaseHas('users', [
  52. 'name' => 'Taylor Otwell',
  53. 'email' => 'taylor@laravel.com',
  54. ]);
  55. }
  56. /**
  57. * @test
  58. */
  59. public function has_timestamps_when_imported_single_model()
  60. {
  61. $import = new class implements ToModel
  62. {
  63. use Importable;
  64. /**
  65. * @param array $row
  66. * @return Model|Model[]|null
  67. */
  68. public function model(array $row)
  69. {
  70. return new User([
  71. 'name' => $row[0],
  72. 'email' => $row[1],
  73. 'password' => 'secret',
  74. ]);
  75. }
  76. };
  77. $import->import('import-users.xlsx');
  78. $user = User::first();
  79. $this->assertNotNull($user->created_at);
  80. $this->assertNotNull($user->updated_at);
  81. }
  82. /**
  83. * @test
  84. */
  85. public function can_import_multiple_models_in_single_to_model()
  86. {
  87. DB::connection()->enableQueryLog();
  88. $import = new class implements ToModel
  89. {
  90. use Importable;
  91. /**
  92. * @param array $row
  93. * @return Model|Model[]|null
  94. */
  95. public function model(array $row)
  96. {
  97. $user1 = new User([
  98. 'name' => $row[0],
  99. 'email' => $row[1],
  100. 'password' => 'secret',
  101. ]);
  102. $faker = Factory::create();
  103. $user2 = new User([
  104. 'name' => $faker->name,
  105. 'email' => $faker->email,
  106. 'password' => 'secret',
  107. ]);
  108. return [$user1, $user2];
  109. }
  110. };
  111. $import->import('import-users.xlsx');
  112. $this->assertCount(4, DB::getQueryLog());
  113. DB::connection()->disableQueryLog();
  114. }
  115. /**
  116. * @test
  117. */
  118. public function can_import_multiple_different_types_of_models_in_single_to_model()
  119. {
  120. DB::connection()->enableQueryLog();
  121. $import = new class implements ToModel
  122. {
  123. use Importable;
  124. /**
  125. * @param array $row
  126. * @return Model|Model[]|null
  127. */
  128. public function model(array $row)
  129. {
  130. $user = new User([
  131. 'name' => $row[0],
  132. 'email' => $row[1],
  133. 'password' => 'secret',
  134. ]);
  135. $group = new Group([
  136. 'name' => $row[0],
  137. ]);
  138. return [$user, $group];
  139. }
  140. };
  141. $import->import('import-users.xlsx');
  142. $this->assertCount(4, DB::getQueryLog());
  143. $this->assertEquals(2, User::count());
  144. $this->assertEquals(2, Group::count());
  145. DB::connection()->disableQueryLog();
  146. }
  147. }