Skip to content

PHPORM-159 Add tests on whereAny and whereAll #2763

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

Merged
merged 2 commits into from
Mar 13, 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
5 changes: 0 additions & 5 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1154,11 +1154,6 @@ protected function compileWheres(): array
return $compiled;
}

/**
* @param array $where
*
* @return array
*/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2765

protected function compileWhereBasic(array $where): array
{
$column = $where['column'];
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected function username(): Attribute
{
return Attribute::make(
get: fn ($value) => $value,
set: fn ($value) => Str::slug($value)
set: fn ($value) => Str::slug($value),
);
}

Expand Down
139 changes: 139 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use stdClass;

use function collect;
use function method_exists;
use function now;
use function var_export;

Expand Down Expand Up @@ -1157,6 +1158,144 @@ function (Builder $elemMatchQuery): void {
},
),
];

// Method added in Laravel v10.47.0
if (method_exists(Builder::class, 'whereAll')) {
/** @see DatabaseQueryBuilderTest::testWhereAll */
yield 'whereAll' => [
[
'find' => [
['$and' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
[], // options
],
],
fn(Builder $builder) => $builder->whereAll(['last_name', 'email'], 'Doe'),
];

yield 'whereAll operator' => [
[
'find' => [
[
'$and' => [
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
],
],
[], // options
],
],
fn(Builder $builder) => $builder->whereAll(['last_name', 'email'], 'not like', '%Doe%'),
];

/** @see DatabaseQueryBuilderTest::testOrWhereAll */
yield 'orWhereAll' => [
[
'find' => [
[
'$or' => [
['first_name' => 'John'],
['$and' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
],
],
[], // options
],
],
fn(Builder $builder) => $builder
->where('first_name', 'John')
->orWhereAll(['last_name', 'email'], 'Doe'),
];

yield 'orWhereAll operator' => [
[
'find' => [
[
'$or' => [
['first_name' => new Regex('^.*John.*$', 'i')],
[
'$and' => [
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
],
],
],
],
[], // options
],
],
fn(Builder $builder) => $builder
->where('first_name', 'like', '%John%')
->orWhereAll(['last_name', 'email'], 'not like', '%Doe%'),
];
}

// Method added in Laravel v10.47.0
if (method_exists(Builder::class, 'whereAny')) {
/** @see DatabaseQueryBuilderTest::testWhereAny */
yield 'whereAny' => [
[
'find' => [
['$or' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
[], // options
],
],
fn(Builder $builder) => $builder->whereAny(['last_name', 'email'], 'Doe'),
];

yield 'whereAny operator' => [
[
'find' => [
[
'$or' => [
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
],
],
[], // options
],
],
fn(Builder $builder) => $builder->whereAny(['last_name', 'email'], 'not like', '%Doe%'),
];

/** @see DatabaseQueryBuilderTest::testOrWhereAny */
yield 'orWhereAny' => [
[
'find' => [
[
'$or' => [
['first_name' => 'John'],
['$or' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
],
],
[], // options
],
],
fn(Builder $builder) => $builder
->where('first_name', 'John')
->orWhereAny(['last_name', 'email'], 'Doe'),
];

yield 'orWhereAny operator' => [
[
'find' => [
[
'$or' => [
['first_name' => new Regex('^.*John.*$', 'i')],
[
'$or' => [
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
],
],
],
],
[], // options
],
],
fn(Builder $builder) => $builder
->where('first_name', 'like', '%John%')
->orWhereAny(['last_name', 'email'], 'not like', '%Doe%'),
];
}
}

/** @dataProvider provideExceptions */
Expand Down