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

212 lines
7 KiB
PHP

<?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());
}
}