Skip to content

Commit 95455d2

Browse files
committed
Check required methods from Laravel in tests
1 parent 143b632 commit 95455d2

File tree

1 file changed

+146
-117
lines changed

1 file changed

+146
-117
lines changed

tests/Query/BuilderTest.php

+146-117
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@
2626
use function collect;
2727
use function method_exists;
2828
use function now;
29+
use function sprintf;
2930
use function var_export;
3031

3132
class BuilderTest extends TestCase
3233
{
3334
#[DataProvider('provideQueryBuilderToMql')]
34-
public function testMql(array $expected, Closure $build): void
35+
public function testMql(array $expected, Closure $build, ?string $requiredMethod = null): void
3536
{
37+
if ($requiredMethod && ! method_exists(Builder::class, $requiredMethod)) {
38+
$this->markTestSkipped(sprintf('Method "%s::%s()" does not exist.', Builder::class, $requiredMethod));
39+
}
40+
3641
$builder = $build(self::getBuilder());
3742
$this->assertInstanceOf(Builder::class, $builder);
3843
$mql = $builder->toMql();
@@ -750,32 +755,44 @@ function (Builder $builder) {
750755

751756
yield 'whereLike' => [
752757
['find' => [['name' => new Regex('^1$', 'i')], []]],
753-
fn (Builder $builder) => $builder->whereLike('name', '1'),
758+
fn
759+
(Builder $builder) => $builder->whereLike('name', '1'),
760+
'whereLike',
754761
];
755762

756763
yield 'whereLike case not sensitive' => [
757764
['find' => [['name' => new Regex('^1$', 'i')], []]],
758-
fn (Builder $builder) => $builder->whereLike('name', '1', false),
765+
fn
766+
(Builder $builder) => $builder->whereLike('name', '1', false),
767+
'whereLike',
759768
];
760769

761770
yield 'whereLike case sensitive' => [
762771
['find' => [['name' => new Regex('^1$', '')], []]],
763-
fn (Builder $builder) => $builder->whereLike('name', '1', true),
772+
fn
773+
(Builder $builder) => $builder->whereLike('name', '1', true),
774+
'whereLike',
764775
];
765776

766777
yield 'whereNotLike' => [
767778
['find' => [['name' => ['$not' => new Regex('^1$', 'i')]], []]],
768-
fn (Builder $builder) => $builder->whereNotLike('name', '1'),
779+
fn
780+
(Builder $builder) => $builder->whereNotLike('name', '1'),
781+
'whereNotLike',
769782
];
770783

771784
yield 'whereNotLike case not sensitive' => [
772785
['find' => [['name' => ['$not' => new Regex('^1$', 'i')]], []]],
773-
fn (Builder $builder) => $builder->whereNotLike('name', '1', false),
786+
fn
787+
(Builder $builder) => $builder->whereNotLike('name', '1', false),
788+
'whereNotLike',
774789
];
775790

776791
yield 'whereNotLike case sensitive' => [
777792
['find' => [['name' => ['$not' => new Regex('^1$', '')]], []]],
778-
fn (Builder $builder) => $builder->whereNotLike('name', '1', true),
793+
fn
794+
(Builder $builder) => $builder->whereNotLike('name', '1', true),
795+
'whereNotLike',
779796
];
780797

781798
$regex = new Regex('^acme$', 'si');
@@ -1191,142 +1208,154 @@ function (Builder $elemMatchQuery): void {
11911208
];
11921209

11931210
// Method added in Laravel v10.47.0
1194-
if (method_exists(Builder::class, 'whereAll')) {
1195-
/** @see DatabaseQueryBuilderTest::testWhereAll */
1196-
yield 'whereAll' => [
1197-
[
1198-
'find' => [
1199-
['$and' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
1200-
[], // options
1201-
],
1211+
/** @see DatabaseQueryBuilderTest::testWhereAll */
1212+
yield 'whereAll' => [
1213+
[
1214+
'find' => [
1215+
['$and' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
1216+
[], // options
12021217
],
1203-
fn(Builder $builder) => $builder->whereAll(['last_name', 'email'], 'Doe'),
1204-
];
1205-
1206-
yield 'whereAll operator' => [
1207-
[
1208-
'find' => [
1209-
[
1210-
'$and' => [
1211-
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1212-
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1213-
],
1218+
],
1219+
fn
1220+
(Builder $builder) => $builder->whereAll(['last_name', 'email'], 'Doe'),
1221+
'whereAll',
1222+
];
1223+
1224+
yield 'whereAll operator' => [
1225+
[
1226+
'find' => [
1227+
[
1228+
'$and' => [
1229+
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1230+
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
12141231
],
1215-
[], // options
12161232
],
1233+
[], // options
12171234
],
1218-
fn(Builder $builder) => $builder->whereAll(['last_name', 'email'], 'not like', '%Doe%'),
1219-
];
1220-
1221-
/** @see DatabaseQueryBuilderTest::testOrWhereAll */
1222-
yield 'orWhereAll' => [
1223-
[
1224-
'find' => [
1225-
[
1226-
'$or' => [
1227-
['first_name' => 'John'],
1228-
['$and' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
1229-
],
1235+
],
1236+
fn
1237+
(Builder $builder) => $builder->whereAll(['last_name', 'email'], 'not like', '%Doe%'),
1238+
'whereAll',
1239+
];
1240+
1241+
/** @see DatabaseQueryBuilderTest::testOrWhereAll */
1242+
yield 'orWhereAll' => [
1243+
[
1244+
'find' => [
1245+
[
1246+
'$or' => [
1247+
['first_name' => 'John'],
1248+
['$and' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
12301249
],
1231-
[], // options
12321250
],
1251+
[], // options
12331252
],
1234-
fn(Builder $builder) => $builder
1235-
->where('first_name', 'John')
1236-
->orWhereAll(['last_name', 'email'], 'Doe'),
1237-
];
1238-
1239-
yield 'orWhereAll operator' => [
1240-
[
1241-
'find' => [
1242-
[
1243-
'$or' => [
1244-
['first_name' => new Regex('^.*John.*$', 'i')],
1245-
[
1246-
'$and' => [
1247-
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1248-
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1249-
],
1253+
],
1254+
fn
1255+
(Builder $builder) => $builder
1256+
->where('first_name', 'John')
1257+
->orWhereAll(['last_name', 'email'], 'Doe'),
1258+
'orWhereAll',
1259+
];
1260+
1261+
yield 'orWhereAll operator' => [
1262+
[
1263+
'find' => [
1264+
[
1265+
'$or' => [
1266+
['first_name' => new Regex('^.*John.*$', 'i')],
1267+
[
1268+
'$and' => [
1269+
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1270+
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
12501271
],
12511272
],
12521273
],
1253-
[], // options
12541274
],
1275+
[], // options
12551276
],
1256-
fn(Builder $builder) => $builder
1257-
->where('first_name', 'like', '%John%')
1258-
->orWhereAll(['last_name', 'email'], 'not like', '%Doe%'),
1259-
];
1260-
}
1277+
],
1278+
fn
1279+
(Builder $builder) => $builder
1280+
->where('first_name', 'like', '%John%')
1281+
->orWhereAll(['last_name', 'email'], 'not like', '%Doe%'),
1282+
'orWhereAll',
1283+
];
12611284

12621285
// Method added in Laravel v10.47.0
1263-
if (method_exists(Builder::class, 'whereAny')) {
1264-
/** @see DatabaseQueryBuilderTest::testWhereAny */
1265-
yield 'whereAny' => [
1266-
[
1267-
'find' => [
1268-
['$or' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
1269-
[], // options
1270-
],
1286+
/** @see DatabaseQueryBuilderTest::testWhereAny */
1287+
yield 'whereAny' => [
1288+
[
1289+
'find' => [
1290+
['$or' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
1291+
[], // options
12711292
],
1272-
fn(Builder $builder) => $builder->whereAny(['last_name', 'email'], 'Doe'),
1273-
];
1274-
1275-
yield 'whereAny operator' => [
1276-
[
1277-
'find' => [
1278-
[
1279-
'$or' => [
1280-
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1281-
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1282-
],
1293+
],
1294+
fn
1295+
(Builder $builder) => $builder->whereAny(['last_name', 'email'], 'Doe'),
1296+
'whereAny',
1297+
];
1298+
1299+
yield 'whereAny operator' => [
1300+
[
1301+
'find' => [
1302+
[
1303+
'$or' => [
1304+
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1305+
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
12831306
],
1284-
[], // options
12851307
],
1308+
[], // options
12861309
],
1287-
fn(Builder $builder) => $builder->whereAny(['last_name', 'email'], 'not like', '%Doe%'),
1288-
];
1289-
1290-
/** @see DatabaseQueryBuilderTest::testOrWhereAny */
1291-
yield 'orWhereAny' => [
1292-
[
1293-
'find' => [
1294-
[
1295-
'$or' => [
1296-
['first_name' => 'John'],
1297-
['$or' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
1298-
],
1310+
],
1311+
fn
1312+
(Builder $builder) => $builder->whereAny(['last_name', 'email'], 'not like', '%Doe%'),
1313+
'whereAny',
1314+
];
1315+
1316+
/** @see DatabaseQueryBuilderTest::testOrWhereAny */
1317+
yield 'orWhereAny' => [
1318+
[
1319+
'find' => [
1320+
[
1321+
'$or' => [
1322+
['first_name' => 'John'],
1323+
['$or' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
12991324
],
1300-
[], // options
13011325
],
1326+
[], // options
13021327
],
1303-
fn(Builder $builder) => $builder
1304-
->where('first_name', 'John')
1305-
->orWhereAny(['last_name', 'email'], 'Doe'),
1306-
];
1307-
1308-
yield 'orWhereAny operator' => [
1309-
[
1310-
'find' => [
1311-
[
1312-
'$or' => [
1313-
['first_name' => new Regex('^.*John.*$', 'i')],
1314-
[
1315-
'$or' => [
1316-
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1317-
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1318-
],
1328+
],
1329+
fn
1330+
(Builder $builder) => $builder
1331+
->where('first_name', 'John')
1332+
->orWhereAny(['last_name', 'email'], 'Doe'),
1333+
'whereAny',
1334+
];
1335+
1336+
yield 'orWhereAny operator' => [
1337+
[
1338+
'find' => [
1339+
[
1340+
'$or' => [
1341+
['first_name' => new Regex('^.*John.*$', 'i')],
1342+
[
1343+
'$or' => [
1344+
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1345+
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
13191346
],
13201347
],
13211348
],
1322-
[], // options
13231349
],
1350+
[], // options
13241351
],
1325-
fn(Builder $builder) => $builder
1326-
->where('first_name', 'like', '%John%')
1327-
->orWhereAny(['last_name', 'email'], 'not like', '%Doe%'),
1328-
];
1329-
}
1352+
],
1353+
fn
1354+
(Builder $builder) => $builder
1355+
->where('first_name', 'like', '%John%')
1356+
->orWhereAny(['last_name', 'email'], 'not like', '%Doe%'),
1357+
'orWhereAny',
1358+
];
13301359
}
13311360

13321361
#[DataProvider('provideExceptions')]

0 commit comments

Comments
 (0)