Skip to content

Commit

Permalink
Merge pull request #1694 from hydephp/update-realtime-compiler-open-f…
Browse files Browse the repository at this point in the history
…lag-to-support-specifying-the-page-to-open

Update realtime compiler open flag to support specifying the page to open
  • Loading branch information
caendesilva committed Apr 28, 2024
2 parents d5390fd + bcfd573 commit e86b33f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 38 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This serves two purposes:

### Added
- Added support for using HTML comments to create Markdown code block filepath labels in https://github.com/hydephp/develop/pull/1693
- You can now specify which path to open when using the `--open` option in the serve command in https://github.com/hydephp/develop/pull/1694

### Changed
- for changes in existing functionality.
Expand Down
15 changes: 9 additions & 6 deletions packages/framework/src/Console/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ServeCommand extends Command
{--dashboard= : Enable the realtime compiler dashboard. (Overrides config setting)}
{--pretty-urls= : Enable pretty URLs. (Overrides config setting)}
{--play-cdn= : Enable the Tailwind Play CDN. (Overrides config setting)}
{--open : Open the site preview in the browser.}
{--open=false : Open the site preview in the browser.}
';

/** @var string */
Expand All @@ -46,8 +46,8 @@ public function safeHandle(): int
$this->configureOutput();
$this->printStartMessage();

