diff --git a/CHANGELOG.md b/CHANGELOG.md index 4143bb011..6b73913b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,8 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed +- [#165](https://github.com/zendframework/zend-http/pull/165) fixes detection of the base URL when operating under a CLI environment. + - [#149](https://github.com/zendframework/zend-http/pull/149) provides fixes to `Client::setUri()` to ensure its status as a relative or absolute URI is correctly memoized. 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..2f18a9677 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()); + } }