♻️ Fix unit tests for compatibility with CI

This commit is contained in:
Molkobain
2019-04-17 14:48:44 +02:00
parent d4d16f43ac
commit dccdd84c25
4 changed files with 35 additions and 159 deletions

View File

@@ -1,5 +1,7 @@
<?php
namespace Combodo\iTop\Application\Status;
define('STATUS_ERROR', 'ERROR');
define('STATUS_RUNNING', 'RUNNING');
@@ -8,11 +10,12 @@ define('STATUS_RUNNING', 'RUNNING');
* Move to a function for allowing a better testing
*
* @param string $sAppRootFilename
*
* @throws \Exception
*/
function StatusGetAppRoot($sAppRootFilename = 'approot.inc.php')
{
$sAppRootFile = dirname(__FILE__).'/'.$sAppRootFilename;
$sAppRootFile = __DIR__.'/../../../'.$sAppRootFilename;
/*
* Check that the approot file exists and has the appropriate access rights
@@ -29,11 +32,12 @@ function StatusGetAppRoot($sAppRootFilename = 'approot.inc.php')
* Move to a function for allowing a better testing
*
* @param string $sConfigFilename
*
* @throws \Exception
*/
function StatusCheckConfigFile($sConfigFilename = 'config-itop.php')
{
\StatusGetAppRoot();
StatusGetAppRoot();
$sConfigFile = APPCONF.ITOP_DEFAULT_ENV.'/'.$sConfigFilename;
@@ -49,19 +53,23 @@ function StatusCheckConfigFile($sConfigFilename = 'config-itop.php')
/**
* Start iTop's application for checking with its internal basic test every it's alright (DB connection, ...)
* Move to a function for allowing a better testing
*
*
* @param \Config $oConfig
*
* @throws \CoreException
* @throws \DictExceptionUnknownLanguage
* @throws \MySQLException
*/
function StatusStartup(\Config $oConfig = null)
{
\StatusCheckConfigFile();
StatusCheckConfigFile();
require_once(APPROOT.'/core/cmdbobject.class.inc.php');
require_once(APPROOT.'/application/utils.inc.php');
require_once(APPROOT.'/core/contexttag.class.inc.php');
$soConfigFile = (empty($oConfig)) ? ITOP_DEFAULT_CONFIG_FILE : $oConfig;
$soConfigFile = (null === $oConfig) ? ITOP_DEFAULT_CONFIG_FILE : $oConfig;
//Check if aplication could be started
//Check if application could be started
\MetaModel::Startup($soConfigFile, true /* $bModelOnly */);
}

View File

@@ -1,12 +1,12 @@
<?php
//Include status functions
require_once(dirname(__FILE__) . '/status.inc.php');
require_once(__DIR__ . '/sources/application/status/status.inc.php');
//Do check Status
try
{
\StatusStartup();
\Combodo\iTop\Application\Status\StatusStartup();
$aResult = array('status' => STATUS_RUNNING, 'code' => \RestResult::OK, 'message' => '');
}
catch (\Exception $e)

View File

@@ -11,6 +11,10 @@ namespace Combodo\iTop\Test\UnitTest\Status;
* User: Guy Couronné (guy.couronne@gmail.com)
* Date: 25/01/2019
*/
use function Combodo\iTop\Application\Status\StatusCheckConfigFile;
use function Combodo\iTop\Application\Status\StatusGetAppRoot;
use function Combodo\iTop\Application\Status\StatusStartup;
use PHPUnit\Framework\TestCase;
if (!defined('DEBUG_UNIT_TEST')) {
@@ -36,7 +40,7 @@ class StatusIncTest extends TestCase {
protected function setUp() {
//AppRoot is the directory containing the directory
//Assume getcwd() is runned inside APPROOT/test
$this->sAppRoot = dirname(getcwd());
$this->sAppRoot = __DIR__ . '/../../sources/application/status';
}
/**
@@ -89,7 +93,7 @@ class StatusIncTest extends TestCase {
public function testStatusStartupWrongDbPwd() {
include_once($this->sAppRoot . '/status.inc.php');
\StatusCheckConfigFile();
StatusCheckConfigFile();
require_once(APPROOT . '/core/cmdbobject.class.inc.php');
require_once(APPROOT . '/application/utils.inc.php');
require_once(APPROOT . '/core/contexttag.class.inc.php');

View File

@@ -7,184 +7,48 @@
namespace Combodo\iTop\Test\UnitTest\Status;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use Combodo\iTop\Test\UnitTest\ItopTestCase;
/**
*
*/
class StatusTest extends ItopDataTestCase {
class StatusTest extends ItopTestCase {
/**
*
*/
public function testStatusWrongUrl() {
$sUrl = \utils::GetAbsoluteUrlAppRoot() . 'status_wrong.php';
$sPath = APPROOT . 'status_wrong.php';
if (function_exists('curl_init')) {
// If cURL is available, let's use it, since it provides a greater control over the various HTTP/SSL options
// For instance fopen does not allow to work around the bug: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112
// by setting the SSLVERSION to 3 as done below.
// Default options, can be overloaded/extended with the 4th parameter of this method, see above $aCurlOptions
$aOptions = array(
CURLOPT_RETURNTRANSFER => true, // return the content of the request
CURLOPT_HEADER => false, // don't return the headers in the output
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
// SSLV3 (CURL_SSLVERSION_SSLv3 = 3) is now considered as obsolete/dangerous: http://disablessl3.com/#why
// but it used to be a MUST to prevent a strange SSL error: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112
// CURLOPT_SSLVERSION
CURLOPT_CUSTOMREQUEST => 'HEAD', //Get only HTTP Code as this page should only return a HTTP Code
);
exec("php $sPath", $aOutput, $iRet);
$ch = curl_init($sUrl);
curl_setopt_array($ch, $aOptions);
curl_exec($ch);
$sHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
} else {
// cURL is not available let's try with streams and fopen...
// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD'
)
)
);
$headers = get_headers($sUrl);
//Undo overriding default context
stream_context_set_default(
array(
'http' => array(
'method' => 'GET'
)
)
);
$sHttpCode = (int) substr($headers[0], 9, 3);
}
$this->assertNotEquals(200, $sHttpCode, "Problem opening URL: $sUrl, $sHttpCode");
$this->assertNotEquals(0, $iRet, "Problem executing status page: $sPath, $iRet");
}
/**
*
*/
public function testStatusGood() {
$sUrl = \utils::GetAbsoluteUrlAppRoot() . 'status.php';
$sPath = APPROOT . 'status.php';
if (function_exists('curl_init')) {
// If cURL is available, let's use it, since it provides a greater control over the various HTTP/SSL options
// For instance fopen does not allow to work around the bug: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112
// by setting the SSLVERSION to 3 as done below.
// Default options, can be overloaded/extended with the 4th parameter of this method, see above $aCurlOptions
$aOptions = array(
CURLOPT_RETURNTRANSFER => true, // return the content of the request
CURLOPT_HEADER => false, // don't return the headers in the output
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
// SSLV3 (CURL_SSLVERSION_SSLv3 = 3) is now considered as obsolete/dangerous: http://disablessl3.com/#why
// but it used to be a MUST to prevent a strange SSL error: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112
// CURLOPT_SSLVERSION
CURLOPT_CUSTOMREQUEST => 'HEAD', //Get only HTTP Code as this page should only return a HTTP Code
);
exec("php $sPath", $aOutput, $iRet);
$ch = curl_init($sUrl);
curl_setopt_array($ch, $aOptions);
curl_exec($ch);
$iErr = curl_errno($ch);
$sErrMsg = curl_error($ch);
$sHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$this->assertEquals(0, $iErr, "Problem opening URL: $sUrl, $sErrMsg");
} else {
// cURL is not available let's try with streams and fopen...
// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD'
)
)
);
$headers = get_headers($sUrl);
//Undo overriding default context
stream_context_set_default(
array(
'http' => array(
'method' => 'GET'
)
)
);
$sHttpCode = (int) substr($headers[0], 9, 3);
}
$this->assertEquals(200, $sHttpCode, "Problem opening URL: $sUrl, $sHttpCode");
$this->assertEquals(0, $iRet, "Problem executing status page: $sPath, $iRet");
}
/**
*
*/
public function testStatusGoodWithJson() {
$sUrl = \utils::GetAbsoluteUrlAppRoot() . 'status.php';
$sPath = APPROOT . 'status.php';
if (function_exists('curl_init')) {
// If cURL is available, let's use it, since it provides a greater control over the various HTTP/SSL options
// For instance fopen does not allow to work around the bug: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112
// by setting the SSLVERSION to 3 as done below.
// Default options, can be overloaded/extended with the 4th parameter of this method, see above $aCurlOptions
$aOptions = array(
CURLOPT_RETURNTRANSFER => true, // return the content of the request
CURLOPT_HEADER => false, // don't return the headers in the output
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
// SSLV3 (CURL_SSLVERSION_SSLv3 = 3) is now considered as obsolete/dangerous: http://disablessl3.com/#why
// but it used to be a MUST to prevent a strange SSL error: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112
// CURLOPT_SSLVERSION
);
$ch = curl_init($sUrl);
curl_setopt_array($ch, $aOptions);
$response = curl_exec($ch);
$iErr = curl_errno($ch);
$sErrMsg = curl_error($ch);
$sHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$this->assertEquals(0, $iErr, "Problem opening URL: $sUrl, $sErrMsg");
$this->assertEquals(200, $sHttpCode, "Problem opening URL: $sUrl, $sHttpCode");
} else {
// cURL is not available let's try with file_get_contents
$response = file_get_contents($sUrl);
$this->assertNotFalse($response, "Problem opening URL: $sUrl");
}
exec("php $sPath", $aOutput, $iRet);
//Check response
$this->assertNotEmpty($response, 'Empty response');
$this->assertJson($response, 'Not a JSON');
$this->assertNotEmpty($aOutput[0], 'Empty response');
$this->assertJson($aOutput[0], 'Not a JSON');
$aResponseDecoded = json_decode($response, true);
$aResponseDecoded = json_decode($aOutput[0], true);
//Check status
$this->assertArrayHasKey('status', $aResponseDecoded, 'JSON does not have a status\' field');