Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 7149c5d

Browse files
committed
Merge pull request #165 from webimpress/hotfix/base-url-console-request
Detecting base url for console requests
2 parents b98e979 + bedb5c8 commit 7149c5d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/PhpEnvironment/Request.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,11 @@ protected function detectBaseUrl()
489489
// Backtrack up the SCRIPT_FILENAME to find the portion
490490
// matching PHP_SELF.
491491

492+
$argv = $this->getServer()->get('argv', []);
493+
if (isset($argv[0]) && strpos($filename, $argv[0]) === 0) {
494+
$filename = substr($filename, strlen($argv[0]));
495+
}
496+
492497
$baseUrl = '/';
493498
$basename = basename($filename);
494499
if ($basename) {

test/PhpEnvironment/RequestTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,4 +791,18 @@ public function testDetectBaseUrlDoesNotRaiseErrorOnEmptyBaseUrl()
791791
// If no baseUrl is detected at all, an empty string is returned.
792792
$this->assertEquals('', $url);
793793
}
794+
795+
public function testDetectCorrectBaseUrlForConsoleRequests()
796+
{
797+
$_SERVER['argv'] = ['/home/user/package/vendor/bin/phpunit'];
798+
$_SERVER['argc'] = 1;
799+
$_SERVER['SCRIPT_FILENAME'] = '/home/user/package/vendor/bin/phpunit';
800+
$_SERVER['SCRIPT_NAME'] = '/home/user/package/vendor/bin/phpunit';
801+
$_SERVER['PHP_SELF'] = '/home/user/package/vendor/bin/phpunit';
802+
803+
$request = new Request();
804+
$request->setRequestUri('/path/query/phpunit');
805+
806+
$this->assertSame('', $request->getBaseUrl());
807+
}
794808
}

0 commit comments

Comments
 (0)