Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed HasOneThrough relationship on laravel-11's change. #52

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
7 changes: 4 additions & 3 deletions src/Database/Eloquent/RelationMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
Expand Down Expand Up @@ -62,7 +63,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 +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 HasManyThrough => $hasManyThrough($query, $parentQuery),
$this instanceof HasOneThrough, $this instanceof HasManyThrough => $hasOneOrManyThrough($query, $parentQuery),
default => throw new LogicException(
sprintf('%s must be a relationship instance.', $this::class)
)
Expand All @@ -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 HasManyThrough => $this->getQualifiedLocalKeyName(),
$this instanceof HasOneThrough, $this instanceof HasManyThrough => $this->getQualifiedLocalKeyName(),
default => throw new LogicException(
sprintf('%s must be a relationship instance.', $this::class)
)
Expand Down
69 changes: 68 additions & 1 deletion tests/Features/HasInTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,81 @@
<?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('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('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'));
});