if ($this->option('open')) {
$this->openInBrowser();
if ($this->option('open') !== 'false') {
$this->openInBrowser((string) $this->option('open'));
}

$this->runServerProcess(sprintf('php -S %s:%d %s',
Expand Down Expand Up @@ -143,16 +143,19 @@ protected function checkArgvForOption(string $name): ?string
return null;
}

protected function openInBrowser(): void
protected function openInBrowser(string $path = '/'): void
{
$command = match (PHP_OS_FAMILY) {
$binary = match (PHP_OS_FAMILY) {
'Windows' => 'start',
'Darwin' => 'open',
'Linux' => 'xdg-open',
default => null
};

$process = $command ? Process::command(sprintf('%s http://%s:%d', $command, $this->getHostSelection(), $this->getPortSelection()))->run() : null;
$command = sprintf('%s http://%s:%d', $binary, $this->getHostSelection(), $this->getPortSelection());
$command = rtrim("$command/$path", '/');

$process = $binary ? Process::command($command)->run() : null;

if (! $process || $process->failed()) {
$this->warn('Unable to open the site preview in the browser on your system:');
Expand Down
128 changes: 96 additions & 32 deletions packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Mockery;
use Hyde\Testing\UnitTestCase;
use Hyde\Foundation\HydeKernel;
use Illuminate\Process\Factory;
use Illuminate\Console\OutputStyle;
use Hyde\Console\Commands\ServeCommand;
use Illuminate\Support\Facades\Process;
Expand All @@ -26,6 +27,18 @@ protected function setUp(): void
'hyde.server.host' => 'localhost',
'hyde.server.port' => 8080,
]);

Process::swap(new Factory());
Process::preventStrayProcesses();
}

protected function tearDown(): void
{
$this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount());

Mockery::close();

parent::tearDown();
}

public function testGetHostSelection()
Expand Down Expand Up @@ -210,34 +223,35 @@ public function testWithOpenArgument()
{
HydeKernel::setInstance(new HydeKernel());

$command = new class(['open' => true]) extends ServeCommandMock
{
public bool $openInBrowserCalled = false;
$command = $this->getOpenServeCommandMock(['open' => true]);

// Void unrelated methods
protected function configureOutput(): void
{
}
$command->safeHandle();

protected function printStartMessage(): void
{
}
$this->assertTrue($command->openInBrowserCalled);
}

protected function runServerProcess(string $command): void
{
}
public function testWithOpenArgumentWhenString()
{
HydeKernel::setInstance(new HydeKernel());

protected function openInBrowser(): void
{
$this->openInBrowserCalled = true;
}
};
$command = $this->getOpenServeCommandMock(['open' => '']);

$command->safeHandle();

$this->assertTrue($command->openInBrowserCalled);
}

public function testWithOpenArgumentWhenPath()
{
HydeKernel::setInstance(new HydeKernel());

$command = $this->getOpenServeCommandMock(['open' => 'dashboard']);

$command->safeHandle();

$this->assertSame('dashboard', $command->openInBrowserPath);
}

public function testOpenInBrowser()
{
$output = $this->createMock(OutputStyle::class);
Expand All @@ -246,11 +260,7 @@ public function testOpenInBrowser()
$command = $this->getMock(['--open' => true]);
$command->setOutput($output);

$binary = match (PHP_OS_FAMILY) {
'Darwin' => 'open',
'Windows' => 'start',
default => 'xdg-open',
};
$binary = $this->getTestRunnerBinary();

Process::shouldReceive('command')->once()->with("$binary http://localhost:8080")->andReturnSelf();
Process::shouldReceive('run')->once()->andReturnSelf();
Expand All @@ -259,6 +269,31 @@ public function testOpenInBrowser()
$command->openInBrowser();
}

public function testOpenInBrowserWithPath()
{
Process::shouldReceive('command')->once()->with("{$this->getTestRunnerBinary()} http://localhost:8080/dashboard")->andReturnSelf();
Process::shouldReceive('run')->once()->andReturnSelf();
Process::shouldReceive('failed')->once()->andReturn(false);

$this->getMock()->openInBrowser('dashboard');
}

public function testOpenInBrowserWithPathNormalizesPaths()
{
Process::shouldReceive('run')->andReturnSelf();
Process::shouldReceive('failed')->andReturn(false);

Process::shouldReceive('command')->times(3)->with("{$this->getTestRunnerBinary()} http://localhost:8080")->andReturnSelf();
Process::shouldReceive('command')->once()->with("{$this->getTestRunnerBinary()} http://localhost:8080/dashboard")->andReturnSelf();
Process::shouldReceive('command')->once()->with("{$this->getTestRunnerBinary()} http://localhost:8080/foo/bar")->andReturnSelf();

$this->getMock()->openInBrowser('');
$this->getMock()->openInBrowser('/');
$this->getMock()->openInBrowser('//');
$this->getMock()->openInBrowser('dashboard/');
$this->getMock()->openInBrowser('foo/bar/');
}

public function testOpenInBrowserThatFails()
{
$output = Mockery::mock(OutputStyle::class);
Expand All @@ -274,28 +309,57 @@ public function testOpenInBrowserThatFails()
$command = $this->getMock(['--open' => true]);
$command->setOutput($output);

$binary = match (PHP_OS_FAMILY) {
'Darwin' => 'open',
'Windows' => 'start',
default => 'xdg-open',
};
$binary = $this->getTestRunnerBinary();

Process::shouldReceive('command')->once()->with("$binary http://localhost:8080")->andReturnSelf();
Process::shouldReceive('run')->once()->andReturnSelf();
Process::shouldReceive('failed')->once()->andReturn(true);
Process::shouldReceive('errorOutput')->once()->andReturn("Missing suitable 'open' binary.");

$command->openInBrowser();
}

Mockery::close();

$this->assertTrue(true);
protected function getTestRunnerBinary(): string
{
return match (PHP_OS_FAMILY) {
'Darwin' => 'open',
'Windows' => 'start',
default => 'xdg-open',
};
}

protected function getMock(array $options = []): ServeCommandMock
{
return new ServeCommandMock($options);
}

protected function getOpenServeCommandMock(array $arguments): ServeCommandMock
{
return new class($arguments) extends ServeCommandMock
{
public bool $openInBrowserCalled = false;
public string $openInBrowserPath = '';

// Void unrelated methods
protected function configureOutput(): void
{
}

protected function printStartMessage(): void
{
}

protected function runServerProcess(string $command): void
{
}

protected function openInBrowser(string $path = '/'): void
{
$this->openInBrowserCalled = true;
$this->openInBrowserPath = $path;
}
};
}
}

/**
Expand All @@ -304,7 +368,7 @@ protected function getMock(array $options = []): ServeCommandMock
* @method getEnvironmentVariables
* @method parseEnvironmentOption(string $name)
* @method checkArgvForOption(string $name)
* @method openInBrowser()
* @method openInBrowser(string $path = '/')
*/
class ServeCommandMock extends ServeCommand
{
Expand Down

0 comments on commit e86b33f

Please sign in to comment.