trove/packages/Lvl0/FediDiscover/tests/Unit/InstanceConfigTest.php

121 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Unit;
use Lvl0\FediDiscover\Config\InstanceConfig;
use Lvl0\FediDiscover\Config\InstanceType;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class InstanceConfigTest extends TestCase
{
public function test_from_array_returns_instance_config_with_correct_field_values(): void
{
$config = InstanceConfig::fromArray([
'type' => 'mastodon',
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'extras' => ['token' => 'abc123'],
]);
$this->assertSame(InstanceType::Mastodon, $config->type);
$this->assertSame('https://mastodon.social', $config->url);
$this->assertTrue($config->enabled);
$this->assertSame(600, $config->intervalSeconds);
$this->assertSame(['token' => 'abc123'], $config->extras);
}
public function test_from_array_rejects_non_positive_interval_seconds(): void
{
$this->expectException(\InvalidArgumentException::class);
InstanceConfig::fromArray([
'type' => 'mastodon',
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 0,
'extras' => [],
]);
}
public function test_extras_defaults_to_empty_array_when_omitted(): void
{
$config = InstanceConfig::fromArray([
'type' => 'mastodon',
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
]);
$this->assertSame([], $config->extras);
}
#[DataProvider('requiredKeyProvider')]
public function test_from_array_throws_when_required_key_is_missing(string $missingKey): void
{
$input = [
'type' => 'mastodon',
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
];
unset($input[$missingKey]);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/' . preg_quote($missingKey, '/') . '/');
InstanceConfig::fromArray($input);
}
public static function requiredKeyProvider(): array
{
return [
'type missing' => ['type'],
'url missing' => ['url'],
'enabled missing' => ['enabled'],
'interval_seconds missing' => ['interval_seconds'],
];
}
public function test_from_array_throws_invalid_argument_exception_for_unknown_type_string(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/pleroma/');
InstanceConfig::fromArray([
'type' => 'pleroma',
'url' => 'https://pleroma.example.com',
'enabled' => true,
'interval_seconds' => 600,
]);
}
public function test_from_array_rejects_malformed_url(): void
{
$this->expectException(\InvalidArgumentException::class);
InstanceConfig::fromArray([
'type' => 'mastodon',
'url' => 'not a url',
'enabled' => true,
'interval_seconds' => 600,
]);
}
public function test_to_array_produces_array_that_round_trips_through_from_array(): void
{
$original = [
'type' => 'mastodon',
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'extras' => ['token' => 'abc123'],
];
$this->assertSame($original, InstanceConfig::fromArray($original)->toArray());
}
}