From 2577907aad28f8db58fba158eaf39ab5ebaff5e1 Mon Sep 17 00:00:00 2001 From: Matias Hernan Lauriti Date: Fri, 10 Dec 2021 01:09:28 -0300 Subject: [PATCH 1/3] Fixed BelongsToMany UpdateOrCreate Create operation --- .../Eloquent/Relations/BelongsToMany.php | 2 +- .../DatabaseEloquentBelongsToManyTest.php | 140 ++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 tests/Database/DatabaseEloquentBelongsToManyTest.php diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 5c93b8f8c111..465e761993a0 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -634,7 +634,7 @@ public function firstOrCreate(array $attributes, array $joining = [], $touch = t public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true) { if (is_null($instance = $this->where($attributes)->first())) { - return $this->create($values, $joining, $touch); + return $this->create(array_merge($attributes, $values), $joining, $touch); } $instance->fill($values); diff --git a/tests/Database/DatabaseEloquentBelongsToManyTest.php b/tests/Database/DatabaseEloquentBelongsToManyTest.php new file mode 100644 index 000000000000..4f31a50a1caf --- /dev/null +++ b/tests/Database/DatabaseEloquentBelongsToManyTest.php @@ -0,0 +1,140 @@ +addConnection([ + 'driver' => 'sqlite', + 'database' => ':memory:', + ]); + + $db->bootEloquent(); + $db->setAsGlobal(); + + $this->createSchema(); + } + + /** + * Setup the database schema. + * + * @return void + */ + public function createSchema() + { + $this->schema()->create('users', function ($table) { + $table->id('id'); + }); + + $this->schema()->create('articles', function ($table) { + $table->id('id'); + + $table->string('title'); + $table->string('description'); + }); + + $this->schema()->create('article_user', function ($table) { + $table->foreignId('article_id')->references('id')->on('articles'); + $table->foreignId('user_id')->references('id')->on('users'); + }); + } + + /** + * Tear down the database schema. + * + * @return void + */ + protected function tearDown(): void + { + $this->schema()->drop('users'); + $this->schema()->drop('articles'); + $this->schema()->drop('article_user'); + } + + public function testCreateWithDesiredAttributesUsingUpdateOrCreate() + { + /** @var BelongsToManySyncTestTestUser $user */ + $user = BelongsToManySyncTestTestUser::create(); + + $user->articles()->updateOrCreate( + ['title' => 'Fixing UpdateOrCreate'], + ['description' => 'Fixed'] + ); + + $article = $user->articles()->first(); + + $this->assertSame($article->description, 'Fixed'); + } + + public function testUpdateWithDesiredAttributesUsingUpdateOrCreate() + { + /** @var BelongsToManySyncTestTestUser $user */ + $user = BelongsToManySyncTestTestUser::create(); + $user->articles()->create([ + 'title' => $title = 'Fixing UpdateOrCreate', + 'description' => 'Not fixed', + ]); + + $user->articles()->updateOrCreate( + ['title' => $title], + ['description' => 'Fixed'] + ); + + $article = $user->articles()->first(); + + $this->assertSame($article->description, 'Fixed'); + } + + /** + * Get a database connection instance. + * + * @return \Illuminate\Database\ConnectionInterface + */ + protected function connection() + { + return Model::getConnectionResolver()->connection(); + } + + /** + * Get a schema builder instance. + * + * @return \Illuminate\Database\Schema\Builder + */ + protected function schema() + { + return $this->connection()->getSchemaBuilder(); + } +} + +class BelongsToManySyncTestTestUser extends Model +{ + protected $table = 'users'; + + protected $fillable = ['email']; + + public $timestamps = false; + + public function articles() + { + return $this->belongsToMany(BelongsToManySyncTestTestArticle::class, 'article_user', 'user_id', 'article_id'); + } +} + +class BelongsToManySyncTestTestArticle extends Model +{ + protected $table = 'articles'; + + public $timestamps = false; + + protected $fillable = ['title', 'description']; +} From ce1521d39c4c23100f8a47c647c53ece7b7f2b89 Mon Sep 17 00:00:00 2001 From: Matias Hernan Lauriti Date: Fri, 10 Dec 2021 01:21:32 -0300 Subject: [PATCH 2/3] Removed unused uses --- tests/Database/DatabaseEloquentBelongsToManyTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Database/DatabaseEloquentBelongsToManyTest.php b/tests/Database/DatabaseEloquentBelongsToManyTest.php index 4f31a50a1caf..3d3d4cf22cb5 100644 --- a/tests/Database/DatabaseEloquentBelongsToManyTest.php +++ b/tests/Database/DatabaseEloquentBelongsToManyTest.php @@ -4,8 +4,6 @@ use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Eloquent\Model; -use Illuminate\Tests\Database\EloquentRelationshipsTest\FakeRelationship; -use Illuminate\Tests\Database\EloquentRelationshipsTest\Post; use PHPUnit\Framework\TestCase; class DatabaseEloquentBelongsToManyTest extends TestCase From 2d3e43eb9b2dadefc0d438c89013b55d7b9bebc3 Mon Sep 17 00:00:00 2001 From: Matias Hernan Lauriti Date: Fri, 10 Dec 2021 01:28:43 -0300 Subject: [PATCH 3/3] Updated classes names --- .../Database/DatabaseEloquentBelongsToManyTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Database/DatabaseEloquentBelongsToManyTest.php b/tests/Database/DatabaseEloquentBelongsToManyTest.php index 3d3d4cf22cb5..d2a0ae14f88f 100644 --- a/tests/Database/DatabaseEloquentBelongsToManyTest.php +++ b/tests/Database/DatabaseEloquentBelongsToManyTest.php @@ -61,8 +61,8 @@ protected function tearDown(): void public function testCreateWithDesiredAttributesUsingUpdateOrCreate() { - /** @var BelongsToManySyncTestTestUser $user */ - $user = BelongsToManySyncTestTestUser::create(); + /** @var FakeUpdateOrCreateUser $user */ + $user = FakeUpdateOrCreateUser::create(); $user->articles()->updateOrCreate( ['title' => 'Fixing UpdateOrCreate'], @@ -76,8 +76,8 @@ public function testCreateWithDesiredAttributesUsingUpdateOrCreate() public function testUpdateWithDesiredAttributesUsingUpdateOrCreate() { - /** @var BelongsToManySyncTestTestUser $user */ - $user = BelongsToManySyncTestTestUser::create(); + /** @var FakeUpdateOrCreateUser $user */ + $user = FakeUpdateOrCreateUser::create(); $user->articles()->create([ 'title' => $title = 'Fixing UpdateOrCreate', 'description' => 'Not fixed', @@ -114,7 +114,7 @@ protected function schema() } } -class BelongsToManySyncTestTestUser extends Model +class FakeUpdateOrCreateUser extends Model { protected $table = 'users'; @@ -124,11 +124,11 @@ class BelongsToManySyncTestTestUser extends Model public function articles() { - return $this->belongsToMany(BelongsToManySyncTestTestArticle::class, 'article_user', 'user_id', 'article_id'); + return $this->belongsToMany(FakeUpdateOrCreateArticle::class, 'article_user', 'user_id', 'article_id'); } } -class BelongsToManySyncTestTestArticle extends Model +class FakeUpdateOrCreateArticle extends Model { protected $table = 'articles';