Files

109 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2026-07-21 13:25:18 +03:30
<?php
namespace Tests\Unit;
use App\Services\ConfigService;
use Illuminate\Http\Request;
2026-07-27 17:29:06 +03:30
use Mockery;
2026-07-21 13:25:18 +03:30
use Tests\TestCase;
class ConfigServiceOtpHostTest extends TestCase
{
2026-07-27 17:29:06 +03:30
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
/** @param mixed $appUrl */
private function cfgWithAppUrl($appUrl): ConfigService
{
$cfg = Mockery::mock(ConfigService::class)->makePartial();
$cfg->shouldReceive('get')->andReturnUsing(function (string $key, $default = null) use ($appUrl) {
if ($key === 'app_url') {
return $appUrl !== null && $appUrl !== '' ? $appUrl : $default;
}
return $default;
});
return $cfg;
}
2026-07-21 13:25:18 +03:30
public function test_uses_request_host_without_settings(): void
{
$request = Request::create('https://boursemes.com/server/auth/login', 'POST');
$this->app->instance('request', $request);
2026-07-27 17:29:06 +03:30
$host = $this->cfgWithAppUrl(null)->resolvePublicHost($request);
2026-07-21 13:25:18 +03:30
$this->assertSame('boursemes.com', $host);
}
public function test_resolve_public_site_url_from_request(): void
{
$request = Request::create('https://boursemes.com/server/auth/login', 'POST');
2026-07-27 17:29:06 +03:30
$url = $this->cfgWithAppUrl(null)->resolvePublicSiteUrl($request);
2026-07-21 13:25:18 +03:30
$this->assertSame('https://boursemes.com', $url);
}
2026-07-27 17:29:06 +03:30
public function test_prefers_app_url_over_api_request_host(): void
{
$request = Request::create('https://goldarvin.gholaktala.ir/server/customer/order', 'POST');
$this->app->instance('request', $request);
$cfg = $this->cfgWithAppUrl('https://my.goldarvin.ir');
$this->assertSame('my.goldarvin.ir', $cfg->resolvePublicHost($request));
$this->assertSame('https://my.goldarvin.ir', $cfg->resolvePublicSiteUrl($request));
}
public function test_does_not_fall_back_to_laravel_api_host(): void
{
config(['app.url' => 'https://goldarvin.gholaktala.ir/server']);
$request = Request::create('https://goldarvin.gholaktala.ir/server/admin/customer', 'POST');
$this->app->instance('request', $request);
$this->assertSame('', $this->cfgWithAppUrl(null)->resolvePublicSiteUrl($request));
$this->assertSame('', $this->cfgWithAppUrl(null)->resolvePublicHost($request));
}
2026-07-27 17:29:06 +03:30
public function test_app_url_without_scheme_gets_https(): void
{
$request = Request::create('https://goldarvin.gholaktala.ir/server/auth/login', 'POST');
$this->assertSame(
'https://my.goldarvin.ir',
$this->cfgWithAppUrl('my.goldarvin.ir')->resolvePublicSiteUrl($request)
);
}
2026-07-21 13:25:18 +03:30
public function test_returns_empty_without_request(): void
{
2026-07-27 17:29:06 +03:30
$host = $this->cfgWithAppUrl(null)->resolvePublicHost();
2026-07-21 13:25:18 +03:30
$this->assertSame('', $host);
}
public function test_strips_www_prefix(): void
{
$request = Request::create('https://www.boursemes.com/server/auth/login', 'POST');
2026-07-27 17:29:06 +03:30
$host = $this->cfgWithAppUrl(null)->resolvePublicHost($request);
2026-07-21 13:25:18 +03:30
$this->assertSame('boursemes.com', $host);
}
public function test_skips_localhost_request_host(): void
{
$request = Request::create('http://localhost/server/auth/login', 'POST');
$this->app->instance('request', $request);
2026-07-27 17:29:06 +03:30
$host = $this->cfgWithAppUrl(null)->resolvePublicHost($request);
2026-07-21 13:25:18 +03:30
$this->assertSame('', $host);
}
}