update 20.10.2025

This commit is contained in:
Kevin Adametz 2025-10-20 17:42:08 +02:00
parent 8c11130b5d
commit a939cd51ef
616 changed files with 84821 additions and 4121 deletions

View file

@ -0,0 +1,212 @@
<?php
namespace Tests\Unit\Domain;
use App\Domain\DomainContext;
use App\Domain\DomainType;
use App\Models\UserShop;
use Tests\TestCase;
/**
* DomainContext Unit Tests
*
* Testet die Funktionalität der erweiterten DomainContext-Klasse
*/
class DomainContextTest extends TestCase
{
public function test_can_create_domain_context_from_array(): void
{
$domainInfo = [
'type' => 'user-shop',
'host' => 'test.mivita.care',
'subdomain' => 'test',
];
$context = DomainContext::fromArray($domainInfo);
$this->assertEquals(DomainType::USER_SHOP, $context->type);
$this->assertEquals('test.mivita.care', $context->host);
$this->assertEquals('test', $context->subdomain);
$this->assertNull($context->userShop);
}
public function test_can_create_domain_context_directly(): void
{
$context = DomainContext::create(
DomainType::CRM,
'my.mivita.care',
'my'
);
$this->assertEquals(DomainType::CRM, $context->type);
$this->assertEquals('my.mivita.care', $context->host);
$this->assertEquals('my', $context->subdomain);
}
public function test_unknown_domain_type_handling(): void
{
$context = DomainContext::create(DomainType::UNKNOWN, 'invalid.domain.com');
$this->assertTrue($context->isUnknown());
$this->assertFalse($context->isValid());
}
public function test_user_shop_detection(): void
{
$userShopContext = DomainContext::create(
DomainType::USER_SHOP,
'berater.mivita.care',
'berater'
);
$this->assertTrue($userShopContext->isUserShop());
$this->assertTrue($userShopContext->shouldPreserveUserShop());
$this->assertTrue($userShopContext->isShopRelated());
}
public function test_session_domain_generation(): void
{
$shopContext = DomainContext::create(DomainType::SHOP, 'mivita.shop');
$careContext = DomainContext::create(DomainType::CRM, 'my.mivita.care');
$this->assertEquals('.mivita.shop', $shopContext->getSessionDomain('mivita', '.shop', '.care'));
$this->assertEquals('.mivita.care', $careContext->getSessionDomain('mivita', '.shop', '.care'));
}
public function test_route_file_mapping(): void
{
$contexts = [
DomainType::MAIN => 'main.php',
DomainType::SHOP => 'shop.php',
DomainType::USER_SHOP => 'user-shop.php',
DomainType::CRM => 'crm.php',
DomainType::PORTAL => 'portal.php',
DomainType::CHECKOUT => 'checkout.php',
];
foreach ($contexts as $type => $expectedFile) {
$context = DomainContext::create($type, 'test.domain.com');
$this->assertEquals($expectedFile, $context->getRouteFile());
}
}
public function test_context_with_metadata(): void
{
$metadata = ['test_key' => 'test_value'];
$context = DomainContext::create(
DomainType::MAIN,
'mivita.care',
null,
null,
$metadata
);
$this->assertEquals('test_value', $context->getMetadata('test_key'));
$this->assertNull($context->getMetadata('non_existent'));
$this->assertEquals('default', $context->getMetadata('non_existent', 'default'));
}
public function test_context_with_user_shop(): void
{
$userShop = new UserShop([
'id' => 1,
'slug' => 'test-shop',
'name' => 'Test Shop',
'active' => true,
]);
$context = DomainContext::create(
DomainType::USER_SHOP,
'test-shop.mivita.care',
'test-shop',
$userShop
);
$this->assertEquals('test-shop', $context->getUserShopSlug());
$this->assertEquals($userShop, $context->userShop);
}
public function test_context_immutability(): void
{
$original = DomainContext::create(DomainType::MAIN, 'mivita.care');
$withUserShop = $original->withUserShop(new UserShop(['slug' => 'test']));
$withType = $original->withType(DomainType::SHOP);
// Original sollte unverändert sein
$this->assertEquals(DomainType::MAIN, $original->type);
$this->assertNull($original->userShop);
// Neue Instanzen sollten geänderte Werte haben
$this->assertEquals(DomainType::MAIN, $withUserShop->type);
$this->assertNotNull($withUserShop->userShop);
$this->assertEquals(DomainType::SHOP, $withType->type);
$this->assertEquals('mivita.care', $withType->host);
}
public function test_context_equality(): void
{
$context1 = DomainContext::create(DomainType::MAIN, 'mivita.care', null);
$context2 = DomainContext::create(DomainType::MAIN, 'mivita.care', null);
$context3 = DomainContext::create(DomainType::SHOP, 'mivita.shop', null);
$this->assertTrue($context1->equals($context2));
$this->assertFalse($context1->equals($context3));
}
public function test_context_to_array(): void
{
$context = DomainContext::create(
DomainType::USER_SHOP,
'test.mivita.care',
'test'
);
$array = $context->toArray();
$this->assertArrayHasKey('type', $array);
$this->assertArrayHasKey('type_description', $array);
$this->assertArrayHasKey('host', $array);
$this->assertArrayHasKey('subdomain', $array);
$this->assertArrayHasKey('should_preserve_user_shop', $array);
$this->assertArrayHasKey('is_shop_related', $array);
$this->assertEquals('user-shop', $array['type']);
$this->assertEquals('test.mivita.care', $array['host']);
$this->assertEquals('test', $array['subdomain']);
$this->assertTrue($array['should_preserve_user_shop']);
$this->assertTrue($array['is_shop_related']);
}
public function test_context_string_representation(): void
{
$context = DomainContext::create(
DomainType::CRM,
'my.mivita.care',
'my'
);
$this->assertEquals('crm://my.mivita.care', (string) $context);
}
public function test_context_validation(): void
{
$validContext = DomainContext::create(DomainType::MAIN, 'mivita.care');
$unknownContext = DomainContext::create(DomainType::UNKNOWN, 'invalid.domain');
$this->assertTrue($validContext->isValid());
$this->assertFalse($unknownContext->isValid());
}
public function test_redirect_url_for_invalid_context(): void
{
$invalidContext = DomainContext::create(DomainType::UNKNOWN, 'invalid.domain');
$redirectUrl = $invalidContext->getRedirectUrl();
$this->assertNotNull($redirectUrl);
$this->assertStringContains('mivita.care', $redirectUrl);
$validContext = DomainContext::create(DomainType::MAIN, 'mivita.care');
$this->assertNull($validContext->getRedirectUrl());
}
}

View file

@ -0,0 +1,319 @@
<?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']);
}
}
}