mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 23:44:11 +01:00
* Add a complete test suite for XML assembly * Add a complete test suite for XML assembly * Dispatched the test of GetDelta into real unit tests * Add test for GetDelta on a rename operation * Add comments on a weird case and a case on rename * Update XML version after rebase from develop to support/2.7 * Fix phpdoc about coverage * Remove ModelFactory::GetRootDirs and ItopTestCase::RecurseRmDir+CreateTmpDir+RecurseMkDir+RecurseCopy, that were meant to be introduced in iTop 3.0 and have been copied here by mistake, when rebasing the branch from develop to 2.7.0 * Update test/ItopTestCase.php Co-authored-by: Molkobain <lajarige.guillaume@free.fr> * Update test/setup/ModelFactoryTest.php Co-authored-by: Molkobain <lajarige.guillaume@free.fr> * Update test/ItopTestCase.php Co-authored-by: Molkobain <lajarige.guillaume@free.fr> Co-authored-by: Pierre Goiffon <pierre.goiffon@combodo.com> Co-authored-by: Molkobain <lajarige.guillaume@free.fr>
175 lines
4.4 KiB
PHP
175 lines
4.4 KiB
PHP
<?php
|
|
/**
|
|
* Copyright (C) 2013-2019 Combodo SARL
|
|
*
|
|
* This file is part of iTop.
|
|
*
|
|
* iTop is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* iTop is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
*/
|
|
|
|
namespace Combodo\iTop\Test\UnitTest;
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: Eric
|
|
* Date: 20/11/2017
|
|
* Time: 11:21
|
|
*/
|
|
|
|
use CMDBSource;
|
|
use MySQLTransactionNotClosedException;
|
|
use PHPUnit\Framework\TestCase;
|
|
use SetupUtils;
|
|
|
|
define('DEBUG_UNIT_TEST', true);
|
|
|
|
class ItopTestCase extends TestCase
|
|
{
|
|
const TEST_LOG_DIR = 'test';
|
|
|
|
/** @noinspection UsingInclusionOnceReturnValueInspection avoid errors for approot includes */
|
|
protected function setUp(): void
|
|
{
|
|
@include_once '../approot.inc.php';
|
|
@include_once '../../approot.inc.php';
|
|
@include_once '../../../approot.inc.php';
|
|
@include_once '../../../../approot.inc.php';
|
|
@include_once '../../../../../approot.inc.php';
|
|
@include_once '../../../../../../approot.inc.php';
|
|
@include_once '../../../../../../../approot.inc.php';
|
|
@include_once '../../../../../../../../approot.inc.php';
|
|
}
|
|
|
|
/**
|
|
* @throws \MySQLTransactionNotClosedException see N°5538
|
|
* @since 2.7.8 3.0.3 3.1.0 N°5538
|
|
*/
|
|
protected function tearDown(): void
|
|
{
|
|
parent::tearDown();
|
|
|
|
if (CMDBSource::IsInsideTransaction()) {
|
|
// Nested transactions were opened but not finished !
|
|
throw new MySQLTransactionNotClosedException('Some DB transactions were opened but not closed ! Fix the code by adding ROLLBACK or COMMIT statements !', []);
|
|
}
|
|
}
|
|
|
|
protected function debug($sMsg)
|
|
{
|
|
if (DEBUG_UNIT_TEST) {
|
|
if (is_string($sMsg)) {
|
|
echo "$sMsg\n";
|
|
} else {
|
|
/** @noinspection ForgottenDebugOutputInspection */
|
|
print_r($sMsg);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function GetMicroTime()
|
|
{
|
|
list($uSec, $sec) = explode(" ", microtime());
|
|
return ((float)$uSec + (float)$sec);
|
|
}
|
|
|
|
public function WriteToCsvHeader($sFilename, $aHeader)
|
|
{
|
|
$sResultFile = APPROOT.'log/'.$sFilename;
|
|
if (is_file($sResultFile))
|
|
{
|
|
@unlink($sResultFile);
|
|
}
|
|
SetupUtils::builddir(dirname($sResultFile));
|
|
file_put_contents($sResultFile, implode(';', $aHeader)."\n");
|
|
}
|
|
|
|
public function WriteToCsvData($sFilename, $aData)
|
|
{
|
|
$sResultFile = APPROOT.'log/'.$sFilename;
|
|
$file = fopen($sResultFile, 'a');
|
|
fputs($file, implode(';', $aData)."\n");
|
|
fclose($file);
|
|
}
|
|
|
|
public function GetTestId()
|
|
{
|
|
$sId = str_replace('"', '', $this->getName());
|
|
$sId = str_replace(' ', '_', $sId);
|
|
|
|
return $sId;
|
|
}
|
|
|
|
/**
|
|
* @since 2.7.4 3.0.0
|
|
*/
|
|
public function InvokeNonPublicStaticMethod($sObjectClass, $sMethodName, $aArgs)
|
|
{
|
|
return $this->InvokeNonPublicMethod($sObjectClass, $sMethodName, null, $aArgs);
|
|
}
|
|
|
|
/**
|
|
* @param string $sObjectClass for example DBObject::class
|
|
* @param string $sMethodName
|
|
* @param object $oObject
|
|
* @param array $aArgs
|
|
*
|
|
* @return mixed method result
|
|
*
|
|
* @throws \ReflectionException
|
|
*
|
|
* @since 2.7.4 3.0.0
|
|
*/
|
|
public function InvokeNonPublicMethod($sObjectClass, $sMethodName, $oObject, $aArgs)
|
|
{
|
|
$class = new \ReflectionClass($sObjectClass);
|
|
$method = $class->getMethod($sMethodName);
|
|
$method->setAccessible(true);
|
|
|
|
return $method->invokeArgs($oObject, $aArgs);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param object $oObject
|
|
* @param string $sProperty
|
|
*
|
|
* @return mixed property
|
|
*
|
|
* @throws \ReflectionException
|
|
* @since 2.7.8 3.0.3 3.1.0
|
|
*/
|
|
public function GetNonPublicProperty(object $oObject, string $sProperty)
|
|
{
|
|
$class = new \ReflectionClass(get_class($oObject));
|
|
$property = $class->getProperty($sProperty);
|
|
$property->setAccessible(true);
|
|
|
|
return $property->getValue($oObject);
|
|
}
|
|
|
|
/**
|
|
* @param object $oObject
|
|
* @param string $sProperty
|
|
* @param $value
|
|
*
|
|
* @throws \ReflectionException
|
|
* @since 2.7.8 3.0.3 3.1.0
|
|
*/
|
|
public function SetNonPublicProperty(object $oObject, string $sProperty, $value)
|
|
{
|
|
$class = new \ReflectionClass(get_class($oObject));
|
|
$property = $class->getProperty($sProperty);
|
|
$property->setAccessible(true);
|
|
|
|
$property->setValue($oObject, $value);
|
|
}
|
|
} |