From 43dd441473517380b250588bd533cc35147ce5e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Mon, 20 Mar 2023 16:15:18 +0100 Subject: [PATCH] fix(AppTest) :make it works --- tests/AppTest.php | 126 +++++++++++++++++++++++++++++++++++++++++- tests/Common.php | 136 ---------------------------------------------- 2 files changed, 124 insertions(+), 138 deletions(-) delete mode 100644 tests/Common.php diff --git a/tests/AppTest.php b/tests/AppTest.php index 4aa797d..016a5e3 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -8,9 +8,9 @@ namespace SeaCMS\App\Test; use SeaCMS\App; -use SeaCMS\App\Test\Common; +use PHPUnit\Framework\TestCase; -final class AppTest extends Common { +final class AppTest extends TestCase { public function testSuccess(): void { @@ -28,4 +28,126 @@ final class AppTest extends Common { { $this->assertTrue(false); } + + /** + * 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)){ + 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 = (basename($shortScriptName) == $fileName) + ? $shortScriptName + : $shortScriptName.((empty($shortScriptName) || substr($shortScriptName,-1) == '/') ? '' : '/').$fileName; + + $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; + if (substr($shortScriptName,-1) == '/'){ + if (!empty($pathInfo) && substr($pathInfo,0,1) == '/'){ + $_SERVER['DOCUMENT_URI'] = $shortScriptName.substr($pathInfo,1); + } else { + $_SERVER['DOCUMENT_URI'] = $shortScriptName.$pathInfo; + } + } else if (empty($pathInfo) || substr($pathInfo,0,1) == '/'){ + $_SERVER['DOCUMENT_URI'] = $shortScriptName.$pathInfo; + } else { + $_SERVER['DOCUMENT_URI'] = $shortScriptName.'/'.$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 + } + } + + /** + * save previous $_SERVER in $GLOBALS if existing + */ + public function saveSERVER() + { + if (!isset($GLOBALS['savedSERVER'])){ + $GLOBALS['savedSERVER'] = [ + 'saved' => false, + 'value' => null + ]; + } + if (!$GLOBALS['savedSERVER']['saved']){ + if (!isset($_SERVER)){ + $GLOBALS['savedSERVER']['value'] = null; + $GLOBALS['savedSERVER']['saved'] = true; + } else { + $GLOBALS['savedSERVER']['value'] = $_SERVER; + $GLOBALS['savedSERVER']['saved'] = true; + } + } + } + + /** + * revert previous $_SERVER in $GLOBALS if existing + */ + public function revertSERVER() + { + if (isset($GLOBALS['savedSERVER']['saved']) && $GLOBALS['savedSERVER']['saved']){ + if (is_null($GLOBALS['savedSERVER']['value'])){ + unset($_SERVER); + } else { + $_SERVER = $GLOBALS['savedSERVER']['value']; + } + $GLOBALS['savedSERVER']['saved'] = false; + $GLOBALS['savedSERVER']['value'] = null; + } + } } \ No newline at end of file diff --git a/tests/Common.php b/tests/Common.php deleted file mode 100644 index ca5d756..0000000 --- a/tests/Common.php +++ /dev/null @@ -1,136 +0,0 @@ - false, - 'value' => null - ]; - } - if (!$GLOBALS['savedSERVER']['saved']){ - if (!isset($_SERVER)){ - $GLOBALS['savedSERVER']['value'] = null; - $GLOBALS['savedSERVER']['saved'] = true; - } else { - $GLOBALS['savedSERVER']['value'] = $_SERVER; - $GLOBALS['savedSERVER']['saved'] = true; - } - } - } - - /** - * revert previous $_SERVER in $GLOBALS if existing - */ - public function revertSERVER() - { - if (isset($GLOBALS['savedSERVER']['saved']) && $GLOBALS['savedSERVER']['saved']){ - if (is_null($GLOBALS['savedSERVER']['value'])){ - unset($_SERVER); - } else { - $_SERVER = $GLOBALS['savedSERVER']['value']; - } - $GLOBALS['savedSERVER']['saved'] = false; - $GLOBALS['savedSERVER']['value'] = null; - } - } -} \ No newline at end of file