Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b9bbcdd

Browse files
GromNaNalcaeus
authored andcommitted
PHPORM-33 Add tests on Query\Builder methods (#14)
- Add tests on query builder methods that don't need to be fixed. - Throw exception when calling unsupported methods: whereIntegerInRaw, orWhereIntegerInRaw, whereIntegerNotInRaw, orWhereIntegerNotInRaw - Throw an exception when Query\Builder::where is called with only a column name
1 parent 0fb83af commit b9bbcdd

File tree

3 files changed

+205
-3
lines changed

3 files changed

+205
-3
lines changed

‎src/Query/Builder.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
925925
}
926926
}
927927

928+
if (func_num_args() == 1 && is_string($column)) {
929+
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));
930+
}
931+
928932
return parent::where(...$params);
929933
}
930934

@@ -1378,4 +1382,28 @@ public function havingBetween($column, iterable $values, $boolean = 'and', $not
13781382
{
13791383
throw new \BadMethodCallException('This method is not supported by MongoDB');
13801384
}
1385+
1386+
/** @internal This method is not supported by MongoDB. */
1387+
public function whereIntegerInRaw($column, $values, $boolean = 'and', $not = false)
1388+
{
1389+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1390+
}
1391+
1392+
/** @internal This method is not supported by MongoDB. */
1393+
public function orWhereIntegerInRaw($column, $values)
1394+
{
1395+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1396+
}
1397+
1398+
/** @internal This method is not supported by MongoDB. */
1399+
public function whereIntegerNotInRaw($column, $values, $boolean = 'and')
1400+
{
1401+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1402+
}
1403+
1404+
/** @internal This method is not supported by MongoDB. */
1405+
public function orWhereIntegerNotInRaw($column, $values, $boolean = 'and')
1406+
{
1407+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1408+
}
13811409
}

‎tests/Query/BuilderTest.php‎

Lines changed: 176 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,36 @@ public static function provideQueryBuilderToMql(): iterable
4545
*/
4646
$date = new DateTimeImmutable('2016年07月12日 15:30:00');
4747

48-
yield 'find' => [
48+
yield 'select replaces previous select' => [
49+
['find' => [[], ['projection' => ['bar' => 1]]]],
50+
fn (Builder $builder) => $builder->select('foo')->select('bar'),
51+
];
52+
53+
yield 'select array' => [
54+
['find' => [[], ['projection' => ['foo' => 1, 'bar' => 1]]]],
55+
fn (Builder $builder) => $builder->select(['foo', 'bar']),
56+
];
57+
58+
/** @see DatabaseQueryBuilderTest::testAddingSelects */
59+
yield 'addSelect' => [
60+
['find' => [[], ['projection' => ['foo' => 1, 'bar' => 1, 'baz' => 1, 'boom' => 1]]]],
61+
fn (Builder $builder) => $builder->select('foo')
62+
->addSelect('bar')
63+
->addSelect(['baz', 'boom'])
64+
->addSelect('bar'),
65+
];
66+
67+
yield 'select all' => [
68+
['find' => [[], []]],
69+
fn (Builder $builder) => $builder->select('*'),
70+
];
71+
72+
yield 'find all with select' => [
73+
['find' => [[], ['projection' => ['foo' => 1, 'bar' => 1]]]],
74+
fn (Builder $builder) => $builder->select('foo', 'bar'),
75+
];
76+
77+
yield 'find equals' => [
4978
['find' => [['foo' => 'bar'], []]],
5079
fn (Builder $builder) => $builder->where('foo', 'bar'),
5180
];
@@ -66,11 +95,55 @@ public static function provideQueryBuilderToMql(): iterable
6695
fn (Builder $builder) => $builder->where('foo', '>', $date),
6796
];
6897

