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

Commit 74e85f3

Browse files
committed
PHPORM-51 Throw an exception when unsupported query builder method is used
1 parent 8562a4b commit 74e85f3

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/Query/Builder.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,4 +1303,63 @@ public function __call($method, $parameters)
13031303

13041304
return parent::__call($method, $parameters);
13051305
}
1306+
1307+
public function toSql()
1308+
{
1309+
throw new \BadMethodCallException('This method is not supported by MongoDB. Try "toMql()" instead.');
1310+
}
1311+
1312+
/** @internal This method is not supported by MongoDB. */
1313+
public function whereColumn($first, $operator = null, $second = null, $boolean = 'and')
1314+
{
1315+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1316+
}
1317+
1318+
/** @internal This method is not supported by MongoDB. */
1319+
public function whereFullText($columns, $value, array $options = [], $boolean = 'and')
1320+
{
1321+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1322+
}
1323+
1324+
/** @internal This method is not supported by MongoDB. */
1325+
public function groupByRaw($sql, array $bindings = [])
1326+
{
1327+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1328+
}
1329+
1330+
/** @internal This method is not supported by MongoDB. */
1331+
public function orderByRaw($sql, $bindings = [])
1332+
{
1333+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1334+
}
1335+
1336+
/** @internal This method is not supported by MongoDB. */
1337+
public function unionAll($query)
1338+
{
1339+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1340+
}
1341+
1342+
/** @internal This method is not supported by MongoDB. */
1343+
public function union($query, $all = false)
1344+
{
1345+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1346+
}
1347+
1348+
/** @internal This method is not supported by MongoDB. */
1349+
public function having($column, $operator = null, $value = null, $boolean = 'and')
1350+
{
1351+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1352+
}
1353+
1354+
/** @internal This method is not supported by MongoDB. */
1355+
public function havingRaw($sql, array $bindings = [], $boolean = 'and')
1356+
{
1357+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1358+
}
1359+
1360+
/** @internal This method is not supported by MongoDB. */
1361+
public function havingBetween($column, iterable $values, $boolean = 'and', $not = false)
1362+
{
1363+
throw new \BadMethodCallException('This method is not supported by MongoDB');
1364+
}
13061365
}

tests/Query/BuilderTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,52 @@ public static function provideExceptions(): iterable
156156
];
157157
}
158158

159+
/** @dataProvider getEloquentMethodsNotSupported */
160+
public function testEloquentMethodsNotSupported(\Closure $callback)
161+
{
162+
$builder = self::getBuilder();
163+
164+
$this->expectException(\BadMethodCallException::class);
165+
$this->expectExceptionMessage('This method is not supported by MongoDB');
166+
167+
$callback($builder);
168+
}
169+
170+
public static function getEloquentMethodsNotSupported()
171+
{
172+
// Most of this methods can be implemented using aggregation framework
173+
// whereInRaw, whereNotInRaw, orWhereInRaw, orWhereNotInRaw, whereBetweenColumns
174+
175+
yield 'toSql' => [fn (Builder $builder) => $builder->toSql()];
176+
yield 'toRawSql' => [fn (Builder $builder) => $builder->toRawSql()];
177+
178+
/** @see DatabaseQueryBuilderTest::testBasicWhereColumn() */
179+
/** @see DatabaseQueryBuilderTest::testArrayWhereColumn() */
180+
yield 'whereColumn' => [fn (Builder $builder) => $builder->whereColumn('first_name', 'last_name')];
181+
yield 'orWhereColumn' => [fn (Builder $builder) => $builder->orWhereColumn('first_name', 'last_name')];
182+
183+
/** @see DatabaseQueryBuilderTest::testWhereFulltextMySql() */
184+
yield 'whereFulltext' => [fn (Builder $builder) => $builder->whereFulltext('body', 'Hello World')];
185+
186+
/** @see DatabaseQueryBuilderTest::testGroupBys() */
187+
yield 'groupByRaw' => [fn (Builder $builder) => $builder->groupByRaw('DATE(created_at)')];
188+
189+
/** @see DatabaseQueryBuilderTest::testOrderBys() */
190+
yield 'orderByRaw' => [fn (Builder $builder) => $builder->orderByRaw('"age" ? desc', ['foo'])];
191+
192+
/** @see DatabaseQueryBuilderTest::testInRandomOrderMySql */
193+
yield 'inRandomOrder' => [fn (Builder $builder) => $builder->inRandomOrder()];
194+
195+
yield 'union' => [fn (Builder $builder) => $builder->union($builder)];
196+
yield 'unionAll' => [fn (Builder $builder) => $builder->unionAll($builder)];
197+
198+
/** @see DatabaseQueryBuilderTest::testRawHavings */
199+
yield 'havingRaw' => [fn (Builder $builder) => $builder->havingRaw('user_foo < user_bar')];
200+
yield 'having' => [fn (Builder $builder) => $builder->having('baz', '=', 1)];
201+
yield 'havingBetween' => [fn (Builder $builder) => $builder->havingBetween('last_login_date', ['2018-11-16', '2018-12-16'])];
202+
yield 'orHavingRaw' => [fn (Builder $builder) => $builder->orHavingRaw('user_foo < user_bar')];
203+
}
204+
159205
private static function getBuilder(): Builder
160206
{
161207
$connection = m::mock(Connection::class);

0 commit comments

Comments
 (0)