From bedb5c8895f2dd766175584dad74fac987cd9024 Mon Sep 17 00:00:00 2001 From: webimpress Date: Tue, 25 Dec 2018 14:40:16 +0100 Subject: [PATCH] Detecting base url for console requests --- src/PhpEnvironment/Request.php | 5 +++++ test/PhpEnvironment/RequestTest.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/PhpEnvironment/Request.php b/src/PhpEnvironment/Request.php index c22d7b003..03d9e7af3 100644 --- a/src/PhpEnvironment/Request.php +++ b/src/PhpEnvironment/Request.php @@ -489,6 +489,11 @@ protected function detectBaseUrl() // Backtrack up the SCRIPT_FILENAME to find the portion // matching PHP_SELF. + $argv = $this->getServer()->get('argv', []); + if (isset($argv[0]) && strpos($filename, $argv[0]) === 0) { + $filename = substr($filename, strlen($argv[0])); + } + $baseUrl = '/'; $basename = basename($filename); if ($basename) { diff --git a/test/PhpEnvironment/RequestTest.php b/test/PhpEnvironment/RequestTest.php index f8e8594d3..c9b37c232 100644 --- a/test/PhpEnvironment/RequestTest.php +++ b/test/PhpEnvironment/RequestTest.php @@ -791,4 +791,18 @@ public function testDetectBaseUrlDoesNotRaiseErrorOnEmptyBaseUrl() // If no baseUrl is detected at all, an empty string is returned. $this->assertEquals('', $url); } + + public function testDetectCorrectBaseUrlForConsoleRequests() + { + $_SERVER['argv'] = ['/home/user/package/vendor/bin/phpunit']; + $_SERVER['argc'] = 1; + $_SERVER['SCRIPT_FILENAME'] = '/home/user/package/vendor/bin/phpunit'; + $_SERVER['SCRIPT_NAME'] = '/home/user/package/vendor/bin/phpunit'; + $_SERVER['PHP_SELF'] = '/home/user/package/vendor/bin/phpunit'; + + $request = new Request(); + $request->setRequestUri('/path/query/phpunit'); + + $this->assertSame('', $request->getBaseUrl()); + } }