Skip to content
This repository was archived by the owner on Nov 30, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/Commands/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ public function handle()
$app = $this->resolvers()->application()->from($this->argument('application'));
$environment = $this->resolvers()->environment()->withApplication($app)->from($this->argument('environment'));

if (! $environment->url) {
$url = $environment->siteUrl();

if (! $url) {
warning('No site found for this environment.');

return self::FAILURE;
}

openUrl($environment->url);
openUrl($url);

outro($environment->url);
outro($url);
}
}
23 changes: 23 additions & 0 deletions app/Dto/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __construct(
public readonly ?string $currentDeploymentId = null,
public readonly array $domainIds = [],
public readonly ?string $primaryDomainId = null,
public readonly ?Domain $primaryDomain = null,
public readonly ?string $databaseSchemaId = null,
public readonly ?string $cacheId = null,
public readonly ?string $websocketApplicationId = null,
Expand Down Expand Up @@ -134,6 +135,12 @@ public static function createFromResponse(array $response): self

if (isset($relationships['primaryDomain']['data']['id'])) {
$transformed['primaryDomainId'] = $relationships['primaryDomain']['data']['id'];

$domain = collect($included)->first(fn ($item) => $item['type'] === 'domains' && $item['id'] === $transformed['primaryDomainId']);

if ($domain) {
$transformed['primaryDomain'] = Domain::createFromResponse(['data' => $domain]);
}
}

if (isset($relationships['database']['data']['id'])) {
Expand Down Expand Up @@ -161,6 +168,22 @@ public static function createFromResponse(array $response): self
return self::from($transformed);
}

/**
* Until hostname and SSL both verify, the custom domain may not resolve or may serve a certificate error.
*/
public function siteUrl(): string
{
if (
$this->primaryDomain?->name
&& $this->primaryDomain->hostnameStatus === 'verified'
&& $this->primaryDomain->sslStatus === 'verified'
) {
return (string) str($this->primaryDomain->name)->start('https://');
}

return $this->url;
}

public function colorEnum(): ?EnvironmentColor
{
return $this->color ? EnvironmentColor::tryFrom($this->color) : null;
Expand Down
2 changes: 1 addition & 1 deletion app/Prompts/MonitorDeployments.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function display(): void

$keyPressListener = KeyPressListener::for($this)->listenForQuit()->on('o', function () {
if ($this->lastDeployment) {
openUrl($this->environment->url);
openUrl($this->environment->siteUrl());
}
});

Expand Down
106 changes: 106 additions & 0 deletions tests/Feature/BrowserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

use App\Client\Resources\Applications\GetApplicationRequest;
use App\Client\Resources\Environments\GetEnvironmentRequest;
use App\Client\Resources\Meta\GetOrganizationRequest;
use App\ConfigRepository;
use App\Git;
use Illuminate\Process\PendingProcess;
use Illuminate\Support\Facades\Process;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;

beforeEach(function () {
$this->mockGit = Mockery::mock(Git::class);
$this->mockGit->shouldReceive('isRepo')->andReturn(true)->byDefault();
$this->mockGit->shouldReceive('hasRemote')->andReturn(true)->byDefault();
$this->mockGit->shouldReceive('getRoot')->andReturn('/tmp/test-repo')->byDefault();
$this->mockGit->shouldReceive('currentBranch')->andReturn('main')->byDefault();
$this->app->instance(Git::class, $this->mockGit);

$this->mockConfig = Mockery::mock(ConfigRepository::class);
$this->mockConfig->shouldReceive('apiTokens')->andReturn(collect(['test-api-token']));
$this->app->instance(ConfigRepository::class, $this->mockConfig);

Process::fake();
});

afterEach(function () {
MockClient::destroyGlobal();
});

function browserEnvironmentResponse(?string $hostnameStatus, string $sslStatus = 'verified'): array
{
if ($hostnameStatus === null) {
return ['data' => createEnvironmentResponse(), 'included' => []];
}

return [
'data' => createEnvironmentResponse([
'relationships' => [
'primaryDomain' => ['data' => ['id' => 'dom-1', 'type' => 'domains']],
],
]),
'included' => [[
'id' => 'dom-1',
'type' => 'domains',
'attributes' => [
'name' => 'example.com',
'type' => 'root',
'hostname_status' => $hostnameStatus,
'ssl_status' => $sslStatus,
'origin_status' => 'verified',
],
]],
];
}

function mockBrowserApi(?string $hostnameStatus, string $sslStatus = 'verified'): void
{
MockClient::global([
GetOrganizationRequest::class => MockResponse::make(organizationResponse(), 200),
GetApplicationRequest::class => MockResponse::make(['data' => createApplicationResponse()], 200),
GetEnvironmentRequest::class => MockResponse::make(browserEnvironmentResponse($hostnameStatus, $sslStatus), 200),
]);
}

function openedUrl(string $url): void
{
Process::assertRan(fn (PendingProcess $process) => in_array($url, (array) $process->command, true));
}

it('opens the verified primary domain', function () {
mockBrowserApi('verified');

$this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true])
->assertSuccessful();

openedUrl('https://example.com');
});

it('falls back to the vanity domain while the primary domain is unverified', function () {
mockBrowserApi('pending');

$this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true])
->assertSuccessful();

openedUrl('https://my-app.cloud.laravel.com');
});

it('falls back to the vanity domain while the certificate is pending', function () {
mockBrowserApi('verified', 'pending');

$this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true])
->assertSuccessful();

openedUrl('https://my-app.cloud.laravel.com');
});

it('opens the vanity domain when there is no custom domain', function () {
mockBrowserApi(null);

$this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true])
->assertSuccessful();

openedUrl('https://my-app.cloud.laravel.com');
});