Skip to content

Commit 6471219

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

File tree

1 file changed

+134
-111
lines changed

1 file changed

+134
-111
lines changed

tests/Query/BuilderTest.php

Lines changed: 134 additions & 111 deletions
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();
@@ -751,31 +756,37 @@ function (Builder $builder) {
751756
yield 'whereLike' => [
752757
['find' => [['name' => new Regex('^1$', 'i')], []]],
753758
fn (Builder $builder) => $builder->whereLike('name', '1'),
759+
'whereLike',
754760
];
755761

756762
yield 'whereLike case not sensitive' => [
757763
['find' => [['name' => new Regex('^1$', 'i')], []]],
758764
fn (Builder $builder) => $builder->whereLike('name', '1', false),
765+
'whereLike',
759766
];
760767

761768
yield 'whereLike case sensitive' => [
762769
['find' => [['name' => new Regex('^1$', '')], []]],
763770
fn (Builder $builder) => $builder->whereLike('name', '1', true),
771+
'whereLike',
764772
];
765773

766774
yield 'whereNotLike' => [
767775
['find' => [['name' => ['$not' => new Regex('^1$', 'i')]], []]],
768776
fn (Builder $builder) => $builder->whereNotLike('name', '1'),
777+
'whereNotLike',
769778
];
770779

771780
yield 'whereNotLike case not sensitive' => [
772781
['find' => [['name' => ['$not' => new Regex('^1$', 'i')]], []]],
773782
fn (Builder $builder) => $builder->whereNotLike('name', '1', false),
783+
'whereNotLike',
774784
];
775785

776786
yield 'whereNotLike case sensitive' => [
777787
['find' => [['name' => ['$not' => new Regex('^1$', '')]], []]],
778788
fn (Builder $builder) => $builder->whereNotLike('name', '1', true),
789+
'whereNotLike',
779790
];
780791

781792
$regex = new Regex('^acme$', 'si');
@@ -1191,142 +1202,154 @@ function (Builder $elemMatchQuery): void {
11911202
];
11921203

11931204
// 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-
],
1205+
/** @see DatabaseQueryBuilderTest::testWhereAll */
1206+
yield 'whereAll' => [
1207+
[
1208+
'find' => [
1209+
['$and' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
1210+
[], // options
12021211
],
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-
],
1212+
],
1213+
fn
1214+
(Builder $builder) => $builder->whereAll(['last_name', 'email'], 'Doe'),
1215+
'whereAll',
1216+
];
1217+
1218+
yield 'whereAll operator' => [
1219+
[
1220+
'find' => [
1221+
[
1222+
'$and' => [
1223+
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1224+
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
12141225
],
1215-
[], // options
12161226
],
1227+
[], // options
12171228
],
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-
],
1229+
],
1230+
fn
1231+
(Builder $builder) => $builder->whereAll(['last_name', 'email'], 'not like', '%Doe%'),
1232+
'whereAll',
1233+
];
1234+
1235+
/** @see DatabaseQueryBuilderTest::testOrWhereAll */
1236+
yield 'orWhereAll' => [
1237+
[
1238+
'find' => [
1239+
[
1240+
'$or' => [
1241+
['first_name' => 'John'],
1242+
['$and' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
12301243
],
1231-
[], // options
12321244
],
1245+
[], // options
12331246
],
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-
],
1247+
],
1248+
fn
1249+
(Builder $builder) => $builder
1250+
->where('first_name', 'John')
1251+
->orWhereAll(['last_name', 'email'], 'Doe'),
1252+
'orWhereAll',
1253+
];
1254+
1255+
yield 'orWhereAll operator' => [
1256+
[
1257+
'find' => [
1258+
[
1259+
'$or' => [
1260+
['first_name' => new Regex('^.*John.*$', 'i')],
1261+
[
1262+
'$and' => [
1263+
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1264+
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
12501265
],
12511266
],
12521267
],
1253-
[], // options
12541268
],
1269+
[], // options
12551270
],
1256-
fn(Builder $builder) => $builder
1257-
->where('first_name', 'like', '%John%')
1258-
->orWhereAll(['last_name', 'email'], 'not like', '%Doe%'),
1259-
];
1260-
}
1271+
],
1272+
fn
1273+
(Builder $builder) => $builder
1274+
->where('first_name', 'like', '%John%')
1275+
->orWhereAll(['last_name', 'email'], 'not like', '%Doe%'),
1276+
'orWhereAll',
1277+
];
12611278

12621279
// 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-
],
1280+
/** @see DatabaseQueryBuilderTest::testWhereAny */
1281+
yield 'whereAny' => [
1282+
[
1283+
'find' => [
1284+
['$or' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
1285+
[], // options
12711286
],
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-
],
1287+
],
1288+
fn
1289+
(Builder $builder) => $builder->whereAny(['last_name', 'email'], 'Doe'),
1290+
'whereAny',
1291+
];
1292+
1293+
yield 'whereAny operator' => [
1294+
[
1295+
'find' => [
1296+
[
1297+
'$or' => [
1298+
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1299+
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
12831300
],
1284-
[], // options
12851301
],
1302+
[], // options
12861303
],
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-
],
1304+
],
1305+
fn
1306+
(Builder $builder) => $builder->whereAny(['last_name', 'email'], 'not like', '%Doe%'),
1307+
'whereAny',
1308+
];
1309+
1310+
/** @see DatabaseQueryBuilderTest::testOrWhereAny */
1311+
yield 'orWhereAny' => [
1312+
[
1313+
'find' => [
1314+
[
1315+
'$or' => [
1316+
['first_name' => 'John'],
1317+
['$or' => [['last_name' => 'Doe'], ['email' => 'Doe']]],
12991318
],
1300-
[], // options
13011319
],
1320+
[], // options
13021321
],
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-
],
1322+
],
1323+
fn
1324+
(Builder $builder) => $builder
1325+
->where('first_name', 'John')
1326+
->orWhereAny(['last_name', 'email'], 'Doe'),
1327+
'whereAny',
1328+
];
1329+
1330+
yield 'orWhereAny operator' => [
1331+
[
1332+
'find' => [
1333+
[
1334+
'$or' => [
1335+
['first_name' => new Regex('^.*John.*$', 'i')],
1336+
[
1337+
'$or' => [
1338+
['last_name' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
1339+
['email' => ['$not' => new Regex('^.*Doe.*$', 'i')]],
13191340
],
13201341
],
13211342
],
1322-
[], // options
13231343
],
1344+
[], // options
13241345
],
1325-
fn(Builder $builder) => $builder
1326-
->where('first_name', 'like', '%John%')
1327-
->orWhereAny(['last_name', 'email'], 'not like', '%Doe%'),
1328-
];
1329-
}
1346+
],
1347+
fn
1348+
(Builder $builder) => $builder
1349+
->where('first_name', 'like', '%John%')
1350+
->orWhereAny(['last_name', 'email'], 'not like', '%Doe%'),
1351+
'orWhereAny',
1352+
];
13301353
}
13311354

13321355
#[DataProvider('provideExceptions')]

0 commit comments

Comments
 (0)