From b817a02eea4043c33781fd5dcc3ea6bac5bdfa62 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Tue, 15 Feb 2022 12:38:28 +0300 Subject: [PATCH 1/4] Fix loadAggregate not correctly applying casts --- src/Illuminate/Database/Eloquent/Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index ff9b2747fe3e..ee583f23e551 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -92,7 +92,7 @@ public function loadAggregate($relations, $column, $function = null) $this->each(function ($model) use ($models, $attributes) { $extraAttributes = Arr::only($models->get($model->getKey())->getAttributes(), $attributes); - $model->forceFill($extraAttributes)->syncOriginalAttributes($attributes); + $model->forceFill($extraAttributes)->syncOriginalAttributes($attributes)->mergeCasts($models->get($model->getKey())->getCasts()); }); return $this; From 8f2e70ae4a2d2723bc2355285eb4b080f0cac77c Mon Sep 17 00:00:00 2001 From: Ahmed Date: Wed, 16 Feb 2022 08:06:27 +0300 Subject: [PATCH 2/4] Added tests to "Fix loadAggregate not correctly applying casts" --- .../DatabaseEloquentCollectionTest.php | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/tests/Database/DatabaseEloquentCollectionTest.php b/tests/Database/DatabaseEloquentCollectionTest.php index 7823e45e177f..590778ecf771 100755 --- a/tests/Database/DatabaseEloquentCollectionTest.php +++ b/tests/Database/DatabaseEloquentCollectionTest.php @@ -2,9 +2,11 @@ namespace Illuminate\Tests\Database; +use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Support\Collection as BaseCollection; use LogicException; use Mockery as m; @@ -13,8 +15,53 @@ class DatabaseEloquentCollectionTest extends TestCase { + /** + * Setup the database schema. + * + * @return void + */ + protected function setUp(): void + { + $db = new DB; + + $db->addConnection([ + 'driver' => 'sqlite', + 'database' => ':memory:', + ]); + + $db->bootEloquent(); + $db->setAsGlobal(); + + $this->createSchema(); + } + + protected function createSchema() + { + + $this->schema()->create('users', function ($table) { + $table->increments('id'); + $table->string('email')->unique(); + }); + + $this->schema()->create('articles', function ($table) { + $table->increments('id'); + $table->integer('user_id'); + $table->string('title'); + }); + + $this->schema()->create('comments', function ($table) { + $table->increments('id'); + $table->integer('article_id'); + $table->string('content'); + }); + + } + protected function tearDown(): void { + $this->schema()->drop('users'); + $this->schema()->drop('articles'); + $this->schema()->drop('comments'); m::close(); } @@ -536,6 +583,56 @@ public function testConvertingEmptyCollectionToQueryThrowsException() $c = new Collection; $c->toQuery(); } + + public function testLoadExistsShouldCastBool() + { + $this->seedData(); + $user = EloquentTestUserModel::with('articles')->first(); + $user->articles->loadExists("comments"); + $commentsExists = $user->articles->pluck('comments_exists')->toArray(); + $this->assertContainsOnly('bool',$commentsExists); + } + + /** + * Helpers... + */ + protected function seedData() + { + $user = EloquentTestUserModel::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']); + + EloquentTestArticleModel::query()->insert([ + ['user_id' => 1, 'title' => 'Another title'], + ['user_id' => 1, 'title' => 'Another title'], + ['user_id' => 1, 'title' => 'Another title'], + ]); + + EloquentTestCommentModel::query()->insert([ + ['article_id' => 1, 'content' => 'Another comment'], + ['article_id' => 2, 'content' => 'Another comment'], + ]); + + } + + /** + * Get a database connection instance. + * + * @return \Illuminate\Database\ConnectionInterface + */ + protected function connection() + { + return Eloquent::getConnectionResolver()->connection(); + } + + /** + * Get a schema builder instance. + * + * @return \Illuminate\Database\Schema\Builder + */ + protected function schema() + { + return $this->connection()->getSchemaBuilder(); + } + } class TestEloquentCollectionModel extends Model @@ -548,3 +645,34 @@ public function getTestAttribute() return 'test'; } } + +class EloquentTestUserModel extends Model +{ + protected $table = 'users'; + protected $guarded = []; + public $timestamps = false; + + public function articles() + { + return $this->hasMany(EloquentTestArticleModel::class,'user_id'); + } +} + +class EloquentTestArticleModel extends Model +{ + protected $table = 'articles'; + protected $guarded = []; + public $timestamps = false; + + public function comments() + { + return $this->hasMany(EloquentTestCommentModel::class,'article_id'); + } +} + +class EloquentTestCommentModel extends Model +{ + protected $table = 'comments'; + protected $guarded = []; + public $timestamps = false; +} From 24f28c80e9bdee177c23d658a0cf4b730f99e7a3 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Wed, 16 Feb 2022 08:48:58 +0300 Subject: [PATCH 3/4] Style fix --- .../DatabaseEloquentCollectionTest.php | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/tests/Database/DatabaseEloquentCollectionTest.php b/tests/Database/DatabaseEloquentCollectionTest.php index 590778ecf771..b242dcb37447 100755 --- a/tests/Database/DatabaseEloquentCollectionTest.php +++ b/tests/Database/DatabaseEloquentCollectionTest.php @@ -37,24 +37,22 @@ protected function setUp(): void protected function createSchema() { + $this->schema()->create('users', function ($table) { + $table->increments('id'); + $table->string('email')->unique(); + }); - $this->schema()->create('users', function ($table) { - $table->increments('id'); - $table->string('email')->unique(); - }); - - $this->schema()->create('articles', function ($table) { - $table->increments('id'); - $table->integer('user_id'); - $table->string('title'); - }); - - $this->schema()->create('comments', function ($table) { - $table->increments('id'); - $table->integer('article_id'); - $table->string('content'); - }); + $this->schema()->create('articles', function ($table) { + $table->increments('id'); + $table->integer('user_id'); + $table->string('title'); + }); + $this->schema()->create('comments', function ($table) { + $table->increments('id'); + $table->integer('article_id'); + $table->string('content'); + }); } protected function tearDown(): void @@ -588,9 +586,9 @@ public function testLoadExistsShouldCastBool() { $this->seedData(); $user = EloquentTestUserModel::with('articles')->first(); - $user->articles->loadExists("comments"); + $user->articles->loadExists('comments'); $commentsExists = $user->articles->pluck('comments_exists')->toArray(); - $this->assertContainsOnly('bool',$commentsExists); + $this->assertContainsOnly('bool', $commentsExists); } /** @@ -610,7 +608,6 @@ protected function seedData() ['article_id' => 1, 'content' => 'Another comment'], ['article_id' => 2, 'content' => 'Another comment'], ]); - } /** @@ -632,7 +629,6 @@ protected function schema() { return $this->connection()->getSchemaBuilder(); } - } class TestEloquentCollectionModel extends Model @@ -654,7 +650,7 @@ class EloquentTestUserModel extends Model public function articles() { - return $this->hasMany(EloquentTestArticleModel::class,'user_id'); + return $this->hasMany(EloquentTestArticleModel::class, 'user_id'); } } @@ -666,7 +662,7 @@ class EloquentTestArticleModel extends Model public function comments() { - return $this->hasMany(EloquentTestCommentModel::class,'article_id'); + return $this->hasMany(EloquentTestCommentModel::class, 'article_id'); } } From 5373e4fd5cf43ea8d239bb1f4bd1ef2ce5c6ad1b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 16 Feb 2022 14:52:09 -0600 Subject: [PATCH 4/4] Update Collection.php --- src/Illuminate/Database/Eloquent/Collection.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index ee583f23e551..cdd972c37fc7 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -92,7 +92,9 @@ public function loadAggregate($relations, $column, $function = null) $this->each(function ($model) use ($models, $attributes) { $extraAttributes = Arr::only($models->get($model->getKey())->getAttributes(), $attributes); - $model->forceFill($extraAttributes)->syncOriginalAttributes($attributes)->mergeCasts($models->get($model->getKey())->getCasts()); + $model->forceFill($extraAttributes) + ->syncOriginalAttributes($attributes) + ->mergeCasts($models->get($model->getKey())->getCasts()); }); return $this;