Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit e0dd130

Browse files
committed
Use assert for exception
1 parent 9ae2701 commit e0dd130

File tree

2 files changed

+65
-27
lines changed

2 files changed

+65
-27
lines changed

src/Query/Builder.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -565,14 +565,7 @@ public function whereBetween($column, iterable $values, $boolean = 'and', $not =
565565
$values = $values->all();
566566
}
567567

568-
if (is_array($values)) {
569-
if (! array_is_list($values)) {
570-
throw new \InvalidArgumentException('Between $values must be a list with exactly two elements: [min, max]');
571-
}
572-
if (count($values) !== 2) {
573-
throw new \InvalidArgumentException('Between $values must have exactly two elements: [min, max]');
574-
}
575-
}
568+
assert(!is_array($values) || array_is_list($values) && count($values) === 2, new \InvalidArgumentException('Between $values must be a list with exactly two elements: [min, max]'));
576569

577570
$this->wheres[] = compact('column', 'type', 'boolean', 'values', 'not');
578571

tests/Query/BuilderTest.php

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,22 @@ function (Builder $builder) {
137137
];
138138

139139
yield 'whereNotBetween array of numbers' => [
140-
['find' => [['$or' => [['id' => ['$lte' => 1]], ['id' => ['$gte' => 2]]]], []]],
140+
['find' => [
141+
['$or' => [
142+
['id' => ['$lte' => 1]],
143+
['id' => ['$gte' => 2]],
144+
]],
145+
[], // options
146+
]],
141147
fn (Builder $builder) => $builder->whereNotBetween('id', [1, 2]),
142148
];
143149

