Skip to content

Commit

Permalink
Extract method to get arguments
Browse files Browse the repository at this point in the history
This makes it easier to mock without having to mess with the superglobals
  • Loading branch information
caendesilva committed Nov 11, 2023
1 parent 418fafe commit 0c7b914
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
11 changes: 8 additions & 3 deletions packages/framework/src/Foundation/Internal/LoadConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ private static function providePharSupportIfNeeded(array &$files): void
private function loadRuntimeConfiguration(Application $app, RepositoryContract $repository): void
{
if ($app->runningInConsole()) {
if (isset($_SERVER['argv'])) {
if ($this->getArgv() !== null) {
// Check if the `--pretty-urls` CLI argument is set, and if so, set the config value accordingly.
if (in_array('--pretty-urls', $_SERVER['argv'], true)) {
if (in_array('--pretty-urls', $this->getArgv(), true)) {
$repository->set('hyde.pretty_urls', true);
}

// Check if the `--no-api` CLI argument is set, and if so, set the config value accordingly.
if (in_array('--no-api', $_SERVER['argv'], true)) {
if (in_array('--no-api', $this->getArgv(), true)) {
$repository->set('hyde.api_calls', false);
}
}
Expand All @@ -106,6 +106,11 @@ private function mergeRealtimeCompilerEnvironment(RepositoryContract $repository
}
}

protected function getArgv(): ?array
{
return $_SERVER['argv'] ?? null;
}

protected function getEnv(string $name): string|false|null
{
return getenv($name);
Expand Down
25 changes: 18 additions & 7 deletions packages/framework/tests/Unit/LoadConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@ class LoadConfigurationTest extends UnitTestCase
{
public function testItLoadsRuntimeConfiguration()
{
$serverBackup = $_SERVER;

$_SERVER['argv'] = ['--pretty-urls', '--no-api'];

$app = new Application(getcwd());

$loader = new LoadConfiguration();
$loader = new LoadConfigurationTestClass(['--pretty-urls', '--no-api']);
$loader->bootstrap($app);

$this->assertTrue(config('hyde.pretty_urls'));
$this->assertFalse(config('hyde.api_calls'));

$_SERVER = $serverBackup;

$loader = new LoadConfigurationTestClass([]);
$loader->bootstrap($app);

$this->assertFalse(config('hyde.pretty_urls'));
$this->assertNull(config('hyde.api_calls'));
}
Expand All @@ -44,6 +40,21 @@ public function testItLoadsRealtimeCompilerEnvironmentConfiguration()
}
}

class LoadConfigurationTestClass extends LoadConfiguration
{
protected array $argv;

public function __construct(array $argv)
{
$this->argv = $argv;
}

protected function getArgv(): ?array
{
return $this->argv;
}
}

class LoadConfigurationEnvironmentTestClass extends LoadConfiguration
{
protected array $env;
Expand Down

0 comments on commit 0c7b914

Please sign in to comment.