Skip to content

Commit 44e493f

Browse files
committed
Allow overriding hostName
1 parent bb4e92b commit 44e493f

5 files changed

Lines changed: 146 additions & 3 deletions

File tree

src/Configuration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ public function userAgent(string $userAgent): self
9595
return $this;
9696
}
9797

98+
/**
99+
* Sets the host for the server.
100+
*/
101+
public function withHost(string $host): self
102+
{
103+
Playwright::setHost($host);
104+
105+
return $this;
106+
}
107+
98108
/**
99109
* Enables debug mode for assertions.
100110
*/

src/Playwright/Playwright.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ final class Playwright
5454
*/
5555
private static ?string $userAgent = null;
5656

57+
/**
58+
* The default host.
59+
*/
60+
private static ?string $host = null;
61+
5762
/**
5863
* Get a browser factory for the given browser type.
5964
*/
@@ -134,6 +139,22 @@ public static function setUserAgent(string $userAgent): void
134139
self::$userAgent = $userAgent;
135140
}
136141

142+
/**
143+
* Set the default host.
144+
*/
145+
public static function setHost(?string $host): void
146+
{
147+
self::$host = $host;
148+
}
149+
150+
/**
151+
* Get the default host.
152+
*/
153+
public static function host(): ?string
154+
{
155+
return self::$host;
156+
}
157+
137158
/**
138159
* Get the default color scheme.
139160
*/

src/ServerManager.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Pest\Browser\Contracts\PlaywrightServer;
99
use Pest\Browser\Drivers\LaravelHttpServer;
1010
use Pest\Browser\Drivers\NullableHttpServer;
11+
use Pest\Browser\Playwright\Playwright;
1112
use Pest\Browser\Playwright\Servers\AlreadyStartedPlaywrightServer;
1213
use Pest\Browser\Playwright\Servers\PlaywrightNpmServer;
1314
use Pest\Browser\Support\PackageJsonDirectory;
@@ -59,17 +60,18 @@ public function playwright(): PlaywrightServer
5960
}
6061

6162
$port = Port::find();
63+
$host = Playwright::host() ?? self::DEFAULT_HOST;
6264

6365
$this->playwright ??= PlaywrightNpmServer::create(
6466
PackageJsonDirectory::find(),
6567
'.'.DIRECTORY_SEPARATOR.'node_modules'.DIRECTORY_SEPARATOR.'.bin'.DIRECTORY_SEPARATOR.'playwright run-server --host %s --port %d --mode launchServer',
66-
self::DEFAULT_HOST,
68+
$host,
6769
$port,
6870
'Listening on',
6971
);
7072

7173
AlreadyStartedPlaywrightServer::persist(
72-
self::DEFAULT_HOST,
74+
$host,
7375
$port,
7476
);
7577

@@ -83,7 +85,7 @@ public function http(): HttpServer
8385
{
8486
return $this->http ??= match (function_exists('app_path')) {
8587
true => new LaravelHttpServer(
86-
self::DEFAULT_HOST,
88+
Playwright::host() ?? self::DEFAULT_HOST,
8789
Port::find(),
8890
),
8991
default => new NullableHttpServer(),
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Support\Facades\Route;
6+
7+
it('can visit subdomain routes with browser testing', function (): void {
8+
Route::domain('app.localhost')->group(function (): void {
9+
Route::get('/', fn (): string => '
10+
<html>
11+
<head><title>App Subdomain</title></head>
12+
<body>
13+
<h1>Welcome to App Subdomain</h1>
14+
<div id="content">This is the app subdomain content</div>
15+
</body>
16+
</html>
17+
');
18+
});
19+
20+
pest()->browser()->withHost('app.localhost');
21+
22+
visit('/')
23+
->assertSee('Welcome to App Subdomain')
24+
->assertSeeIn('#content', 'This is the app subdomain content')
25+
->assertTitle('App Subdomain');
26+
});
27+
28+
it('works with Laravel Sail style subdomains', function (): void {
29+
// Simulate Laravel Sail subdomain routing pattern
30+
Route::domain('{subdomain}.localhost')->group(function (): void {
31+
Route::get('/api/health', fn (): array => [
32+
'status' => 'ok',
33+
'subdomain' => request()->route('subdomain'),
34+
'host' => request()->getHost(),
35+
]);
36+
});
37+
38+
pest()->browser()->withHost('api.localhost');
39+
40+
visit('/api/health')
41+
->assertSee('"status":"ok"')
42+
->assertSee('"subdomain":"api"')
43+
->assertSee('"host":"api.localhost"');
44+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Pest\Browser\Configuration;
6+
use Pest\Browser\Playwright\Playwright;
7+
8+
beforeEach(function (): void {
9+
// Reset Playwright state before each test
10+
Playwright::setHost(null);
11+
});
12+
13+
it('can set host via configuration', function (): void {
14+
$config = new Configuration();
15+
16+
$result = $config->withHost('tenant.localhost');
17+
18+
expect($result)->toBeInstanceOf(Configuration::class);
19+
expect(Playwright::host())->toBe('tenant.localhost');
20+
});
21+
22+
it('follows fluent interface pattern', function (): void {
23+
$config = new Configuration();
24+
25+
$result = $config
26+
->withHost('app.localhost')
27+
->userAgent('Test Agent')
28+
->timeout(10000);
29+
30+
expect($result)->toBeInstanceOf(Configuration::class);
31+
expect(Playwright::host())->toBe('app.localhost');
32+
});
33+
34+
it('stores host in Playwright global state', function (): void {
35+
expect(Playwright::host())->toBeNull();
36+
37+
Playwright::setHost('custom.localhost');
38+
39+
expect(Playwright::host())->toBe('custom.localhost');
40+
});
41+
42+
it('can override host multiple times', function (): void {
43+
Playwright::setHost('first.localhost');
44+
expect(Playwright::host())->toBe('first.localhost');
45+
46+
Playwright::setHost('second.localhost');
47+
expect(Playwright::host())->toBe('second.localhost');
48+
49+
Playwright::setHost('final.localhost');
50+
expect(Playwright::host())->toBe('final.localhost');
51+
});
52+
53+
it('handles various host formats', function (): void {
54+
$hosts = [
55+
'localhost',
56+
'app.localhost',
57+
'subdomain.domain.tld',
58+
'192.168.1.100',
59+
'custom-host.test',
60+
];
61+
62+
foreach ($hosts as $host) {
63+
Playwright::setHost($host);
64+
expect(Playwright::host())->toBe($host);
65+
}
66+
});

0 commit comments

Comments
 (0)