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

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jul 19, 2023
1 parent fb84adf commit 437d9cf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
}
}

if (func_num_args() == 1 && is_string($column)) {
throw new \ArgumentCountError(sprintf('Too few arguments to function %s("%s"), 1 passed and at least 2 expected when the 1st is a string.', __METHOD__, $column));
}

return parent::where(...$params);
}

Expand Down
50 changes: 32 additions & 18 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,14 @@ public static function provideQueryBuilderToMql(): iterable
*/
$date = new DateTimeImmutable('2016-07-12 15:30:00');

/** @see DatabaseQueryBuilderTest::testBasicSelectWithGetColumns */
yield 'find all' => [
['find' => [[], []]],
fn (Builder $builder) => $builder->select('*'),
];

yield 'find default null' => [
['find' => [['foo' => null], []]],
fn (Builder $builder) => $builder->where('foo'),
yield 'select replaces previous select' => [
['find' => [[], ['projection' => ['bar' => 1]]]],
fn (Builder $builder) => $builder->select('foo')->select('bar'),
];

yield 'find all with select' => [
yield 'select array' => [
['find' => [[], ['projection' => ['foo' => 1, 'bar' => 1]]]],
fn (Builder $builder) => $builder->select('foo', 'bar'),
fn (Builder $builder) => $builder->select(['foo', 'bar']),
];

/** @see DatabaseQueryBuilderTest::testAddingSelects */
Expand All @@ -70,6 +64,16 @@ public static function provideQueryBuilderToMql(): iterable
->addSelect('bar'),
];

yield 'select all' => [
['find' => [[], []]],
fn (Builder $builder) => $builder->select('*'),
];

yield 'find all with select' => [
['find' => [[], ['projection' => ['foo' => 1, 'bar' => 1]]]],
fn (Builder $builder) => $builder->select('foo', 'bar'),
];

yield 'find equals' => [
['find' => [['foo' => 'bar'], []]],
fn (Builder $builder) => $builder->where('foo', 'bar'),
Expand All @@ -86,11 +90,8 @@ public static function provideQueryBuilderToMql(): iterable
fn (Builder $builder) => $builder->whereIn('foo', ['bar', 'baz']),
];

yield 'whereIn associative array' => [
['find' => [['id' => ['$in' => [45582, 2, 3]]], []]],
fn (Builder $builder) => $builder->whereIn('id', ['issue' => 45582, 'id' => 2, 3]),
];

// Nested array are not flattened like in the Eloquent builder.
// MongoDB can compare arrays.
$array = [['issue' => 45582], ['id' => 2], [3]];
yield 'whereIn nested array' => [
['find' => [['id' => ['$in' => $array]], []]],
Expand Down Expand Up @@ -138,7 +139,7 @@ public static function provideQueryBuilderToMql(): iterable
fn (Builder $builder) => $builder->limit(10)->offset(5)->select('foo', 'bar'),
];

yield 'skip limit' => [
yield 'offset limit' => [
['find' => [[], ['skip' => 5, 'limit' => 10]]],
fn (Builder $builder) => $builder->offset(5)->limit(10),
];
Expand All @@ -159,7 +160,7 @@ public static function provideQueryBuilderToMql(): iterable
fn (Builder $builder) => $builder->skip(5)->take(10),
];

yield 'kip 0 take 0' => [
yield 'skip 0 take 0' => [
['find' => [[], []]],
fn (Builder $builder) => $builder->skip(0)->take(0),
];
Expand Down Expand Up @@ -475,6 +476,19 @@ public static function provideExceptions(): iterable
'Between $values must be a list with exactly two elements: [min, max]',
fn (Builder $builder) => $builder->whereBetween('id', ['min' => 1, 'max' => 2]),
];

yield 'find with single string argument' => [
\ArgumentCountError::class,
'Too few arguments to function Jenssegers\Mongodb\Query\Builder::where("foo"), 1 passed and at least 2 expected when the 1st is a string',
fn (Builder $builder) => $builder->where('foo'),
];

yield 'whereIn associative array' => [
\InvalidArgumentException::class,
'whereIn() expects 2nd argument to be a list, associative array given with keys "issue", "id", "0"',
fn (Builder $builder) => $builder->whereIn('id', ['issue' => 45582, 'id' => 2, 3]),
];

}

/** @dataProvider getEloquentMethodsNotSupported */
Expand Down

0 comments on commit 437d9cf

Please sign in to comment.