Skip to content

Commit e1a83f4

Browse files
GromNaNalcaeus
authored andcommitted
PHPORM-51 Throw an exception when unsupported query builder method is used (#9)
1 parent edd0871 commit e1a83f4

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

Diff for: src/Query/Builder.php

+66
Original file line numberDiff line numberDiff line change
@@ -1303,4 +1303,70 @@ public function __call($method, $parameters)
13031303

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

Diff for: tests/Query/BuilderTest.php

+46
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)