From fdd6f50d4657aa7c56ed2aecceb7683f24609313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Sun, 19 Mar 2023 20:18:57 +0100 Subject: [PATCH 1/3] feat(Test/common): create --- composer.json | 1 + tests/AppTest.php | 10 ++++-- tests/Common.php | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 tests/Common.php diff --git a/composer.json b/composer.json index 5c7ba8e..e6f98cd 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "autoload": { "classmap": ["App.php","SeacmsAppPlugin.php"], "psr-4": { + "SeaCMS\\App\\Test\\": "tests", "SeaCMS\\App\\": "src" } }, diff --git a/tests/AppTest.php b/tests/AppTest.php index f207b99..4aa797d 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -8,12 +8,18 @@ namespace SeaCMS\App\Test; use SeaCMS\App; -use PHPUnit\Framework\TestCase; +use SeaCMS\App\Test\Common; -final class AppTest extends TestCase { +final class AppTest extends Common { public function testSuccess(): void { + $this->defineServer( + true, + 'index.php', + '/', + '' + ); $app = new App(''); $output = $app->runPico(); $this->assertTrue($app instanceof App); diff --git a/tests/Common.php b/tests/Common.php new file mode 100644 index 0000000..abf681d --- /dev/null +++ b/tests/Common.php @@ -0,0 +1,91 @@ + Date: Mon, 20 Mar 2023 08:46:02 +0100 Subject: [PATCH 2/3] fix(Test/Common): to be able to define all possible $_SERVER --- tests/Common.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tests/Common.php b/tests/Common.php index abf681d..5008660 100644 --- a/tests/Common.php +++ b/tests/Common.php @@ -30,10 +30,7 @@ abstract class Common extends TestCase { string $pathInfo = '' ) { - if (is_file($filePath) && ( - substr($shortScriptName,-1) == '/' || - basename($shortScriptName) == basename($shortScriptName) - )){ + if (is_file($filePath)){ if (!is_array($_SERVER) || $reset){ $new = []; if (is_array($_SERVER)){ @@ -57,9 +54,9 @@ abstract class Common extends TestCase { : substr($documentRoot,0,-strlen(DIRECTORY_SEPARATOR)); $documentRootFull = $documentRootShort.DIRECTORY_SEPARATOR; - $scriptName = (substr($shortScriptName,-1) == '/') - ? $shortScriptName.$fileName - : $shortScriptName; + $scriptName = (basename($shortScriptName) == $fileName) + ? $shortScriptName + : $shortScriptName.((empty($shortScriptName) || substr($shortScriptName,-1) == '/') ? '' : '/').$fileName; $hashPos = strpos($queryString,'#'); $queryStringWithoutHash = @@ -77,7 +74,17 @@ abstract class Common extends TestCase { $_SERVER['PATH_INFO'] = $pathInfo; $_SERVER['ORIG_PATH_INFO'] = $pathInfo; $_SERVER['PATH_TRANSLATED'] = $documentRootShort.$pathInfo; - $_SERVER['DOCUMENT_URI'] = $shortRequestUri.$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 From b6a2a223898e50498b312e7fb1400f4de99e51d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Mon, 20 Mar 2023 08:51:54 +0100 Subject: [PATCH 3/3] feat(test/commen): add helpers for SERVER manipulating --- tests/Common.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/Common.php b/tests/Common.php index 5008660..ca5d756 100644 --- a/tests/Common.php +++ b/tests/Common.php @@ -95,4 +95,42 @@ abstract class Common extends TestCase { $_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