test(vikon): add ViconApiConfig unit tests

- token returns value from DB
- apiUrl returns value from DB
- apiUrl falls back to constant when not in payload
- token throws RuntimeException when not configured
- token throws when payload is empty
- inactive credential is ignored
This commit is contained in:
F4ilji
2026-07-10 16:51:34 +05:00
parent 73a0a6fe9c
commit 0ea2b7d9ef
@@ -0,0 +1,89 @@
<?php
namespace Tests\Feature\Vicon;
use App\Containers\Dashboard\Models\IntegrationCredential;
use App\Services\Vicon\ViconApiConfig;
use App\Ship\Enums\CacheKeys;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class ViconApiConfigTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Cache::flush();
}
private function createCredential(array $payload, bool $active = true): void
{
IntegrationCredential::create([
'provider' => 'vikon_api',
'payload' => $payload,
'is_active' => $active,
]);
}
public function test_token_returns_value_from_db(): void
{
$this->createCredential(['token' => 'test-jwt-token-123', 'api_url' => 'https://api.example.com']);
$config = app(ViconApiConfig::class);
$this->assertEquals('test-jwt-token-123', $config->token());
}
public function test_api_url_returns_value_from_db(): void
{
$this->createCredential(['token' => 'tok', 'api_url' => 'https://custom.api.com/v2']);
$config = app(ViconApiConfig::class);
$this->assertEquals('https://custom.api.com/v2', $config->apiUrl());
}
public function test_api_url_falls_back_when_not_in_payload(): void
{
$this->createCredential(['token' => 'tok']);
$config = app(ViconApiConfig::class);
$this->assertEquals('https://db-nica.ru/api/v1', $config->apiUrl());
}
public function test_token_throws_when_not_configured(): void
{
$config = app(ViconApiConfig::class);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('VIKON API token not configured');
$config->token();
}
public function test_token_throws_when_payload_empty(): void
{
$this->createCredential(['token' => '', 'api_url' => 'https://api.example.com']);
$config = app(ViconApiConfig::class);
$this->expectException(\RuntimeException::class);
$config->token();
}
public function test_inactive_credential_is_ignored(): void
{
$this->createCredential(['token' => 'inactive-tok', 'api_url' => 'https://api.example.com'], active: false);
$config = app(ViconApiConfig::class);
$this->expectException(\RuntimeException::class);
$config->token();
}
}