146 lines
4.4 KiB
PHP
146 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Service;
|
|
|
|
use App\Model\DomainConfig;
|
|
use App\Service\DomainConfigProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
class DomainConfigProviderTest extends TestCase
|
|
{
|
|
public function testReturnsConfigForExactMatch(): void
|
|
{
|
|
$provider = $this->createProvider([
|
|
'example.com' => [
|
|
'theme' => 'custom',
|
|
'gtm_id' => 'GTM-123',
|
|
'cmp_url' => 'https://cmp.example.com',
|
|
],
|
|
], 'example.com');
|
|
|
|
$config = $provider->getConfig();
|
|
|
|
$this->assertSame('custom', $config->theme);
|
|
$this->assertSame('GTM-123', $config->gtmId);
|
|
$this->assertSame('https://cmp.example.com', $config->cmpUrl);
|
|
}
|
|
|
|
public function testReturnsConfigForSubdomainMatch(): void
|
|
{
|
|
$provider = $this->createProvider([
|
|
'example.com' => [
|
|
'theme' => 'custom',
|
|
'gtm_id' => 'GTM-456',
|
|
'cmp_url' => 'https://cmp.example.com',
|
|
],
|
|
], 'www.example.com');
|
|
|
|
$config = $provider->getConfig();
|
|
|
|
$this->assertSame('custom', $config->theme);
|
|
$this->assertSame('GTM-456', $config->gtmId);
|
|
}
|
|
|
|
public function testReturnsDefaultConfigForUnknownDomain(): void
|
|
{
|
|
$provider = $this->createProvider([
|
|
'example.com' => [
|
|
'theme' => 'custom',
|
|
'gtm_id' => 'GTM-123',
|
|
'cmp_url' => 'https://cmp.example.com',
|
|
],
|
|
], 'unknown.com');
|
|
|
|
$config = $provider->getConfig();
|
|
|
|
$this->assertSame(DomainConfig::DEFAULT_THEME, $config->theme);
|
|
$this->assertSame('', $config->gtmId);
|
|
$this->assertSame('', $config->cmpUrl);
|
|
}
|
|
|
|
public function testReturnsDefaultConfigWhenNoRequest(): void
|
|
{
|
|
$requestStack = new RequestStack();
|
|
$provider = new DomainConfigProvider([], $requestStack);
|
|
|
|
$config = $provider->getConfig();
|
|
|
|
$this->assertSame(DomainConfig::DEFAULT_THEME, $config->theme);
|
|
}
|
|
|
|
public function testHandlesMissingConfigValues(): void
|
|
{
|
|
$provider = $this->createProvider([
|
|
'example.com' => [
|
|
'theme' => 'custom',
|
|
],
|
|
], 'example.com');
|
|
|
|
$config = $provider->getConfig();
|
|
|
|
$this->assertSame('custom', $config->theme);
|
|
$this->assertSame('', $config->gtmId);
|
|
$this->assertSame('', $config->cmpUrl);
|
|
}
|
|
|
|
public function testCachesConfigForSameHost(): void
|
|
{
|
|
$provider = $this->createProvider([
|
|
'example.com' => [
|
|
'theme' => 'custom',
|
|
'gtm_id' => 'GTM-123',
|
|
'cmp_url' => 'https://cmp.example.com',
|
|
],
|
|
], 'example.com');
|
|
|
|
$config1 = $provider->getConfigForHost('example.com');
|
|
$config2 = $provider->getConfigForHost('example.com');
|
|
|
|
$this->assertSame($config1, $config2);
|
|
}
|
|
|
|
public function testFirstMatchWins(): void
|
|
{
|
|
// When a host matches multiple patterns, the first pattern in iteration order wins
|
|
// Here 'deep.portal.example.com' matches both 'portal.example.com' (via subdomain)
|
|
// and 'example.com' (via subdomain), but portal.example.com is checked first
|
|
$provider = $this->createProvider([
|
|
'portal.example.com' => ['theme' => 'portal'],
|
|
'example.com' => ['theme' => 'main'],
|
|
], 'deep.portal.example.com');
|
|
|
|
$config = $provider->getConfig();
|
|
|
|
$this->assertSame('portal', $config->theme);
|
|
}
|
|
|
|
public function testCaseInsensitiveMatching(): void
|
|
{
|
|
$provider = $this->createProvider([
|
|
'Example.COM' => [
|
|
'theme' => 'custom',
|
|
],
|
|
], 'EXAMPLE.com');
|
|
|
|
$config = $provider->getConfig();
|
|
|
|
$this->assertSame('custom', $config->theme);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, array{theme?: string, gtm_id?: string, cmp_url?: string}> $domainConfig
|
|
*/
|
|
private function createProvider(array $domainConfig, string $host): DomainConfigProvider
|
|
{
|
|
$request = Request::create('https://'.$host.'/');
|
|
$requestStack = new RequestStack();
|
|
$requestStack->push($request);
|
|
|
|
return new DomainConfigProvider($domainConfig, $requestStack);
|
|
}
|
|
}
|