1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-06 14:46:56 +02:00

Make sure e107_config.php backup functions correctly, even when the tests are halted.

This commit is contained in:
camer0n
2025-03-15 17:34:14 -07:00
parent f19607c780
commit fc19cabda7

View File

@@ -2,9 +2,6 @@
namespace Helper; namespace Helper;
include_once(codecept_root_dir() . "lib/preparers/PreparerFactory.php"); include_once(codecept_root_dir() . "lib/preparers/PreparerFactory.php");
// here you can define custom actions
// all public methods declared in helper class will be available in $I
use Codeception\Lib\ModuleContainer; use Codeception\Lib\ModuleContainer;
use PreparerFactory; use PreparerFactory;
use Twig\Environment; use Twig\Environment;
@@ -12,9 +9,10 @@ use Twig\Loader\ArrayLoader;
abstract class E107Base extends Base abstract class E107Base extends Base
{ {
const APP_PATH_E107_CONFIG = APP_PATH."/e107_config.php"; const APP_PATH_E107_CONFIG = APP_PATH . "/e107_config.php";
const E107_MYSQL_PREFIX = 'e107_'; const E107_MYSQL_PREFIX = 'e107_';
protected $preparer = null; protected $preparer = null;
private $configBackedUp = false; // Track if weve backed up the config
public function __construct(ModuleContainer $moduleContainer, $config = null) public function __construct(ModuleContainer $moduleContainer, $config = null)
{ {
@@ -22,7 +20,7 @@ abstract class E107Base extends Base
$this->preparer = PreparerFactory::create(); $this->preparer = PreparerFactory::create();
} }
public function _beforeSuite($settings = array()) public function _beforeSuite($settings = [])
{ {
$this->backupLocalE107Config(); $this->backupLocalE107Config();
$this->preparer->snapshot(); $this->preparer->snapshot();
@@ -32,16 +30,16 @@ abstract class E107Base extends Base
protected function backupLocalE107Config() protected function backupLocalE107Config()
{ {
if(file_exists(self::APP_PATH_E107_CONFIG)) if (file_exists(self::APP_PATH_E107_CONFIG)) {
{ rename(self::APP_PATH_E107_CONFIG, APP_PATH . '/e107_config.php.bak');
rename(self::APP_PATH_E107_CONFIG, APP_PATH.'/e107_config.php.bak'); $this->configBackedUp = true; // Mark as backed up
} }
} }
protected function writeLocalE107Config() protected function writeLocalE107Config()
{ {
$twig_loader = new ArrayLoader([ $twig_loader = new ArrayLoader([
'e107_config.php' => file_get_contents(codecept_data_dir()."/e107_config.php.sample") 'e107_config.php' => file_get_contents(codecept_data_dir() . "/e107_config.php.sample")
]); ]);
$twig = new Environment($twig_loader); $twig = new Environment($twig_loader);
@@ -66,29 +64,31 @@ abstract class E107Base extends Base
$this->restoreLocalE107Config(); $this->restoreLocalE107Config();
$this->workaroundOldPhpUnitPhpCodeCoverage(); $this->workaroundOldPhpUnitPhpCodeCoverage();
} }
/*
public function _failed($test, $fail) /**
* Destructor: Ensures cleanup even on crashes or fatal errors.
*/
public function __destruct()
{ {
parent::_failed($test, $fail); // Only restore if we backed up and havent already restored
if ($this->configBackedUp && file_exists(APP_PATH . '/e107_config.php.bak')) {
$this->revokeLocalE107Config(); $this->revokeLocalE107Config();
$this->preparer->rollback();
$this->restoreLocalE107Config(); $this->restoreLocalE107Config();
$this->workaroundOldPhpUnitPhpCodeCoverage(); }
}
}*/
protected function revokeLocalE107Config() protected function revokeLocalE107Config()
{ {
if (file_exists(self::APP_PATH_E107_CONFIG)) if (file_exists(self::APP_PATH_E107_CONFIG)) {
unlink(self::APP_PATH_E107_CONFIG); unlink(self::APP_PATH_E107_CONFIG);
} }
}
protected function restoreLocalE107Config() protected function restoreLocalE107Config()
{ {
if(file_exists(APP_PATH."/e107_config.php.bak")) if (file_exists(APP_PATH . "/e107_config.php.bak")) {
{ rename(APP_PATH . '/e107_config.php.bak', self::APP_PATH_E107_CONFIG);
rename(APP_PATH.'/e107_config.php.bak', self::APP_PATH_E107_CONFIG); $this->configBackedUp = false; // Reset flag after restoration
} }
} }
@@ -100,13 +100,11 @@ abstract class E107Base extends Base
{ {
$composer_installed_file = codecept_absolute_path("vendor/composer/installed.json"); $composer_installed_file = codecept_absolute_path("vendor/composer/installed.json");
$composer_installed = json_decode(file_get_contents($composer_installed_file)); $composer_installed = json_decode(file_get_contents($composer_installed_file));
if (isset($composer_installed->packages)) if (isset($composer_installed->packages)) {
{
// Composer 2 format for the installed packages manifest // Composer 2 format for the installed packages manifest
$composer_installed = $composer_installed->packages; $composer_installed = $composer_installed->packages;
} }
$installed_phpunit_php_code_coverage = current(array_filter($composer_installed, function ($element) $installed_phpunit_php_code_coverage = current(array_filter($composer_installed, function ($element) {
{
return $element->name == 'phpunit/php-code-coverage'; return $element->name == 'phpunit/php-code-coverage';
})); }));
if (version_compare($installed_phpunit_php_code_coverage->version_normalized, '6.0.8', '>=')) if (version_compare($installed_phpunit_php_code_coverage->version_normalized, '6.0.8', '>='))