Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Tkachev committed Feb 17, 2025
1 parent 1efb560 commit a3b3637
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Admin/ModuleConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private function saveMerchantIdentifier(string $systemMode): void

$isValid = $this->validateCredentialsForSaving($newValue, $errorIds);

if ($isValid && $this->isUpdate) {
if ($isValid && $this->isUpdate && $newValue !== null) {
$this->moduleSettings->setApplePayMerchantIdentifier($newValue);
}
}
Expand Down
90 changes: 54 additions & 36 deletions tests/Unit/Service/ModuleSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,57 +181,77 @@ protected function setUp(): void
{
parent::setUp();

// Backup the var directory
$this->backupVarDirectory();

$container = ContainerFactory::getInstance()->getContainer();
$this->moduleSettingService = $container->get(ModuleSettingServiceInterface::class);
$this->backupSettings();
}

/**
* @throws \Exception
*/
protected function tearDown(): void
{
$this->restoreSettings();
parent::tearDown();
}
// Restore var directory first
$this->restoreVarDirectory();

private function backupSettings(): void
{
foreach ($this->settingsToBackup as $settingName) {
if ($this->moduleSettingService->exists($settingName, Module::MODULE_ID)) {
try {
$this->originalSettings[$settingName] = $this->getSettingWithType($settingName);
} catch (\Exception $e) {
continue;
// Get fresh instance of module setting service
$container = ContainerFactory::getInstance()->getContainer();
$moduleSettingService = $container->get(ModuleSettingServiceInterface::class);

// Set sandbox mode
$moduleSettingService->saveInteger('UnzerSystemMode', 0, Module::MODULE_ID);

// Clear all production-related fields
$productionFields = array_filter($this->settingsToBackup, function($setting) {
return str_starts_with($setting, 'production-');
});

foreach ($productionFields as $field) {
try {
if (strpos($field, 'Key') !== false) {
$moduleSettingService->saveString($field, '', Module::MODULE_ID);
} elseif (strpos($field, 'webhookConfiguration') !== false) {
$moduleSettingService->saveCollection($field, [], Module::MODULE_ID);
}
} catch (\Exception $e) {
continue;
}
}

// Reinitialize module events
(new \OxidSolutionCatalysts\Unzer\Core\Events())->onActivate();

parent::tearDown();
}

private function getSettingWithType(string $settingName): array
private function backupVarDirectory(): void
{
if (in_array($settingName, ['UnzerDebug', 'UnzerjQuery'])) {
return [
'value' => $this->moduleSettingService->getBoolean($settingName, Module::MODULE_ID),
'type' => 'boolean'
];
}
$varDir = '/var/www/var';
$this->varDirBackup = '/tmp/var_backup_' . uniqid();

if ($settingName === 'UnzerSystemMode') {
return [
'value' => $this->moduleSettingService->getInteger($settingName, Module::MODULE_ID),
'type' => 'integer'
];
if (is_dir($varDir)) {
exec("cp -r {$varDir} {$this->varDirBackup}");
}
}

if ($settingName === 'webhookConfiguration') {
return [
'value' => $this->moduleSettingService->getCollection($settingName, Module::MODULE_ID),
'type' => 'collection'
];
}
private function restoreVarDirectory(): void
{
$varDir = '/var/www/var';
$confModuleYamlBkp = '/var/www/var/configuration/environment/shops/1/modules/osc-unzer.yaml.bak';
$conModuleYaml = '/var/www/var/configuration/environment/shops/1/modules/osc-unzer.yaml';

return [
'value' => $this->moduleSettingService->getString($settingName, Module::MODULE_ID)->toString(),
'type' => 'string'
];
if (is_dir($this->varDirBackup)) {
// Remove current var directory
exec("rm -rf {$varDir}");
// Restore from backup
exec("cp -r {$this->varDirBackup} {$varDir}");
// Clean up backup
exec("rm -rf {$this->varDirBackup}");

exec("cp {$confModuleYamlBkp} {$conModuleYaml}");
}
}

private function restoreSettings(): void
Expand Down Expand Up @@ -269,11 +289,9 @@ private function configureModuleSettingServiceMock(
string $moduleId,
$value
): void {
// Configure exists method
$mock->method('exists')
->willReturn(true);

// Configure getInteger for system mode
if ($name === 'UnzerSystemMode') {
$mock->method('getInteger')
->with($name, $moduleId)
Expand Down

0 comments on commit a3b3637

Please sign in to comment.