From dd10128652c18adff21c629bcd432a130646ed6d Mon Sep 17 00:00:00 2001 From: KSneijders Date: Thu, 5 Jun 2025 09:13:46 +0200 Subject: [PATCH 1/5] Adds new GroupedUser relation with mandatory relation to groups + tests --- tests/Concerns/ToModelTest.php | 105 ++++++++++++------ tests/Data/Stubs/Database/GroupedUser.php | 20 ++++ ...0_00_000003_create_grouped_users_table.php | 32 ++++++ 3 files changed, 123 insertions(+), 34 deletions(-) create mode 100644 tests/Data/Stubs/Database/GroupedUser.php create mode 100644 tests/Data/Stubs/Database/Migrations/0000_00_00_000003_create_grouped_users_table.php diff --git a/tests/Concerns/ToModelTest.php b/tests/Concerns/ToModelTest.php index e2f40fcfc..41686f8bb 100644 --- a/tests/Concerns/ToModelTest.php +++ b/tests/Concerns/ToModelTest.php @@ -10,6 +10,7 @@ use Maatwebsite\Excel\Concerns\PersistRelations; use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group; +use Maatwebsite\Excel\Tests\Data\Stubs\Database\GroupedUser; use Maatwebsite\Excel\Tests\Data\Stubs\Database\User; use Maatwebsite\Excel\Tests\TestCase; @@ -30,19 +31,18 @@ public function test_can_import_each_row_to_model() { DB::connection()->enableQueryLog(); - $import = new class implements ToModel - { + $import = new class implements ToModel { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { return new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); } @@ -54,31 +54,30 @@ public function model(array $row) DB::connection()->disableQueryLog(); $this->assertDatabaseHas('users', [ - 'name' => 'Patrick Brouwers', + 'name' => 'Patrick Brouwers', 'email' => 'patrick@maatwebsite.nl', ]); $this->assertDatabaseHas('users', [ - 'name' => 'Taylor Otwell', + 'name' => 'Taylor Otwell', 'email' => 'taylor@laravel.com', ]); } public function test_has_timestamps_when_imported_single_model() { - $import = new class implements ToModel - { + $import = new class implements ToModel { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { return new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); } @@ -96,27 +95,26 @@ public function test_can_import_multiple_models_in_single_to_model() { DB::connection()->enableQueryLog(); - $import = new class implements ToModel - { + $import = new class implements ToModel { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { $user1 = new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); $faker = Factory::create(); $user2 = new User([ - 'name' => $faker->name, - 'email' => $faker->email, + 'name' => $faker->name, + 'email' => $faker->email, 'password' => 'secret', ]); @@ -134,19 +132,18 @@ public function test_can_import_multiple_different_types_of_models_in_single_to_ { DB::connection()->enableQueryLog(); - $import = new class implements ToModel - { + $import = new class implements ToModel { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { $user = new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); @@ -173,19 +170,18 @@ public function test_can_import_models_with_belongs_to_relations() DB::connection()->enableQueryLog(); - $import = new class implements ToModel, PersistRelations - { + $import = new class implements ToModel, PersistRelations { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { $user = new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); @@ -221,19 +217,18 @@ public function test_can_import_models_with_belongs_to_many_relations() DB::connection()->enableQueryLog(); - $import = new class implements ToModel, PersistRelations - { + $import = new class implements ToModel, PersistRelations { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { $user = new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); @@ -260,4 +255,46 @@ public function model(array $row) $this->assertEquals(2, Group::count()); DB::connection()->disableQueryLog(); } + + public function test_can_import_models_with_non_nullable_belongs_to_many_relations() + { + Group::query()->truncate(); + GroupedUser::query()->truncate(); + + DB::connection()->enableQueryLog(); + + $import = new class implements ToModel, PersistRelations { + use Importable; + + /** + * @param array $row + * @return GroupedUser + */ + public function model(array $row): GroupedUser + { + $groupedUser = new GroupedUser([ + 'name' => $row[0], + 'email' => $row[1], + 'password' => 'secret', + ]); + + $groupedUser->setRelation('group', new Group(['name' => $row[0]])); + + return $groupedUser; + } + }; + + $import->import('import-users.xlsx'); + + $this->assertCount(6, DB::getQueryLog()); + + $users = GroupedUser::all(); + $users->each(function (GroupedUser $groupedUser) { + $this->assertInstanceOf(Group::class, $groupedUser->group); + }); + + $this->assertCount(2, $users); + $this->assertEquals(2, Group::count()); + DB::connection()->disableQueryLog(); + } } diff --git a/tests/Data/Stubs/Database/GroupedUser.php b/tests/Data/Stubs/Database/GroupedUser.php new file mode 100644 index 000000000..c991bfb19 --- /dev/null +++ b/tests/Data/Stubs/Database/GroupedUser.php @@ -0,0 +1,20 @@ +belongsTo(Group::class); + } +} diff --git a/tests/Data/Stubs/Database/Migrations/0000_00_00_000003_create_grouped_users_table.php b/tests/Data/Stubs/Database/Migrations/0000_00_00_000003_create_grouped_users_table.php new file mode 100644 index 000000000..b20b79970 --- /dev/null +++ b/tests/Data/Stubs/Database/Migrations/0000_00_00_000003_create_grouped_users_table.php @@ -0,0 +1,32 @@ +increments('id'); + $table->string('name'); + $table->string('email'); + $table->string('password'); + $table->unsignedInteger('group_id')->index(); + + $table->datetimes(); + }); + } + + /** + * Reverse the migrations. + */ + public function down() + { + Schema::dropIfExists('grouped_users'); + } +} From 8bfb5731e69af1fb1a97342aa599982e85375618 Mon Sep 17 00:00:00 2001 From: KSneijders Date: Thu, 5 Jun 2025 09:25:51 +0200 Subject: [PATCH 2/5] Fixes accidental style changes --- tests/Concerns/ToModelTest.php | 72 ++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/tests/Concerns/ToModelTest.php b/tests/Concerns/ToModelTest.php index 41686f8bb..0a4a05322 100644 --- a/tests/Concerns/ToModelTest.php +++ b/tests/Concerns/ToModelTest.php @@ -10,7 +10,6 @@ use Maatwebsite\Excel\Concerns\PersistRelations; use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group; -use Maatwebsite\Excel\Tests\Data\Stubs\Database\GroupedUser; use Maatwebsite\Excel\Tests\Data\Stubs\Database\User; use Maatwebsite\Excel\Tests\TestCase; @@ -31,18 +30,19 @@ public function test_can_import_each_row_to_model() { DB::connection()->enableQueryLog(); - $import = new class implements ToModel { + $import = new class implements ToModel + { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { return new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); } @@ -54,30 +54,31 @@ public function model(array $row) DB::connection()->disableQueryLog(); $this->assertDatabaseHas('users', [ - 'name' => 'Patrick Brouwers', + 'name' => 'Patrick Brouwers', 'email' => 'patrick@maatwebsite.nl', ]); $this->assertDatabaseHas('users', [ - 'name' => 'Taylor Otwell', + 'name' => 'Taylor Otwell', 'email' => 'taylor@laravel.com', ]); } public function test_has_timestamps_when_imported_single_model() { - $import = new class implements ToModel { + $import = new class implements ToModel + { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { return new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); } @@ -95,26 +96,27 @@ public function test_can_import_multiple_models_in_single_to_model() { DB::connection()->enableQueryLog(); - $import = new class implements ToModel { + $import = new class implements ToModel + { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { $user1 = new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); $faker = Factory::create(); $user2 = new User([ - 'name' => $faker->name, - 'email' => $faker->email, + 'name' => $faker->name, + 'email' => $faker->email, 'password' => 'secret', ]); @@ -132,18 +134,19 @@ public function test_can_import_multiple_different_types_of_models_in_single_to_ { DB::connection()->enableQueryLog(); - $import = new class implements ToModel { + $import = new class implements ToModel + { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { $user = new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); @@ -170,18 +173,19 @@ public function test_can_import_models_with_belongs_to_relations() DB::connection()->enableQueryLog(); - $import = new class implements ToModel, PersistRelations { + $import = new class implements ToModel, PersistRelations + { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { $user = new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); @@ -217,18 +221,19 @@ public function test_can_import_models_with_belongs_to_many_relations() DB::connection()->enableQueryLog(); - $import = new class implements ToModel, PersistRelations { + $import = new class implements ToModel, PersistRelations + { use Importable; /** - * @param array $row + * @param array $row * @return Model|Model[]|null */ public function model(array $row) { $user = new User([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); @@ -263,18 +268,19 @@ public function test_can_import_models_with_non_nullable_belongs_to_many_relatio DB::connection()->enableQueryLog(); - $import = new class implements ToModel, PersistRelations { + $import = new class implements ToModel, PersistRelations + { use Importable; /** - * @param array $row + * @param array $row * @return GroupedUser */ public function model(array $row): GroupedUser { $groupedUser = new GroupedUser([ - 'name' => $row[0], - 'email' => $row[1], + 'name' => $row[0], + 'email' => $row[1], 'password' => 'secret', ]); From ce7443b28c91bad125b542e95e372c5bf8d49ade Mon Sep 17 00:00:00 2001 From: KSneijders Date: Thu, 5 Jun 2025 09:31:02 +0200 Subject: [PATCH 3/5] Fixes migration function --- .../Migrations/0000_00_00_000003_create_grouped_users_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Data/Stubs/Database/Migrations/0000_00_00_000003_create_grouped_users_table.php b/tests/Data/Stubs/Database/Migrations/0000_00_00_000003_create_grouped_users_table.php index b20b79970..26ef5e6cb 100644 --- a/tests/Data/Stubs/Database/Migrations/0000_00_00_000003_create_grouped_users_table.php +++ b/tests/Data/Stubs/Database/Migrations/0000_00_00_000003_create_grouped_users_table.php @@ -18,7 +18,7 @@ public function up() $table->string('password'); $table->unsignedInteger('group_id')->index(); - $table->datetimes(); + $table->timestamps(); }); } From dc134d6b8f9ff4af05aba5b8a7f9c9f993f90497 Mon Sep 17 00:00:00 2001 From: KSneijders Date: Thu, 5 Jun 2025 09:54:12 +0200 Subject: [PATCH 4/5] Fixes missing import Copying over my changes to the fork was apparently very difficult --- tests/Concerns/ToModelTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Concerns/ToModelTest.php b/tests/Concerns/ToModelTest.php index 0a4a05322..45706c887 100644 --- a/tests/Concerns/ToModelTest.php +++ b/tests/Concerns/ToModelTest.php @@ -11,6 +11,7 @@ use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group; use Maatwebsite\Excel\Tests\Data\Stubs\Database\User; +use Maatwebsite\Excel\Tests\Data\Stubs\Database\GroupedUser; use Maatwebsite\Excel\Tests\TestCase; class ToModelTest extends TestCase From 2d395b3827cc0265a6b41399401706dd0accce35 Mon Sep 17 00:00:00 2001 From: KSneijders Date: Thu, 5 Jun 2025 09:55:11 +0200 Subject: [PATCH 5/5] Fixes incorrectly ordered imports --- tests/Concerns/ToModelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Concerns/ToModelTest.php b/tests/Concerns/ToModelTest.php index 45706c887..8487d9ee7 100644 --- a/tests/Concerns/ToModelTest.php +++ b/tests/Concerns/ToModelTest.php @@ -10,8 +10,8 @@ use Maatwebsite\Excel\Concerns\PersistRelations; use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group; -use Maatwebsite\Excel\Tests\Data\Stubs\Database\User; use Maatwebsite\Excel\Tests\Data\Stubs\Database\GroupedUser; +use Maatwebsite\Excel\Tests\Data\Stubs\Database\User; use Maatwebsite\Excel\Tests\TestCase; class ToModelTest extends TestCase