Skip to content
2 changes: 1 addition & 1 deletion src/Sql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ protected function processOrder(
}

$orders[] = [
$platform->quoteIdentifierInFragment($k),
IdentifierQuoter::quote($k, $platform),
strcasecmp(trim($v), self::ORDER_DESCENDING) === 0
? self::ORDER_DESCENDING
: self::ORDER_ASCENDING,
Expand Down
169 changes: 169 additions & 0 deletions test/unit/Sql/SelectOrderNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

declare(strict_types=1);

namespace PhpDbTest\Sql;

use PhpDb\Adapter\Platform\PlatformInterface;
use PhpDb\Sql\Exception\InvalidArgumentException;
use PhpDb\Sql\Expression;
use PhpDb\Sql\ExpressionInterface;
use PhpDb\Sql\Select;
use PhpDbTest\TestAsset\TestSql92Platform;
use PhpDbTest\TestAsset\TrustingHyphenWordPlatform;
use PhpDbTest\TestAsset\TrustingSql92Platform;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[Group('unit')]
#[CoversMethod(Select::class, 'order')]
#[CoversMethod(Select::class, 'processOrder')]
final class SelectOrderNameTest extends TestCase
{
/**
* @return array<string, array{ExpressionInterface|array<int|string, string|ExpressionInterface>|string, string}>
*/
public static function brokenOrderProvider(): array
{
return [
'hyphen' => ['x-y', '"x-y" ASC'],
'hyphen descending' => ['x-y DESC', '"x-y" DESC'],
'dotted hyphen' => ['t-1.x-y DESC', '"t-1"."x-y" DESC'],
'quote character' => ['a"b DESC', '"a\'b" DESC'],
'non-ascii' => ['café DESC', '"café" DESC'],
'hyphen in comma list' => ['x-y DESC, b', '"x-y" DESC, "b" ASC'],
'hyphen array key' => [['x-y' => 'DESC'], '"x-y" DESC'],
'space array key' => [['my col' => 'DESC'], '"my col" DESC'],
'dotted space array key' => [['t.my col' => 'ASC'], '"t"."my col" ASC'],
'quote character array key' => [['a"b' => 'DESC'], '"a\'b" DESC'],
'non-ascii array key' => [['café' => 'DESC'], '"café" DESC'],
'hyphen array list' => [['x-y DESC', 'b'], '"x-y" DESC, "b" ASC'],
'hyphen qualified star' => ['x-y.* DESC', '"x-y".* DESC'],
'as after qualifier' => [['t.as' => 'DESC'], '"t"."as" DESC'],
];
}

/**
* @return array<string, array{PlatformInterface, string, string}>
*/
public static function platformOrderProvider(): array
{
return [
'hyphen word character' => [new TrustingHyphenWordPlatform(), 'x-y DESC', '`x-y` DESC'],
'hyphen word character arithmetic' => [
new TrustingHyphenWordPlatform(),
'price*x-y DESC',
'`price`*`x-y` DESC',
],
'identifier quoting off' => [
new TestSql92Platform(quoteIdentifiers: false),
'x-y DESC',
'x-y DESC',
],
'identifier quoting off arithmetic' => [
new TestSql92Platform(quoteIdentifiers: false),
'price*qty DESC',
'price*qty DESC',
],
];
}

/**
* @return array<string, array{PlatformInterface, array<int|string, string>|string, string}>
*/
public static function unquotableOrderProvider(): array
{
return [
'arithmetic with hyphen' => [new TrustingSql92Platform(), 'price*x-y DESC', 'price*x-y'],
'function call' => [new TrustingSql92Platform(), ['COUNT(id)' => 'DESC'], 'COUNT(id)'],
'plus on hyphen platform' => [new TrustingHyphenWordPlatform(), 'x+y DESC', 'x+y'],
];
}

/**
* @return array<string, array{ExpressionInterface|array<int|string, string|ExpressionInterface>|string, string}>
*/
public static function validOrderProvider(): array
{
return [
'plain' => ['a', '"a" ASC'],
'plain descending' => ['a DESC', '"a" DESC'],
'lower case descending' => ['a desc', '"a" DESC'],
'dotted' => ['t.col DESC', '"t"."col" DESC'],
'comma list' => ['a DESC, b', '"a" DESC, "b" ASC'],
'arithmetic' => ['price*qty DESC', '"price"*"qty" DESC'],
'string with space' => ['my col DESC', '"my" ASC'],
'array key' => [['a' => 'DESC'], '"a" DESC'],
'array key unknown direction' => [['a' => 'sideways'], '"a" ASC'],
'spaced arithmetic key' => [['price * qty' => 'DESC'], '"price" * "qty" DESC'],
'dotted arithmetic key' => [['t.price*t.qty' => 'DESC'], '"t"."price"*"t"."qty" DESC'],
'trailing space key' => [['a ' => 'DESC'], '"a" DESC'],
'array list' => [['a DESC', 'b'], '"a" DESC, "b" ASC'],
'expression' => [new Expression('RAND()'), 'RAND()'],
'expression in array' => [['a', new Expression('RAND()')], '"a" ASC, RAND()'],
];
}

#[Test]
#[DataProvider('platformOrderProvider')]
public function followsThePlatformFragmentRulesForOrderNames(
PlatformInterface $platform,
string $order,
string $expected,
): void {
$select = (new Select('foo'))->order($order);

self::assertStringEndsWith(" ORDER BY {$expected}", $select->getSqlString($platform));
}

/**
* @param ExpressionInterface|array<int|string, string|ExpressionInterface>|string $order
*/
#[Test]
#[DataProvider('brokenOrderProvider')]
public function quotesOrderNameAsIdentifierChainWhenFragmentQuotingWouldBreakIt(
ExpressionInterface|array|string $order,
string $expected,
): void {
self::assertSame("SELECT \"foo\".* FROM \"foo\" ORDER BY {$expected}", $this->render($order));
}

/**
* @param array<int|string, string>|string $order
*/
#[Test]
#[DataProvider('unquotableOrderProvider')]
public function rejectsOrderNamesHoldingExpressionSyntax(
PlatformInterface $platform,
array|string $order,
string $name,
): void {
$select = (new Select('foo'))->order($order);

$this->expectExceptionObject(InvalidArgumentException::forUnquotableIdentifier($name));

$select->getSqlString($platform);
}

/**
* @param ExpressionInterface|array<int|string, string|ExpressionInterface>|string $order
*/
#[Test]
#[DataProvider('validOrderProvider')]
public function rendersValidOrderUnchanged(ExpressionInterface|array|string $order, string $expected): void
{
self::assertSame("SELECT \"foo\".* FROM \"foo\" ORDER BY {$expected}", $this->render($order));
}

/**
* @param ExpressionInterface|array<int|string, string|ExpressionInterface>|string $order
*/
private function render(ExpressionInterface|array|string $order): string
{
return (new Select('foo'))->order($order)
->getSqlString(new TrustingSql92Platform());
}
}
Loading