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

Commit

Permalink
Merge pull request #165 from webimpress/hotfix/base-url-console-request
Browse files Browse the repository at this point in the history
Detecting base url for console requests
  • Loading branch information
weierophinney committed Jan 8, 2019
2 parents b98e979 + bedb5c8 commit 7149c5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/PhpEnvironment/Request.php
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions test/PhpEnvironment/RequestTest.php
Expand Up @@ -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());
}
}

0 comments on commit 7149c5d

Please sign in to comment.