144-
$period = now()->toPeriod(now()->addDay());
145-
yield 'whereBetween CarbonPeriod' => [
146-
['find' => [['created_at' => ['$gte' => new UTCDateTime($period->start), '$lte' => new UTCDateTime($period->end)]], []]],
147-
fn (Builder $builder) => $builder->whereBetween('created_at', $period),
148-
];
149-
150150
$period = now()->toPeriod(now()->addMonth());
151-
yield 'custom long carbon period date' => [
152-
['find' => [['created_at' => ['$gte' => new UTCDateTime($period->start), '$lte' => new UTCDateTime($period->end)]], []]],
151+
yield 'whereBetween CarbonPeriod' => [
152+
['find' => [
153+
['created_at' => ['$gte' => new UTCDateTime($period->start), '$lte' => new UTCDateTime($period->end)]],
154+
[], // options
155+
]],
153156
fn (Builder $builder) => $builder->whereBetween('created_at', $period),
154157
];
155158

@@ -168,22 +171,43 @@ function (Builder $builder) {
168171

169172
/** @link https://www.mongodb.com/docs/manual/reference/bson-type-comparison-order/#arrays */
170173
yield 'orWhereBetween nested array of numbers' => [
171-
['find' => [['$or' => [['id' => 1], ['id' => ['$gte' => [4], '$lte' => [6, 8]]]]], []]],
174+
['find' => [
175+
['$or' => [
176+
['id' => 1],
177+
['id' => ['$gte' => [4], '$lte' => [6, 8]]],
178+
]],
179+
[], // options
180+
]],
172181
fn (Builder $builder) => $builder
173182
->where('id', '=', 1)
174183
->orWhereBetween('id', [[4], [6, 8]]),
175184
];
176185

177186
yield 'orWhereBetween collection' => [
178-
['find' => [['$or' => [['id' => 1], ['id' => ['$gte' => 3, '$lte' => 4]]]], []]],
187+
['find' => [
188+
['$or' => [
189+
['id' => 1],
190+
['id' => ['$gte' => 3, '$lte' => 4]],
191+
]],
192+
[], // options
193+
]],
179194
fn (Builder $builder) => $builder
180195
->where('id', '=', 1)
181196
->orWhereBetween('id', collect([3, 4])),
182197
];
183198

184199
/** @see DatabaseQueryBuilderTest::testOrWhereNotBetween() */
185200
yield 'orWhereNotBetween array of numbers' => [
186-
['find' => [['$or' => [['id' => 1], ['$or' => [['id' => ['$lte' => 3]], ['id' => ['$gte' => 5]]]]]], []]],
201+
['find' => [
202+
['$or' => [
203+
['id' => 1],
204+
['$or' => [
205+
['id' => ['$lte' => 3]],
206+
['id' => ['$gte' => 5]],
207+
]],
208+
]],
209+
[], // options
210+
]],
187211
fn (Builder $builder) => $builder
188212
->where('id', '=', 1)
189213
->orWhereNotBetween('id', [3, 5]),
@@ -197,14 +221,32 @@ function (Builder $builder) {
197221
];
198222

199223
yield 'orWhereNotBetween excessive nested array of numbers' => [
200-
['find' => [['$or' => [['id' => 1], ['$or' => [['id' => ['$lte' => [4]]], ['id' => ['$gte' => [6, 8]]]]]]], []]],
224+
['find' => [
225+
['$or' => [
226+
['id' => 1],
227+
['$or' => [
228+
['id' => ['$lte' => [4]]],
229+
['id' => ['$gte' => [6, 8]]],
230+
]]
231+
]],
232+
[], // options
233+
]],
201234
fn (Builder $builder) => $builder
202235
->where('id', '=', 1)
203236
->orWhereNotBetween('id', [[4], [6, 8]]),
204237
];
205238

206239
yield 'orWhereNotBetween collection' => [
207-
['find' => [['$or' => [['id' => 1], ['$or' => [['id' => ['$lte' => 3]], ['id' => ['$gte' => 4]]]]]], []]],
240+
['find' => [
241+
['$or' => [
242+
['id' => 1],
243+
['$or' => [
244+
['id' => ['$lte' => 3]],
245+
['id' => ['$gte' => 4]],
246+
]],
247+
]],
248+
[], // options
249+
]],
208250
fn (Builder $builder) => $builder
209251
->where('id', '=', 1)
210252
->orWhereNotBetween('id', collect([3, 4])),
@@ -216,7 +258,10 @@ function (Builder $builder) {
216258
];
217259

218260
yield 'groupBy' => [
219-
['aggregate' => [[['$group' => ['_id' => ['foo' => '$foo'], 'foo' => ['$last' => '$foo']]]], []]],
261+
['aggregate' => [
262+
[['$group' => ['_id' => ['foo' => '$foo'], 'foo' => ['$last' => '$foo']]]],
263+
[], // options
264+
]],
220265
fn (Builder $builder) => $builder->groupBy('foo'),
221266
];
222267
}
@@ -244,19 +289,19 @@ public static function provideExceptions(): iterable
244289
/** @see DatabaseQueryBuilderTest::testWhereBetweens */
245290
yield 'whereBetween array too short' => [
246291
\InvalidArgumentException::class,
247-
'Between $values must have exactly two elements: [min, max]',
292+
'Between $values must be a list with exactly two elements: [min, max]',
248293
fn (Builder $builder) => $builder->whereBetween('id', [1]),
249294
];
250295

251296
yield 'whereBetween array too long' => [
252297
\InvalidArgumentException::class,
253-
'Between $values must have exactly two elements: [min, max]',
298+
'Between $values must be a list with exactly two elements: [min, max]',
254299
fn (Builder $builder) => $builder->whereBetween('id', [1, 2, 3]),
255300
];
256301

257302
yield 'whereBetween collection too long' => [
258303
\InvalidArgumentException::class,
259-
'Between $values must have exactly two elements: [min, max]',
304+
'Between $values must be a list with exactly two elements: [min, max]',
260305
fn (Builder $builder) => $builder->whereBetween('id', new Collection([1, 2, 3])),
261306
];
262307

@@ -268,7 +313,7 @@ public static function provideExceptions(): iterable
268313

269314
yield 'whereBetween nested' => [
270315
\InvalidArgumentException::class,
271-
'Between $values must have exactly two elements: [min, max]',
316+
'Between $values must be a list with exactly two elements: [min, max]',
272317
fn (Builder $builder) => $builder->whereBetween('id', [[1, 2]]),
273318
];
274319
}

0 commit comments

Comments
 (0)