mivita/dev/subdomain-optimization-claude/tests/Unit/OptimizedDomainServiceTest.php
2025-10-20 17:42:08 +02:00

319 lines
10 KiB
PHP

<?php
namespace Tests\Unit\Services;
use App\Domain\DomainContext;
use App\Domain\DomainType;
use App\Models\UserShop;
use App\Models\User;
use App\Services\OptimizedDomainService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
/**
* OptimizedDomainService Unit Tests
*
* Testet die Funktionalität des optimierten Domain-Services
*/
class OptimizedDomainServiceTest extends TestCase
{
use RefreshDatabase;
private OptimizedDomainService $domainService;
private array $testConfig;
protected function setUp(): void
{
parent::setUp();
$this->testConfig = [
'protocol' => 'https://',
'domains' => [
'main' => [
'host' => 'mivita.test',
'type' => 'main',
],
'shop' => [
'host' => 'mivita.shop',
'type' => 'main-shop',
'default_user_shop' => 'aloevera',
],
'crm' => [
'host' => 'my.mivita.test',
'type' => 'crm',
],
'portal' => [
'host' => 'in.mivita.test',
'type' => 'portal',
],
'checkout' => [
'host' => 'checkout.mivita.test',
'type' => 'checkout',
],
'user-shop' => [
'host' => '{subdomain}.mivita.test',
'type' => 'user-shop',
],
],
'reserved_subdomains' => ['my', 'in', 'checkout'],
];
$this->domainService = new OptimizedDomainService($this->testConfig);
}
public function test_parse_main_domain(): void
{
$result = $this->domainService->parseDomain('mivita.test');
$this->assertEquals('main', $result['type']);
$this->assertEquals('mivita.test', $result['host']);
$this->assertEquals('mivita', $result['domain']);
$this->assertEquals('.test', $result['tld']);
$this->assertNull($result['subdomain']);
}
public function test_parse_user_shop_domain(): void
{
$result = $this->domainService->parseDomain('berater.mivita.test');
$this->assertEquals('user-shop', $result['type']);
$this->assertEquals('berater.mivita.test', $result['host']);
$this->assertEquals('berater', $result['subdomain']);
}
public function test_parse_reserved_subdomain(): void
{
$crmResult = $this->domainService->parseDomain('my.mivita.test');
$portalResult = $this->domainService->parseDomain('in.mivita.test');
$this->assertEquals('crm', $crmResult['type']);
$this->assertEquals('portal', $portalResult['type']);
}
public function test_parse_invalid_domain(): void
{
$result = $this->domainService->parseDomain('invalid');
$this->assertEquals('unknown', $result['type']);
$this->assertEquals('invalid', $result['host']);
}
public function test_resolve_domain_with_user_shop(): void
{
// UserShop und User erstellen
$user = User::factory()->create([
'payment_shop' => now()->addMonths(1),
]);
$userShop = UserShop::factory()->create([
'slug' => 'testshop',
'user_id' => $user->id,
'active' => true,
]);
$context = $this->domainService->resolveDomain('testshop.mivita.test');
$this->assertEquals(DomainType::USER_SHOP, $context->type);
$this->assertEquals('testshop.mivita.test', $context->host);
$this->assertEquals('testshop', $context->subdomain);
$this->assertNotNull($context->userShop);
$this->assertEquals($userShop->id, $context->userShop->id);
}
public function test_resolve_domain_with_inactive_user_shop(): void
{
// Inaktiver UserShop
UserShop::factory()->create([
'slug' => 'inactive-shop',
'active' => false,
]);
$context = $this->domainService->resolveDomain('inactive-shop.mivita.test');
$this->assertEquals(DomainType::UNKNOWN, $context->type);
$this->assertNull($context->userShop);
}
public function test_build_url_for_main_domain(): void
{
$url = $this->domainService->buildUrl('main');
$this->assertEquals('https://mivita.test', $url);
$urlWithPath = $this->domainService->buildUrl('main', 'some/path');
$this->assertEquals('https://mivita.test/some/path', $urlWithPath);
}
public function test_build_url_for_user_shop(): void
{
$url = $this->domainService->buildUrl('user-shop', null, 'myshop');
$this->assertEquals('https://myshop.mivita.test', $url);
$urlWithPath = $this->domainService->buildUrl('user-shop', 'products', 'myshop');
$this->assertEquals('https://myshop.mivita.test/products', $urlWithPath);
}
public function test_build_url_throws_exception_for_unknown_type(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->domainService->buildUrl('unknown-type');
}
public function test_build_url_throws_exception_for_user_shop_without_slug(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->domainService->buildUrl('user-shop');
}
public function test_is_valid_user_shop(): void
{
$user = User::factory()->create([
'payment_shop' => now()->addMonths(1),
]);
$userShop = UserShop::factory()->create([
'slug' => 'validshop',
'user_id' => $user->id,
'active' => true,
]);
$this->assertTrue($this->domainService->isValidUserShop('validshop'));
$this->assertFalse($this->domainService->isValidUserShop('nonexistent'));
}
public function test_get_user_shop(): void
{
$user = User::factory()->create([
'payment_shop' => now()->addMonths(1),
]);
$userShop = UserShop::factory()->create([
'slug' => 'getshop',
'user_id' => $user->id,
'active' => true,
]);
$result = $this->domainService->getUserShop('getshop');
$this->assertNotNull($result);
$this->assertEquals($userShop->id, $result->id);
$nonExistent = $this->domainService->getUserShop('nonexistent');
$this->assertNull($nonExistent);
}
public function test_cache_functionality(): void
{
Cache::flush();
// Erstes Laden sollte DB-Query ausführen
$result1 = $this->domainService->parseDomain('test.mivita.test');
// Zweites Laden sollte aus Cache kommen
$result2 = $this->domainService->parseDomain('test.mivita.test');
$this->assertEquals($result1, $result2);
}
public function test_clear_user_shop_cache(): void
{
$user = User::factory()->create([
'payment_shop' => now()->addMonths(1),
]);
$userShop = UserShop::factory()->create([
'slug' => 'cacheshop',
'user_id' => $user->id,
'active' => true,
]);
// UserShop laden (wird gecacht)
$this->domainService->getUserShop('cacheshop');
// Cache löschen
$this->domainService->clearUserShopCache('cacheshop');
// Erneut laden sollte DB-Query ausführen
$result = $this->domainService->getUserShop('cacheshop');
$this->assertNotNull($result);
}
public function test_validate_configuration(): void
{
// Gültige Konfiguration
$errors = $this->domainService->validateConfiguration();
$this->assertEmpty($errors);
// Ungültige Konfiguration
$invalidService = new OptimizedDomainService([
'domains' => [],
]);
$errors = $invalidService->validateConfiguration();
$this->assertNotEmpty($errors);
}
public function test_get_default_user_shop(): void
{
$user = User::factory()->create([
'payment_shop' => now()->addMonths(1),
]);
UserShop::factory()->create([
'slug' => 'aloevera',
'user_id' => $user->id,
'active' => true,
]);
$defaultShop = $this->domainService->getDefaultUserShop();
$this->assertNotNull($defaultShop);
$this->assertEquals('aloevera', $defaultShop->slug);
}
public function test_warm_up_cache(): void
{
$user = User::factory()->create([
'payment_shop' => now()->addMonths(1),
]);
UserShop::factory()->create([
'slug' => 'warmup-shop',
'user_id' => $user->id,
'active' => true,
]);
Cache::flush();
// Cache aufwärmen
$this->domainService->warmUpCache(['warmup-shop']);
// Überprüfen, dass Daten im Cache sind
$cachedShop = Cache::get('user_shop_warmup-shop');
$this->assertNotNull($cachedShop);
}
public function test_subdomain_validation(): void
{
// Gültige Subdomains
$validSubdomains = ['test', 'valid-shop', 'shop123'];
foreach ($validSubdomains as $subdomain) {
$result = $this->domainService->parseDomain($subdomain . '.mivita.test');
$this->assertEquals('user-shop', $result['type']);
}
// Ungültige Subdomains
$invalidSubdomains = ['a', 'ab', 'INVALID', 'invalid_shop', 'invalid.shop'];
foreach ($invalidSubdomains as $subdomain) {
$result = $this->domainService->parseDomain($subdomain . '.mivita.test');
$this->assertEquals('unknown', $result['type']);
}
}
public function test_reserved_subdomain_handling(): void
{
foreach ($this->testConfig['reserved_subdomains'] as $reserved) {
$result = $this->domainService->parseDomain($reserved . '.mivita.test');
$this->assertNotEquals('user-shop', $result['type']);
$this->assertNotEquals('unknown', $result['type']);
}
}
}