From ed4e14d63ab2699b477b2f83bc666f74c3e40cb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Thu, 17 Oct 2024 20:52:08 +0800 Subject: [PATCH 1/3] fixed `HasOneThrough` relationship on laravel-11's change. --- .gitignore | 3 +- .scrutinizer.yml | 2 +- src/Database/Eloquent/RelationMixin.php | 8 +-- tests/Features/HasInTest.php | 71 ++++++++++++++++++++++++- tests/Features/WithWhereHasInTest.php | 10 ++-- 5 files changed, 81 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 280d0d0..e5f2358 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ docs phpunit.xml testbench.yaml vendor -node_modules \ No newline at end of file +node_modules +.phpunit.cache diff --git a/.scrutinizer.yml b/.scrutinizer.yml index d182fad..3d7de21 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -17,7 +17,7 @@ build: image: default-bionic environment: php: - version: 8.1 + version: 8.2 tests: override: - command: './vendor/bin/pest' diff --git a/src/Database/Eloquent/RelationMixin.php b/src/Database/Eloquent/RelationMixin.php index 03cc03a..9baab34 100644 --- a/src/Database/Eloquent/RelationMixin.php +++ b/src/Database/Eloquent/RelationMixin.php @@ -6,9 +6,9 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; -use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOneOrMany; +use Illuminate\Database\Eloquent\Relations\HasOneOrManyThrough; use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphOneOrMany; use Illuminate\Database\Eloquent\Relations\MorphToMany; @@ -62,7 +62,7 @@ public function getRelationExistenceInQuery(): Closure return $relation($query, $parentQuery, $columns); }; - $hasManyThrough = function (Builder $query, Builder $parentQuery) use ($relation): Builder { + $hasOneOrManyThrough = function (Builder $query, Builder $parentQuery) use ($relation): Builder { $columns = $this->getQualifiedFirstKeyName(); if ($parentQuery->getQuery()->from === $query->getQuery()->from) { $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()); @@ -132,7 +132,7 @@ public function getRelationExistenceInQuery(): Closure $this instanceof BelongsTo => $belongsTo($query, $parentQuery), $this instanceof BelongsToMany => $belongsToMany($query, $parentQuery), $this instanceof HasOneOrMany => $hasOneOrMany($query, $parentQuery), - $this instanceof HasManyThrough => $hasManyThrough($query, $parentQuery), + $this instanceof HasOneOrManyThrough => $hasOneOrManyThrough($query, $parentQuery), default => throw new LogicException( sprintf('%s must be a relationship instance.', $this::class) ) @@ -145,7 +145,7 @@ public function getRelationWhereInKey(): Closure return fn (): string => match (true) { $this instanceof BelongsTo => $this->getQualifiedForeignKeyName(), $this instanceof HasOneOrMany, $this instanceof BelongsToMany => $this->getQualifiedParentKeyName(), - $this instanceof HasManyThrough => $this->getQualifiedLocalKeyName(), + $this instanceof HasOneOrManyThrough => $this->getQualifiedLocalKeyName(), default => throw new LogicException( sprintf('%s must be a relationship instance.', $this::class) ) diff --git a/tests/Features/HasInTest.php b/tests/Features/HasInTest.php index 5ccb7bf..9323bb7 100644 --- a/tests/Features/HasInTest.php +++ b/tests/Features/HasInTest.php @@ -1,15 +1,82 @@ orderBy('id')->pluck('id'); $hasIn = User::hasIn('posts')->orderBy('id')->pluck('id'); expect($has)->toEqual($hasIn); }); -test('hasIn(gte 2) same as has(gte 2)', function () { +test('HasOne: hasIn same as has', function () { + $has = User::has('phone')->orderBy('id')->pluck('id'); + $hasIn = User::hasIn('phone')->orderBy('id')->pluck('id'); + + expect($has)->toEqual($hasIn); +}); + +test('BelongsTo: hasIn same as has', function () { + $has = User::has('country')->orderBy('id')->pluck('id'); + $hasIn = User::hasIn('country')->orderBy('id')->pluck('id'); + + expect($has)->toEqual($hasIn); +}); + +test('BelongsToMany: hasIn same as has', function () { + $has = User::has('roles')->orderBy('id')->pluck('id'); + $hasIn = User::hasIn('roles')->orderBy('id')->pluck('id'); + + expect($has)->toEqual($hasIn); +}); + +test('HasOneThrough: hasIn same as has', function () { + $has = Supplier::has('userHistory')->orderBy('id')->pluck('id'); + $hasIn = Supplier::hasIn('userHistory')->orderBy('id')->pluck('id'); + + expect($has)->toEqual($hasIn); +}); + +test('HasManyThrough: hasIn same as has', function () { + $has = Country::has('posts')->orderBy('id')->pluck('id'); + $hasIn = Country::hasIn('posts')->orderBy('id')->pluck('id'); + + expect($has)->toEqual($hasIn); +}); + +test('MorphOne: hasIn same as has', function () { + $has = User::has('image')->orderBy('id')->pluck('id'); + $hasIn = User::hasIn('image')->orderBy('id')->pluck('id'); + + expect($has)->toEqual($hasIn); +}); + +test('MorphTo: hasIn same as has', function () { + $has = Image::has('imageable')->orderBy('id')->pluck('id'); + $hasIn = Image::hasIn('imageable')->orderBy('id')->pluck('id'); + + expect($has)->toEqual($hasIn); +}); + +test('MorphMany: hasIn same as has', function () { + $has = Video::has('comments')->orderBy('id')->pluck('id'); + $hasIn = Video::hasIn('comments')->orderBy('id')->pluck('id'); + + expect($has)->toEqual($hasIn); +}); + +test('MorphToMany: hasIn same as has', function () { + $has = Video::has('tags')->orderBy('id')->pluck('id'); + $hasIn = Video::hasIn('tags')->orderBy('id')->pluck('id'); + + expect($has)->toEqual($hasIn); +}); + +test('HasMany: hasIn(gte 2) same as has(gte 2)', function () { $has = User::has('posts', '>=', 2)->orderBy('id')->pluck('id'); $hasIn = User::hasIn('posts', '>=', 2)->orderBy('id')->pluck('id'); diff --git a/tests/Features/WithWhereHasInTest.php b/tests/Features/WithWhereHasInTest.php index bfa5b14..f58792d 100644 --- a/tests/Features/WithWhereHasInTest.php +++ b/tests/Features/WithWhereHasInTest.php @@ -11,8 +11,8 @@ $query->where('votes', '>', 20); })->orderBy('id')->get(); - expect($whereHas->pluck('id'))->toEqual($whereHasIn->pluck('id')); - expect($whereHas->pluck('posts.id'))->toEqual($whereHasIn->pluck('posts.id')); + expect($whereHas->pluck('id'))->toEqual($whereHasIn->pluck('id')) + ->and($whereHas->pluck('posts.id'))->toEqual($whereHasIn->pluck('posts.id')); }); test('nested withWhereHasIn same as nested withWhereHas', function () { @@ -23,7 +23,7 @@ $query->where('status', '>', 2); })->orderBy('id')->get(); - expect($whereHas->pluck('id'))->toEqual($whereHasIn->pluck('id')); - expect($whereHas->pluck('posts.id'))->toEqual($whereHasIn->pluck('posts.id')); - expect($whereHas->pluck('posts.comments.id'))->toEqual($whereHasIn->pluck('posts.comments.id')); + expect($whereHas->pluck('id'))->toEqual($whereHasIn->pluck('id')) + ->and($whereHas->pluck('posts.id'))->toEqual($whereHasIn->pluck('posts.id')) + ->and($whereHas->pluck('posts.comments.id'))->toEqual($whereHasIn->pluck('posts.comments.id')); }); From 8a094102f3a7ccc4953aed135d87aca1cc1c4f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Thu, 17 Oct 2024 20:56:13 +0800 Subject: [PATCH 2/3] fixed `HasOneThrough` relationship on laravel-11's change. --- tests/Features/HasInTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Features/HasInTest.php b/tests/Features/HasInTest.php index 9323bb7..934ff54 100644 --- a/tests/Features/HasInTest.php +++ b/tests/Features/HasInTest.php @@ -76,7 +76,7 @@ expect($has)->toEqual($hasIn); }); -test('HasMany: hasIn(gte 2) same as has(gte 2)', function () { +test('hasIn(gte 2) same as has(gte 2)', function () { $has = User::has('posts', '>=', 2)->orderBy('id')->pluck('id'); $hasIn = User::hasIn('posts', '>=', 2)->orderBy('id')->pluck('id'); From e73a0c9b6e5b27e3fddd699b6122e2859291ff06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Thu, 17 Oct 2024 21:16:22 +0800 Subject: [PATCH 3/3] fixed `HasOneThrough` relationship on laravel-11's change. --- src/Database/Eloquent/RelationMixin.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Database/Eloquent/RelationMixin.php b/src/Database/Eloquent/RelationMixin.php index 9baab34..581d778 100644 --- a/src/Database/Eloquent/RelationMixin.php +++ b/src/Database/Eloquent/RelationMixin.php @@ -6,9 +6,10 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOneOrMany; -use Illuminate\Database\Eloquent\Relations\HasOneOrManyThrough; +use Illuminate\Database\Eloquent\Relations\HasOneThrough; use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphOneOrMany; use Illuminate\Database\Eloquent\Relations\MorphToMany; @@ -132,7 +133,7 @@ public function getRelationExistenceInQuery(): Closure $this instanceof BelongsTo => $belongsTo($query, $parentQuery), $this instanceof BelongsToMany => $belongsToMany($query, $parentQuery), $this instanceof HasOneOrMany => $hasOneOrMany($query, $parentQuery), - $this instanceof HasOneOrManyThrough => $hasOneOrManyThrough($query, $parentQuery), + $this instanceof HasOneThrough, $this instanceof HasManyThrough => $hasOneOrManyThrough($query, $parentQuery), default => throw new LogicException( sprintf('%s must be a relationship instance.', $this::class) ) @@ -145,7 +146,7 @@ public function getRelationWhereInKey(): Closure return fn (): string => match (true) { $this instanceof BelongsTo => $this->getQualifiedForeignKeyName(), $this instanceof HasOneOrMany, $this instanceof BelongsToMany => $this->getQualifiedParentKeyName(), - $this instanceof HasOneOrManyThrough => $this->getQualifiedLocalKeyName(), + $this instanceof HasOneThrough, $this instanceof HasManyThrough => $this->getQualifiedLocalKeyName(), default => throw new LogicException( sprintf('%s must be a relationship instance.', $this::class) )