From 0c7b9145994de81e6534264851e23bae4461dfdc Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 22:20:18 +0100 Subject: [PATCH] Extract method to get arguments This makes it easier to mock without having to mess with the superglobals --- .../Foundation/Internal/LoadConfiguration.php | 11 +++++--- .../tests/Unit/LoadConfigurationTest.php | 25 +++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index ea97ad8f03d..0e7aa8eb502 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -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); } } @@ -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); diff --git a/packages/framework/tests/Unit/LoadConfigurationTest.php b/packages/framework/tests/Unit/LoadConfigurationTest.php index c80bb45e731..e0a2e43e9ea 100644 --- a/packages/framework/tests/Unit/LoadConfigurationTest.php +++ b/packages/framework/tests/Unit/LoadConfigurationTest.php @@ -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')); } @@ -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;