diff --git a/src/Sql/Sql.php b/src/Sql/Sql.php index af17e312..e5488dab 100644 --- a/src/Sql/Sql.php +++ b/src/Sql/Sql.php @@ -14,15 +14,15 @@ class Sql { protected AdapterInterface $adapter; - /** @var TableIdentifier|string|array|null */ + /** @var TableIdentifier|string|array|null */ protected TableIdentifier|string|array|null $table; protected Platform\PlatformDecoratorInterface $sqlPlatform; - /** @param TableIdentifier|string|array|null $table */ + /** @param TableIdentifier|string|array|null $table */ public function __construct( AdapterInterface $adapter, - array|string|TableIdentifier|null $table = null, + TableIdentifier|array|string|null $table = null, ) { $this->table = $table; $this->adapter = $adapter; @@ -46,7 +46,7 @@ public function buildSqlString(SqlInterface $sqlObject, ?AdapterInterface $adapt ); } - public function delete(string|TableIdentifier|null $table = null): Delete + public function delete(TableIdentifier|string|null $table = null): Delete { if (null !== $this->table && null !== $table) { throw Exception\InvalidArgumentException::forForeignTable($this->table); @@ -65,8 +65,8 @@ public function getSqlPlatform(): Platform\PlatformDecoratorInterface return $this->sqlPlatform; } - /** @return TableIdentifier|string|array|null */ - public function getTable(): array|string|TableIdentifier|null + /** @return TableIdentifier|string|array|null */ + public function getTable(): TableIdentifier|array|string|null { return $this->table; } @@ -76,7 +76,7 @@ public function hasTable(): bool return null !== $this->table; } - public function insert(string|TableIdentifier|null $table = null): Insert + public function insert(TableIdentifier|string|null $table = null): Insert { if (null !== $this->table && null !== $table) { throw Exception\InvalidArgumentException::forForeignTable($this->table); @@ -106,7 +106,11 @@ public function prepareStatementForSqlObject( return $statement; } - public function select(string|TableIdentifier|null $table = null): Select + /** + * @param TableIdentifier|string|array|null $table + * @throws Exception\InvalidArgumentException + */ + public function select(TableIdentifier|array|string|null $table = null): Select { if (null !== $this->table && null !== $table) { throw Exception\InvalidArgumentException::forForeignTable($this->table); @@ -116,17 +120,17 @@ public function select(string|TableIdentifier|null $table = null): Select } /** - * @param TableIdentifier|string|array $table + * @param TableIdentifier|string|array $table * @throws Exception\InvalidArgumentException */ - public function setTable(array|string|TableIdentifier $table): self + public function setTable(TableIdentifier|array|string $table): self { $this->table = $table; return $this; } - public function update(string|TableIdentifier|null $table = null): Update + public function update(TableIdentifier|string|null $table = null): Update { if (null !== $this->table && null !== $table) { throw Exception\InvalidArgumentException::forForeignTable($this->table); diff --git a/test/unit/Sql/SqlTest.php b/test/unit/Sql/SqlTest.php index 2c61837c..d68fdb6e 100644 --- a/test/unit/Sql/SqlTest.php +++ b/test/unit/Sql/SqlTest.php @@ -23,6 +23,7 @@ use PhpDbTest\TestAsset; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\MockObject\MockObject; @@ -41,6 +42,7 @@ #[CoversMethod(Sql::class, 'delete')] #[CoversMethod(Sql::class, 'prepareStatementForSqlObject')] #[CoversMethod(Sql::class, 'buildSqlString')] +#[Group('unit')] final class SqlTest extends TestCase { protected MockObject&Adapter $mockAdapter; @@ -229,6 +231,32 @@ public function select(): void $this->sql->select('bar'); } + #[Test] + public function selectAcceptsAliasedTableArray(): void + { + $sql = new Sql($this->mockAdapter); + + static::assertSame(['f' => 'foo'], $sql->select(['f' => 'foo'])->getRawState('table')); + } + + #[Test] + public function selectRejectsAliasedTableWhenTableIsFixed(): void + { + self::expectException(InvalidArgumentException::class); + self::expectExceptionMessage( + 'This Sql object is intended to work with only the table "foo" provided at construction time.', + ); + $this->sql->select(['f' => 'foo']); + } + + #[Test] + public function selectRendersAliasedTable(): void + { + $sql = new Sql($this->mockAdapter); + + static::assertSame('SELECT "f".* FROM "foo" AS "f"', $sql->buildSqlString($sql->select(['f' => 'foo']))); + } + #[Test] public function selectThrowsWhenTableConflicts(): void {