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
12 changes: 12 additions & 0 deletions src/Providers/DTO/ProviderMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WordPress\AiClient\Providers\DTO;

use WordPress\AiClient\Common\AbstractDataTransferObject;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Providers\Enums\ProviderTypeEnum;
use WordPress\AiClient\Providers\Http\Enums\RequestAuthenticationMethod;

Expand Down Expand Up @@ -89,6 +90,7 @@ class ProviderMetadata extends AbstractDataTransferObject
* @param RequestAuthenticationMethod|null $authenticationMethod The authentication method.
* @param string|null $description The provider's description.
* @param string|null $logoPath The full path to the provider's logo image file.
* @throws InvalidArgumentException If the provider ID contains invalid characters.
*/
public function __construct(
string $id,
Expand All @@ -99,6 +101,16 @@ public function __construct(
?string $description = null,
?string $logoPath = null
) {
if (!preg_match('/^[a-z0-9\-_]+$/', $id)) {
throw new InvalidArgumentException(
sprintf(
// phpcs:ignore Generic.Files.LineLength.TooLong
'Invalid provider ID "%s". Only lowercase alphanumeric characters, hyphens, and underscores are allowed.',
Comment thread
felixarntz marked this conversation as resolved.
$id
)
);
}

$this->id = $id;
$this->name = $name;
$this->description = $description;
Expand Down
61 changes: 53 additions & 8 deletions tests/unit/Providers/DTO/ProviderMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use WordPress\AiClient\Common\Contracts\WithArrayTransformationInterface;
use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Providers\DTO\ProviderMetadata;
use WordPress\AiClient\Providers\Enums\ProviderTypeEnum;

Expand Down Expand Up @@ -264,20 +265,64 @@ public function testSpecialCharactersInNames(): void
}

/**
* Tests with empty strings.
* Tests that empty string ID throws an exception.
*
* @return void
*/
public function testEmptyStrings(): void
public function testEmptyIdThrowsException(): void
{
$metadata = new ProviderMetadata('', '', ProviderTypeEnum::cloud());
$this->expectException(InvalidArgumentException::class);
new ProviderMetadata('', '', ProviderTypeEnum::cloud());
}

$this->assertEquals('', $metadata->getId());
$this->assertEquals('', $metadata->getName());
/**
* Tests that uppercase characters in ID throw an exception.
*
* @return void
*/
public function testUppercaseIdThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);
new ProviderMetadata('OpenAI', 'OpenAI', ProviderTypeEnum::cloud());
}

$array = $metadata->toArray();
$this->assertEquals('', $array[ProviderMetadata::KEY_ID]);
$this->assertEquals('', $array[ProviderMetadata::KEY_NAME]);
/**
* Tests that spaces in ID throw an exception.
*
* @return void
*/
public function testSpacesInIdThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);
new ProviderMetadata('my provider', 'My Provider', ProviderTypeEnum::cloud());
}

/**
* Tests that special characters in ID throw an exception.
*
* @return void
*/
public function testSpecialCharsInIdThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);
new ProviderMetadata('provider!', 'Provider', ProviderTypeEnum::cloud());
}

/**
* Tests that valid IDs with hyphens and underscores are accepted.
*
* @return void
*/
public function testValidIdFormats(): void
{
$metadata1 = new ProviderMetadata('my-provider', 'My Provider', ProviderTypeEnum::cloud());
$this->assertEquals('my-provider', $metadata1->getId());

$metadata2 = new ProviderMetadata('provider_v2', 'Provider V2', ProviderTypeEnum::cloud());
$this->assertEquals('provider_v2', $metadata2->getId());

$metadata3 = new ProviderMetadata('a1-b2_c3', 'Test', ProviderTypeEnum::cloud());
$this->assertEquals('a1-b2_c3', $metadata3->getId());
}

/**
Expand Down
Loading