From 0c5459d3a8aeeccf77038b0ede157bc4271fbdbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 12 Jul 2023 15:14:22 +0200 Subject: [PATCH] PHPORM-51 Throw an exception when unsupported query builder method is used --- src/Query/Builder.php | 66 +++++++++++++++++++++++++++++++++++++ tests/Query/BuilderTest.php | 46 ++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 78aabff..0e6a826 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -1303,4 +1303,70 @@ public function __call($method, $parameters) return parent::__call($method, $parameters); } + + /** @internal This method is not supported by MongoDB. */ + public function toSql() + { + throw new \BadMethodCallException('This method is not supported by MongoDB. Try "toMql()" instead.'); + } + + /** @internal This method is not supported by MongoDB. */ + public function toRawSql() + { + throw new \BadMethodCallException('This method is not supported by MongoDB. Try "toMql()" instead.'); + } + + /** @internal This method is not supported by MongoDB. */ + public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') + { + throw new \BadMethodCallException('This method is not supported by MongoDB'); + } + + /** @internal This method is not supported by MongoDB. */ + public function whereFullText($columns, $value, array $options = [], $boolean = 'and') + { + throw new \BadMethodCallException('This method is not supported by MongoDB'); + } + + /** @internal This method is not supported by MongoDB. */ + public function groupByRaw($sql, array $bindings = []) + { + throw new \BadMethodCallException('This method is not supported by MongoDB'); + } + + /** @internal This method is not supported by MongoDB. */ + public function orderByRaw($sql, $bindings = []) + { + throw new \BadMethodCallException('This method is not supported by MongoDB'); + } + + /** @internal This method is not supported by MongoDB. */ + public function unionAll($query) + { + throw new \BadMethodCallException('This method is not supported by MongoDB'); + } + + /** @internal This method is not supported by MongoDB. */ + public function union($query, $all = false) + { + throw new \BadMethodCallException('This method is not supported by MongoDB'); + } + + /** @internal This method is not supported by MongoDB. */ + public function having($column, $operator = null, $value = null, $boolean = 'and') + { + throw new \BadMethodCallException('This method is not supported by MongoDB'); + } + + /** @internal This method is not supported by MongoDB. */ + public function havingRaw($sql, array $bindings = [], $boolean = 'and') + { + throw new \BadMethodCallException('This method is not supported by MongoDB'); + } + + /** @internal This method is not supported by MongoDB. */ + public function havingBetween($column, iterable $values, $boolean = 'and', $not = false) + { + throw new \BadMethodCallException('This method is not supported by MongoDB'); + } } diff --git a/tests/Query/BuilderTest.php b/tests/Query/BuilderTest.php index b06a89f..f7d12ad 100644 --- a/tests/Query/BuilderTest.php +++ b/tests/Query/BuilderTest.php @@ -156,6 +156,52 @@ public static function provideExceptions(): iterable ]; } + /** @dataProvider getEloquentMethodsNotSupported */ + public function testEloquentMethodsNotSupported(\Closure $callback) + { + $builder = self::getBuilder(); + + $this->expectException(\BadMethodCallException::class); + $this->expectExceptionMessage('This method is not supported by MongoDB'); + + $callback($builder); + } + + public static function getEloquentMethodsNotSupported() + { + // Most of this methods can be implemented using aggregation framework + // whereInRaw, whereNotInRaw, orWhereInRaw, orWhereNotInRaw, whereBetweenColumns + + yield 'toSql' => [fn (Builder $builder) => $builder->toSql()]; + yield 'toRawSql' => [fn (Builder $builder) => $builder->toRawSql()]; + + /** @see DatabaseQueryBuilderTest::testBasicWhereColumn() */ + /** @see DatabaseQueryBuilderTest::testArrayWhereColumn() */ + yield 'whereColumn' => [fn (Builder $builder) => $builder->whereColumn('first_name', 'last_name')]; + yield 'orWhereColumn' => [fn (Builder $builder) => $builder->orWhereColumn('first_name', 'last_name')]; + + /** @see DatabaseQueryBuilderTest::testWhereFulltextMySql() */ + yield 'whereFulltext' => [fn (Builder $builder) => $builder->whereFulltext('body', 'Hello World')]; + + /** @see DatabaseQueryBuilderTest::testGroupBys() */ + yield 'groupByRaw' => [fn (Builder $builder) => $builder->groupByRaw('DATE(created_at)')]; + + /** @see DatabaseQueryBuilderTest::testOrderBys() */ + yield 'orderByRaw' => [fn (Builder $builder) => $builder->orderByRaw('"age" ? desc', ['foo'])]; + + /** @see DatabaseQueryBuilderTest::testInRandomOrderMySql */ + yield 'inRandomOrder' => [fn (Builder $builder) => $builder->inRandomOrder()]; + + yield 'union' => [fn (Builder $builder) => $builder->union($builder)]; + yield 'unionAll' => [fn (Builder $builder) => $builder->unionAll($builder)]; + + /** @see DatabaseQueryBuilderTest::testRawHavings */ + yield 'havingRaw' => [fn (Builder $builder) => $builder->havingRaw('user_foo < user_bar')]; + yield 'having' => [fn (Builder $builder) => $builder->having('baz', '=', 1)]; + yield 'havingBetween' => [fn (Builder $builder) => $builder->havingBetween('last_login_date', ['2018-11-16', '2018-12-16'])]; + yield 'orHavingRaw' => [fn (Builder $builder) => $builder->orHavingRaw('user_foo < user_bar')]; + } + private static function getBuilder(): Builder { $connection = m::mock(Connection::class);