Skip to content

Commit

Permalink
feat: orders/groups use ColumnTerm, not col name
Browse files Browse the repository at this point in the history
  • Loading branch information
mecha committed Apr 12, 2024
1 parent cab56eb commit d1805cc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 36 deletions.
5 changes: 5 additions & 0 deletions src/Expression/ColumnTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ protected function toBaseString(): string

return $result;
}

public function __toString(): string
{
return $this->toSql();
}
}
6 changes: 3 additions & 3 deletions src/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Order
public const ASC = 'ASC';
public const DESC = 'DESC';

protected string $column;
protected ColumnTerm $column;
/** @psalm-var Order::* */
protected string $sort;

Expand All @@ -23,11 +23,11 @@ class Order
*/
public function __construct($column, string $sort = self::ASC)
{
$this->column = $column instanceof ColumnTerm ? $column->getName() : $column;
$this->column = $column instanceof ColumnTerm ? $column : new ColumnTerm(null, $column);
$this->sort = $sort;
}

public function getColumn(): string
public function getColumn(): ColumnTerm
{
return $this->column;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Traits/HasGroupByTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function compileGroupBy(): string

$groupParts = [];
foreach ($this->groups as $group) {
$groupParts[] = "`{$group->getColumn()}` {$group->getSort()}";
$groupParts[] = $group->getColumn() . ' ' . $group->getSort();
}

return 'GROUP BY ' . implode(', ', $groupParts);
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Traits/HasOrderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function compileOrder(): string

$orderParts = [];
foreach ($this->order as $order) {
$orderParts[] = "`{$order->getColumn()}` {$order->getSort()}";
$orderParts[] = $order->getColumn() . ' ' . $order->getSort();
}

return 'ORDER BY ' . implode(', ', $orderParts);
Expand Down
54 changes: 23 additions & 31 deletions tests/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,30 @@

class OrderTest extends TestCase
{
public function testConstructorNoSort()
public static function provideDataForCtorTest()
{
$order = new Order($column = 'foo');

$this->assertEquals($column, $order->getColumn());
$this->assertEquals(Order::ASC, $order->getSort());
}

public function testConstructorAsc()
{
$order = new Order($column = 'foo', Order::ASC);

$this->assertEquals($column, $order->getColumn());
$this->assertEquals(Order::ASC, $order->getSort());
}

public function testConstructorDesc()
{
$order = new Order($column = 'foo', Order::DESC);

$this->assertEquals($column, $order->getColumn());
$this->assertEquals(Order::DESC, $order->getSort());
}

public function testConstructorWithTerm()
{
$columnTerm = $this->createMock(ColumnTerm::class);
$columnTerm->method('getName')->willReturn($columnName = 'foo');

$order = new Order($columnTerm);

$this->assertEquals($columnName, $order->getColumn());
return [
'no sort' => [
new Order('foo'),
new ColumnTerm(null, 'foo'),
Order::ASC
],
'asc' => [
new Order('foo', Order::ASC),
new ColumnTerm(null, 'foo'),
Order::ASC
],
'desc' => [
new Order('foo', Order::DESC),
new ColumnTerm(null, 'foo'),
Order::DESC
],
'column term' => [
new Order($ct = new ColumnTerm(null, 'foo')),
$ct,
Order::ASC
],
];
}

public function provideDataForTestDir(): array
Expand Down

0 comments on commit d1805cc

Please sign in to comment.