188 lines
6.2 KiB
PHP
188 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Domain\DomainContext;
|
|
use App\Domain\DomainType;
|
|
use App\Services\DomainService;
|
|
use App\Models\UserShop;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Domain Routing Test - Beispiel-Tests für das neue System
|
|
*/
|
|
class DomainRoutingTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private DomainService $domainService;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->domainService = app(DomainService::class);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_resolves_main_domain_correctly()
|
|
{
|
|
$context = $this->domainService->resolveDomain('mivita.care');
|
|
|
|
$this->assertEquals(DomainType::MAIN, $context->type);
|
|
$this->assertEquals('mivita.care', $context->host);
|
|
$this->assertNull($context->subdomain);
|
|
$this->assertNull($context->userShop);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_resolves_crm_domain_correctly()
|
|
{
|
|
$context = $this->domainService->resolveDomain('my.mivita.care');
|
|
|
|
$this->assertEquals(DomainType::CRM, $context->type);
|
|
$this->assertEquals('my.mivita.care', $context->host);
|
|
$this->assertEquals('my', $context->subdomain);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_resolves_portal_domain_correctly()
|
|
{
|
|
$context = $this->domainService->resolveDomain('in.mivita.care');
|
|
|
|
$this->assertEquals(DomainType::PORTAL, $context->type);
|
|
$this->assertEquals('in.mivita.care', $context->host);
|
|
$this->assertEquals('in', $context->subdomain);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_resolves_user_shop_domain_correctly()
|
|
{
|
|
// UserShop in DB erstellen
|
|
$userShop = UserShop::factory()->create([
|
|
'slug' => 'testshop',
|
|
'name' => 'Test Shop',
|
|
'active' => true,
|
|
]);
|
|
|
|
$userShop->user()->create([
|
|
'name' => 'Test User',
|
|
'email' => 'test@example.com',
|
|
'payment_shop' => now()->addDays(30),
|
|
]);
|
|
|
|
$context = $this->domainService->resolveDomain('testshop.mivita.care');
|
|
|
|
$this->assertEquals(DomainType::USER_SHOP, $context->type);
|
|
$this->assertEquals('testshop.mivita.care', $context->host);
|
|
$this->assertEquals('testshop', $context->subdomain);
|
|
$this->assertNotNull($context->userShop);
|
|
$this->assertEquals('testshop', $context->userShop->slug);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_handles_unknown_domains_correctly()
|
|
{
|
|
$context = $this->domainService->resolveDomain('unknown.mivita.care');
|
|
|
|
$this->assertEquals(DomainType::UNKNOWN, $context->type);
|
|
$this->assertEquals('unknown.mivita.care', $context->host);
|
|
$this->assertEquals('unknown', $context->subdomain);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_handles_invalid_user_shop_correctly()
|
|
{
|
|
// UserShop ohne aktive Zahlung
|
|
$userShop = UserShop::factory()->create([
|
|
'slug' => 'invalidshop',
|
|
'active' => true,
|
|
]);
|
|
|
|
$userShop->user()->create([
|
|
'name' => 'Test User',
|
|
'email' => 'test@example.com',
|
|
'payment_shop' => now()->subDays(1), // Abgelaufen
|
|
]);
|
|
|
|
$context = $this->domainService->resolveDomain('invalidshop.mivita.care');
|
|
|
|
$this->assertEquals(DomainType::UNKNOWN, $context->type);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_validates_user_shop_slug_correctly()
|
|
{
|
|
$this->assertTrue($this->domainService->isValidUserShopSlug('validshop123'));
|
|
$this->assertFalse($this->domainService->isValidUserShopSlug('ab')); // Zu kurz
|
|
$this->assertFalse($this->domainService->isValidUserShopSlug('invalid-shop!')); // Ungültige Zeichen
|
|
}
|
|
|
|
/** @test */
|
|
public function it_builds_urls_correctly()
|
|
{
|
|
$url = $this->domainService->buildUrl('crm');
|
|
$this->assertEquals('https://my.mivita.care', $url);
|
|
|
|
$url = $this->domainService->buildUrl('user-shop', '/products', 'testshop');
|
|
$this->assertEquals('https://testshop.mivita.care/products', $url);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_provides_debug_information()
|
|
{
|
|
$debug = $this->domainService->getDebugInfo('my.mivita.care');
|
|
|
|
$this->assertArrayHasKey('host', $debug);
|
|
$this->assertArrayHasKey('domain_info', $debug);
|
|
$this->assertArrayHasKey('determined_type', $debug);
|
|
$this->assertArrayHasKey('is_known', $debug);
|
|
$this->assertEquals('my.mivita.care', $debug['host']);
|
|
$this->assertEquals('crm', $debug['determined_type']);
|
|
}
|
|
|
|
/** @test */
|
|
public function domain_context_has_correct_string_representation()
|
|
{
|
|
$context = $this->domainService->resolveDomain('my.mivita.care');
|
|
$string = (string) $context;
|
|
|
|
$this->assertStringContains('DomainType: crm', $string);
|
|
$this->assertStringContains('Host: my.mivita.care', $string);
|
|
$this->assertStringContains('Subdomain: my', $string);
|
|
}
|
|
|
|
/** @test */
|
|
public function domain_context_to_array_works_correctly()
|
|
{
|
|
$context = $this->domainService->resolveDomain('my.mivita.care');
|
|
$array = $context->toArray();
|
|
|
|
$this->assertEquals('crm', $array['type']);
|
|
$this->assertEquals('my.mivita.care', $array['host']);
|
|
$this->assertEquals('my', $array['subdomain']);
|
|
$this->assertTrue($array['is_known']);
|
|
$this->assertFalse($array['is_user_shop']);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_caches_domain_resolution()
|
|
{
|
|
// Erste Auflösung (uncached)
|
|
$start = microtime(true);
|
|
$context1 = $this->domainService->resolveDomain('cachetest.mivita.care');
|
|
$firstDuration = microtime(true) - $start;
|
|
|
|
// Zweite Auflösung (cached)
|
|
$start = microtime(true);
|
|
$context2 = $this->domainService->resolveDomain('cachetest.mivita.care');
|
|
$secondDuration = microtime(true) - $start;
|
|
|
|
// Gleicher Context
|
|
$this->assertEquals($context1->type, $context2->type);
|
|
$this->assertEquals($context1->host, $context2->host);
|
|
|
|
// Zweite sollte schneller sein (Cache-Hit)
|
|
$this->assertLessThan($firstDuration, $secondDuration);
|
|
}
|
|
}
|