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

Detecting base url for console requests #165

Merged
merged 1 commit into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/PhpEnvironment/Request.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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());
}
}