parent
804ddb76d4
commit
fdd6f50d46
@ -0,0 +1,91 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* SPDX-License-Identifier: EUPL-1.2 |
||||
* Authors: see /README.md |
||||
*/ |
||||
|
||||
namespace SeaCMS\App\Test; |
||||
|
||||
use SeaCMS\App; |
||||
use PHPUnit\Framework\TestCase; |
||||
|
||||
abstract class Common extends TestCase { |
||||
|
||||
/** |
||||
* define $_SERVER because fastcgi not run in CLI |
||||
* @param bool $reset reset previous $_SERVER |
||||
* @param string $filePath realpath of current script file |
||||
* @param string $shortScriptName uri path of current script file |
||||
* @param string $queryString wanted query string |
||||
* @param string $method |
||||
* @param string $pathInfo |
||||
*/ |
||||
public function defineServer( |
||||
bool $reset, |
||||
string $filePath, |
||||
string $shortScriptName, |
||||
string $queryString, |
||||
string $method = 'GET', |
||||
string $pathInfo = '' |
||||
) |
||||
{ |
||||
if (is_file($filePath) && ( |
||||
substr($shortScriptName,-1) == '/' || |
||||
basename($shortScriptName) == basename($shortScriptName) |
||||
)){ |
||||
if (!is_array($_SERVER) || $reset){ |
||||
$new = []; |
||||
if (is_array($_SERVER)){ |
||||
foreach(['SERVER_SOFTWARE','SERVER_PROTOCOL','GATEWAY_INTERFACE'] as $key){ |
||||
if (array_key_exists($key,$_SERVER)){ |
||||
$new[$key] = $_SERVER[$key]; |
||||
} |
||||
} |
||||
} |
||||
$_SERVER = $new; |
||||
} |
||||
$_SERVER['QUERY_STRING'] = $queryString; |
||||
$_SERVER['REQUEST_METHOD'] = in_array($method,['GET','POST','PUT','DELETE','HEAD']) ? $method : 'GET'; |
||||
// $_SERVER['CONTENT_TYPE'] = 'not defined'; |
||||
// $_SERVER['CONTENT_LENGTH'] = 'not defined'; |
||||
|
||||
$documentRoot = dirname(realpath($filePath)); |
||||
$fileName = basename(realpath($filePath)); |
||||
$documentRootShort = (substr($documentRoot,-strlen(DIRECTORY_SEPARATOR)) != DIRECTORY_SEPARATOR) |
||||
? $documentRoot |
||||
: substr($documentRoot,0,-strlen(DIRECTORY_SEPARATOR)); |
||||
$documentRootFull = $documentRootShort.DIRECTORY_SEPARATOR; |
||||
|
||||
$scriptName = (substr($shortScriptName,-1) == '/') |
||||
? $shortScriptName.$fileName |
||||
: $shortScriptName; |
||||
|
||||
$hashPos = strpos($queryString,'#'); |
||||
$queryStringWithoutHash = |
||||
(empty($queryString) || ($hashPos === 0)) |
||||
? '' |
||||
: ( |
||||
($hashPos === false) |
||||
? "?$queryString" |
||||
: '?'.sustr($queryString,0,$hashPos+1) |
||||
); |
||||
|
||||
$_SERVER['SCRIPT_FILENAME'] = $documentRootFull.$fileName; |
||||
$_SERVER['SCRIPT_NAME'] = $scriptName; |
||||
$_SERVER['PHP_SELF'] = $scriptName.$pathInfo; |
||||
$_SERVER['PATH_INFO'] = $pathInfo; |
||||
$_SERVER['ORIG_PATH_INFO'] = $pathInfo; |
||||
$_SERVER['PATH_TRANSLATED'] = $documentRootShort.$pathInfo; |
||||
$_SERVER['DOCUMENT_URI'] = $shortRequestUri.$pathInfo; |
||||
$_SERVER['REQUEST_URI'] = $_SERVER['DOCUMENT_URI'].$queryStringWithoutHash; |
||||
$_SERVER['DOCUMENT_ROOT'] = $documentRootShort; |
||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // forced |
||||
$_SERVER['REMOTE_PORT'] = '80'; // forced |
||||
$_SERVER['SERVER_ADDR'] = '127.0.0.1'; // forced |
||||
$_SERVER['SERVER_ADDR'] = '80'; // forced |
||||
$_SERVER['SERVER_NAME'] = 'localhost'; // forced |
||||
$_SERVER['HTTPS'] = null; // forced |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue