Compare commits

..

2 Commits

  1. 35
      src/TestBaseUrl.php
  2. 109
      src/TestBaseUrlException.php
  3. 196
      tests/AppTest.php
  4. 136
      tests/Common.php

@ -0,0 +1,35 @@
<?php
// SPDX-License-Identifier: EUPL-1.2
// Authors: see README.md
namespace SeaCMS\App;
use SeacmsAppPlugin;
use SeaCMS\App\TestException;
use SeaCMS\App\TestInterface;
use SeaCMS\App\TestBaseUrlException;
/**
* test base Url
*/
class TestBaseUrl implements TestInterface
{
/**
* run tests
* @param SeacmsAppPlugin $plugin
* @param string $output
* @throws TestException
*/
public function run(SeacmsAppPlugin $plugin, string $output){
$pico = $plugin->getPico();
throw new TestBaseUrlException(
$pico->getBaseUrl(),
$pico->getRootDir(),
$pico->getPluginsDir(),
$pico->getThemesDir(),
$pico->getCurrentPage(),
"Testing base url"
);
}
}

@ -0,0 +1,109 @@
<?php
// SPDX-License-Identifier: EUPL-1.2
// Authors: see README.md
namespace SeaCMS\App;
use SeaCMS\App\TestException;
use Throwable;
/**
* define exception to caught return for tests
*/
class TestBaseUrlException extends TestException
{
/**
* caught baseUrl
* @var string
*/
protected $baseUrl;
/**
* plugin dir
* @var string
*/
protected $pluginDir;
/**
* root dir
* @var string
*/
protected $rootDir;
/**
* theme dir
* @var string
*/
protected $themeDir;
/**
* currentPage
* @var null|array
*/
protected $currentPage;
public function __construct(
string $baseUrl,
string $rootDir,
string $pluginDir,
string $themeDir,
?array $currentPage,
string $message = "",
int $code=0,
Throwable $th = null
)
{
parent::__construct($message,$code,$th);
$this->baseUrl = $baseUrl;
$this->rootDir = $rootDir;
$this->pluginDir = $pluginDir;
$this->themeDir = $themeDir;
$this->currentPage = $currentPage;
}
/**
* get baseUrl
* @return string
*/
public function getBaseUrl(): string
{
return $this->baseUrl;
}
/**
* get pluginDir
* @return string
*/
public function getPluginDir(): string
{
return $this->pluginDir;
}
/**
* get rootDir
* @return string
*/
public function getRootDir(): string
{
return $this->rootDir;
}
/**
* get themeDir
* @return string
*/
public function getThemeDir(): string
{
return $this->themeDir;
}
/**
* get currentPage
* @return null|array
*/
public function getcurrentPage(): ?array
{
return $this->currentPage;
}
}

@ -7,25 +7,199 @@
namespace SeaCMS\App\Test; namespace SeaCMS\App\Test;
use PHPUnit\Framework\TestCase;
use SeaCMS\App; use SeaCMS\App;
use SeaCMS\App\Test\Common; use SeaCMS\App\TestBaseUrl;
use SeaCMS\App\TestBaseUrlException;
use Throwable;
final class AppTest extends Common { final class AppTest extends TestCase {
public function testSuccess(): void /**
* @dataProvider apiRewriteProvider
* @covers App::update_SERVERIfNeeded
* @param string $rootFolder,
* @param string $filePath,
* @param string $shortScriptName,
* @param string $queryString
*/
public function testApiRewrite(
string $rootFolder,
string $filePath,
string $shortScriptName,
string $queryString,
string $waitedBaseUrl,
): void
{ {
$this->saveSERVER();
$this->defineServer( $this->defineServer(
true, true,
'index.php', $filePath,
'/', $shortScriptName,
'' $queryString
); );
$app = new App(''); $app = new App($rootFolder,new TestBaseUrl());
$output = $app->runPico(); $thrown = false;
$this->assertTrue($app instanceof App); $foundTh = null;
try {
$output = $app->runPico();
} catch (TestBaseUrlException $th) {
$thrown = true;
$foundTh = $th;
} catch (Throwable $th){
}
$this->revertSERVER();
$this->assertTrue($thrown,"TestBaseUrlException not found");
$this->assertEquals($waitedBaseUrl,$foundTh->getBaseUrl(),"Not same baseUrl");
} }
public function testFailure(): void
public function apiRewriteProvider()
{
$data = [];
$this->prepareData($data,'content/*','content','index.php','/','','http://localhost/');
$this->prepareData($data,'content/index.php*','content','index.php','/index.php','','http://localhost/');
return $data;
}
public function prepareData(
array &$data,
string $name,
string $rootFolder,
string $filePath,
string $shortScriptName,
string $queryString,
string $waitedBaseUrl,
)
{
$data[$name] = [
'rootFolder' => $rootFolder,
'filePath' => $filePath,
'shortScriptName' => $shortScriptName,
'queryString' => $queryString,
'waitedBaseUrl' => $waitedBaseUrl,
];
}
/**
* 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()
{ {
$this->assertTrue(false); 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;
}
} }
} }

@ -1,136 +0,0 @@
<?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)){
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;
}
}
}
Loading…
Cancel
Save