Skip to content

Commit

Permalink
fixed HasOneThrough relationship on laravel-11's change.
Browse files Browse the repository at this point in the history
  • Loading branch information
陆云峰 committed Oct 17, 2024
1 parent 5d5e868 commit ed4e14d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ docs
phpunit.xml
testbench.yaml
vendor
node_modules
node_modules
.phpunit.cache
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ build:
image: default-bionic
environment:
php:
version: 8.1
version: 8.2
tests:
override:
- command: './vendor/bin/pest'
8 changes: 4 additions & 4 deletions src/Database/Eloquent/RelationMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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)
)
Expand All @@ -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)
)
Expand Down
71 changes: 69 additions & 2 deletions tests/Features/HasInTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,82 @@
<?php

use BiiiiiigMonster\Hasin\Tests\Models\Country;
use BiiiiiigMonster\Hasin\Tests\Models\Image;
use BiiiiiigMonster\Hasin\Tests\Models\Supplier;
use BiiiiiigMonster\Hasin\Tests\Models\User;
use BiiiiiigMonster\Hasin\Tests\Models\Video;

test('hasIn same as has', function () {
test('HasMany: hasIn same as has', function () {
$has = User::has('posts')->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');

Expand Down
10 changes: 5 additions & 5 deletions tests/Features/WithWhereHasInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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'));
});

0 comments on commit ed4e14d

Please sign in to comment.