Skip to content

Commit

Permalink
PHPORM-51 Throw an exception when unsupported query builder method is…
Browse files Browse the repository at this point in the history
… used (#9)
  • Loading branch information
GromNaN authored and alcaeus committed Aug 22, 2023
1 parent edd0871 commit e1a83f4
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
46 changes: 46 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e1a83f4

Please sign in to comment.