69-
yield 'find in array' => [
98+
/** @see DatabaseQueryBuilderTest::testBasicWhereIns */
99+
yield 'whereIn' => [
70100
['find' => [['foo' => ['$in' => ['bar', 'baz']]], []]],
71101
fn (Builder $builder) => $builder->whereIn('foo', ['bar', 'baz']),
72102
];
73103

104+
// Nested array are not flattened like in the Eloquent builder. MongoDB can compare objects.
105+
$array = [['issue' => 45582], ['id' => 2], [3]];
106+
yield 'whereIn nested array' => [
107+
['find' => [['id' => ['$in' => $array]], []]],
108+
fn (Builder $builder) => $builder->whereIn('id', $array),
109+
];
110+
111+
yield 'orWhereIn' => [
112+
['find' => [
113+
['$or' => [
114+
['id' => 1],
115+
['id' => ['$in' => [1, 2, 3]]],
116+
]],
117+
[], // options
118+
]],
119+
fn (Builder $builder) => $builder->where('id', '=', 1)
120+
->orWhereIn('id', [1, 2, 3]),
121+
];
122+
123+
/** @see DatabaseQueryBuilderTest::testBasicWhereNotIns */
124+
yield 'whereNotIn' => [
125+
['find' => [['id' => ['$nin' => [1, 2, 3]]], []]],
126+
fn (Builder $builder) => $builder->whereNotIn('id', [1, 2, 3]),
127+
];
128+
129+
yield 'orWhereNotIn' => [
130+
['find' => [
131+
['$or' => [
132+
['id' => 1],
133+
['id' => ['$nin' => [1, 2, 3]]],
134+
]],
135+
[], // options
136+
]],
137+
fn (Builder $builder) => $builder->where('id', '=', 1)
138+
->orWhereNotIn('id', [1, 2, 3]),
139+
];
140+
141+
/** @see DatabaseQueryBuilderTest::testEmptyWhereIns */
142+
yield 'whereIn empty array' => [
143+
['find' => [['id' => ['$in' => []]], []]],
144+
fn (Builder $builder) => $builder->whereIn('id', []),
145+
];
146+
74147
yield 'find limit offset select' => [
75148
['find' => [[], ['limit' => 10, 'skip' => 5, 'projection' => ['foo' => 1, 'bar' => 1]]]],
76149
fn (Builder $builder) => $builder->limit(10)->offset(5)->select('foo', 'bar'),
@@ -266,6 +339,43 @@ public static function provideQueryBuilderToMql(): iterable
266339
fn (Builder $builder) => $builder->forPage(3, 20),
267340
];
268341

342+
/** @see DatabaseQueryBuilderTest::testLimitsAndOffsets() */
343+
yield 'offset limit' => [
344+
['find' => [[], ['skip' => 5, 'limit' => 10]]],
345+
fn (Builder $builder) => $builder->offset(5)->limit(10),
346+
];
347+
348+
yield 'offset limit zero (unset)' => [
349+
['find' => [[], []]],
350+
fn (Builder $builder) => $builder
351+
->offset(0)->limit(0),
352+
];
353+
354+
yield 'offset limit zero (reset)' => [
355+
['find' => [[], []]],
356+
fn (Builder $builder) => $builder
357+
->offset(5)->limit(10)
358+
->offset(0)->limit(0),
359+
];
360+
361+
yield 'offset limit negative (unset)' => [
362+
['find' => [[], []]],
363+
fn (Builder $builder) => $builder
364+
->offset(-5)->limit(-10),
365+
];
366+
367+
yield 'offset limit null (reset)' => [
368+
['find' => [[], []]],
369+
fn (Builder $builder) => $builder
370+
->offset(5)->limit(10)
371+
->offset(null)->limit(null),
372+
];
373+
374+
yield 'skip take (aliases)' => [
375+
['find' => [[], ['skip' => 5, 'limit' => 10]]],
376+
fn (Builder $builder) => $builder->skip(5)->limit(10),
377+
];
378+
269379
/** @see DatabaseQueryBuilderTest::testOrderBys() */
270380
yield 'orderBy multiple columns' => [
271381
['find' => [[], ['sort' => ['email' => 1, 'age' => -1]]]],
@@ -452,11 +562,57 @@ function (Builder $builder) {
452562
->orWhereNotBetween('id', collect([3, 4])),
453563
];
454564

565+
/** @see DatabaseQueryBuilderTest::testBasicSelectDistinct */
455566
yield 'distinct' => [
456567
['distinct' => ['foo', [], []]],
457568
fn (Builder $builder) => $builder->distinct('foo'),
458569
];
459570

571+
yield 'select distinct' => [
572+
['distinct' => ['foo', [], []]],
573+
fn (Builder $builder) => $builder->select('foo', 'bar')
574+
->distinct(),
575+
];
576+
577+
/** @see DatabaseQueryBuilderTest::testBasicSelectDistinctOnColumns */
578+
yield 'select distinct on' => [
579+
['distinct' => ['foo', [], []]],
580+
fn (Builder $builder) => $builder->distinct('foo')
581+
->select('foo', 'bar'),
582+
];
583+
584+
/** @see DatabaseQueryBuilderTest::testLatest() */
585+
yield 'latest' => [
586+
['find' => [[], ['sort' => ['created_at' => -1]]]],
587+
fn (Builder $builder) => $builder->latest(),
588+
];
589+
590+
yield 'latest limit' => [
591+
['find' => [[], ['sort' => ['created_at' => -1], 'limit' => 1]]],
592+
fn (Builder $builder) => $builder->latest()->limit(1),
593+
];
594+
595+
yield 'latest custom field' => [
596+
['find' => [[], ['sort' => ['updated_at' => -1]]]],
597+
fn (Builder $builder) => $builder->latest('updated_at'),
598+
];
599+
600+
/** @see DatabaseQueryBuilderTest::testOldest() */
601+
yield 'oldest' => [
602+
['find' => [[], ['sort' => ['created_at' => 1]]]],
603+
fn (Builder $builder) => $builder->oldest(),
604+
];
605+
606+
yield 'oldest limit' => [
607+
['find' => [[], ['sort' => ['created_at' => 1], 'limit' => 1]]],
608+
fn (Builder $builder) => $builder->oldest()->limit(1),
609+
];
610+
611+
yield 'oldest custom field' => [
612+
['find' => [[], ['sort' => ['updated_at' => 1]]]],
613+
fn (Builder $builder) => $builder->oldest('updated_at'),
614+
];
615+
460616
yield 'groupBy' => [
461617
['aggregate' => [
462618
[['$group' => ['_id' => ['foo' => '$foo'], 'foo' => ['$last' => '$foo']]]],
@@ -516,6 +672,12 @@ public static function provideExceptions(): iterable
516672
'Between $values must be a list with exactly two elements: [min, max]',
517673
fn (Builder $builder) => $builder->whereBetween('id', ['min' => 1, 'max' => 2]),
518674
];
675+
676+
yield 'find with single string argument' => [
677+
\ArgumentCountError::class,
678+
'Too few arguments to function Jenssegers\Mongodb\Query\Builder::where("foo"), 1 passed and at least 2 expected when the 1st is a string',
679+
fn (Builder $builder) => $builder->where('foo'),
680+
];
519681
}
520682

521683
/** @dataProvider getEloquentMethodsNotSupported */
@@ -562,6 +724,18 @@ public static function getEloquentMethodsNotSupported()
562724
yield 'having' => [fn (Builder $builder) => $builder->having('baz', '=', 1)];
563725
yield 'havingBetween' => [fn (Builder $builder) => $builder->havingBetween('last_login_date', ['2018年11月16日', '2018年12月16日'])];
564726
yield 'orHavingRaw' => [fn (Builder $builder) => $builder->orHavingRaw('user_foo < user_bar')];
727+
728+
/** @see DatabaseQueryBuilderTest::testWhereIntegerInRaw */
729+
yield 'whereIntegerInRaw' => [fn (Builder $builder) => $builder->whereIntegerInRaw('id', ['1a', 2])];
730+
731+
/** @see DatabaseQueryBuilderTest::testOrWhereIntegerInRaw */
732+
yield 'orWhereIntegerInRaw' => [fn (Builder $builder) => $builder->orWhereIntegerInRaw('id', ['1a', 2])];
733+
734+
/** @see DatabaseQueryBuilderTest::testWhereIntegerNotInRaw */
735+
yield 'whereIntegerNotInRaw' => [fn (Builder $builder) => $builder->whereIntegerNotInRaw('id', ['1a', 2])];
736+
737+
/** @see DatabaseQueryBuilderTest::testOrWhereIntegerNotInRaw */
738+
yield 'orWhereIntegerNotInRaw' => [fn (Builder $builder) => $builder->orWhereIntegerNotInRaw('id', ['1a', 2])];
565739
}
566740

567741
private static function getBuilder(): Builder

‎tests/TransactionTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public function testTransaction(): void
332332
$count = User::count();
333333
$this->assertEquals(2, $count);
334334

335-
$this->assertTrue(User::where('alcaeus')->exists());
335+
$this->assertTrue(User::where('name', 'alcaeus')->exists());
336336
$this->assertTrue(User::where(['name' => 'klinson'])->where('age', 21)->exists());
337337
}
338338

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /