Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Audit/Adapter/ClickHouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,30 @@ public function setDatabase(string $database): self
return $this;
}

/**
* Set the table name for subsequent operations.
*
* @param string $table
* @return self
* @throws Exception
*/
public function setTable(string $table): self
{
$this->validateIdentifier($table, 'Table');
$this->table = $table;
return $this;
}

/**
* Get the table name (without namespace prefix).
*
* @return string
*/
public function getTable(): string
{
return $this->table;
}

/**
* Enable or disable HTTPS for ClickHouse HTTP interface.
*/
Expand Down
84 changes: 84 additions & 0 deletions tests/Audit/Adapter/ClickHouseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,90 @@ public function testSetDatabaseWithValidIdentifier(): void
$this->assertInstanceOf(ClickHouse::class, $result);
}

/**
* Test setTable validates empty identifier
*/
public function testSetTableValidatesEmpty(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Table cannot be empty');

$adapter = new ClickHouse(
host: 'clickhouse',
username: 'default',
password: 'clickhouse'
);

$adapter->setTable('');
}

/**
* Test setTable validates identifier length
*/
public function testSetTableValidatesLength(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Table cannot exceed 255 characters');

$adapter = new ClickHouse(
host: 'clickhouse',
username: 'default',
password: 'clickhouse'
);

$adapter->setTable(str_repeat('a', 256));
}

/**
* Test setTable validates identifier format
*/
public function testSetTableValidatesFormat(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Table must start with a letter or underscore');

$adapter = new ClickHouse(
host: 'clickhouse',
username: 'default',
password: 'clickhouse'
);

$adapter->setTable('123invalid');
}

/**
* Test setTable rejects SQL keywords
*/
public function testSetTableRejectsKeywords(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Table cannot be a reserved SQL keyword');

$adapter = new ClickHouse(
host: 'clickhouse',
username: 'default',
password: 'clickhouse'
);

$adapter->setTable('SELECT');
}

/**
* Test setTable with valid identifier
*/
public function testSetTableWithValidIdentifier(): void
{
$adapter = new ClickHouse(
host: 'clickhouse',
username: 'default',
password: 'clickhouse'
);

$result = $adapter->setTable('my_audit_logs');
$this->assertInstanceOf(ClickHouse::class, $result);
$this->assertEquals('my_audit_logs', $adapter->getTable());
}

/**
* Test setNamespace allows empty string
*/
Expand Down
Loading