Skip to content

Commit

Permalink
update tests and styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Tkachev committed Feb 17, 2025
1 parent 501459b commit 1efb560
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ jobs:
- name: Activate theme
run: |
docker compose exec -T php bin/oe-console oe:theme:activate apex
cp source/var/configuration/environment/shops/1/modules/osc-unzer.yaml.bak source/var/configuration/environment/shops/1/modules/osc-unzer.yaml
- name: Run tests
run: |
Expand Down
53 changes: 42 additions & 11 deletions src/Service/ModuleSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OxidEsales\EshopCommunity\Core\Exception\FileException;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ModuleConfigurationDaoBridgeInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ModuleSettingBridgeInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
use OxidEsales\Facts\Facts;
use OxidSolutionCatalysts\Unzer\Module;
use Exception;
Expand Down Expand Up @@ -42,18 +43,18 @@ class ModuleSettings
'visa' => '1'
];

private ModuleSettingBridgeInterface $moduleSettingBridge;
private ModuleSettingServiceInterface $moduleSettingService;
private ModuleConfigurationDaoBridgeInterface $moduleInfoBridge;
private Session $session;
private Config $config;

public function __construct(
ModuleSettingBridgeInterface $moduleSettingBridge,
ModuleSettingServiceInterface $moduleSettingService,
ModuleConfigurationDaoBridgeInterface $moduleInfoBridge,
Session $session,
Config $config
) {
$this->moduleSettingBridge = $moduleSettingBridge;
$this->moduleSettingService = $moduleSettingService;
$this->moduleInfoBridge = $moduleInfoBridge;
$this->session = $session;
$this->config = $config;
Expand Down Expand Up @@ -340,7 +341,11 @@ public function saveApplePayNetworks(array $networks): void

public function saveWebhookConfiguration(array $webhookConfig): void
{
$this->moduleSettingBridge->save('webhookConfiguration', $webhookConfig, Module::MODULE_ID);
$this->moduleSettingService->saveCollection(
'webhookConfiguration',
$webhookConfig,
Module::MODULE_ID
);
}

public function getWebhookConfiguration(): array
Expand Down Expand Up @@ -380,20 +385,46 @@ public function getPrivateKeysWithContext(): array

private function saveSetting(string $name, bool|int|string|array $setting): void
{
$this->moduleSettingBridge->save($name, $setting, Module::MODULE_ID);
if (is_string($setting)) {
$this->moduleSettingService->saveString($name, $setting, Module::MODULE_ID);
}
if (is_bool($setting)) {
$this->moduleSettingService->saveBoolean($name, $setting, Module::MODULE_ID);
} elseif (is_int($setting)) {
$this->moduleSettingService->saveInteger($name, $setting, Module::MODULE_ID);
} elseif (is_array($setting)) {
$this->moduleSettingService->saveCollection($name, $setting, Module::MODULE_ID);
}
}

private function getSettingValue(string $key): mixed
{
$result = '';
try {
$result = $this->moduleSettingBridge->get($key, Module::MODULE_ID);
} catch (ModuleConfigurationNotFoundException $exception) {
//todo: improve
if (!$this->moduleSettingService->exists($key, Module::MODULE_ID)) {
return '';
}

// Try to get as boolean first for known boolean settings
if (in_array($key, ['UnzerDebug', 'UnzerjQuery'])) {
return $this->moduleSettingService->getBoolean($key, Module::MODULE_ID);
}

// Try to get as integer for known integer settings
if ($key === 'UnzerSystemMode') {
return $this->moduleSettingService->getInteger($key, Module::MODULE_ID);
}

// Try to get as array for known array settings
if (in_array($key, ['webhookConfiguration', 'applepay_merchant_capabilities', 'applepay_networks'])) {
return $this->moduleSettingService->getCollection($key, Module::MODULE_ID);
}

// Default to string for all other settings
return $this->moduleSettingService->getString($key, Module::MODULE_ID)->toString();
} catch (\Exception $exception) {
return '';
}
return $result;
}

/**
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
Expand Down
194 changes: 186 additions & 8 deletions tests/Unit/Service/ModuleSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,79 @@
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Session;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ModuleConfigurationDaoBridgeInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ModuleSettingBridgeInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
use OxidSolutionCatalysts\Unzer\Module;
use OxidSolutionCatalysts\Unzer\Service\ModuleSettings;
use PHPUnit\Framework\TestCase;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use Symfony\Component\String\UnicodeString;

class ModuleSettingsTest extends TestCase
{
private ModuleSettingServiceInterface $moduleSettingService;
private array $originalSettings = [];
private array $settingsToBackup = [
'UnzerDebug',
'UnzerjQuery',
'UnzerSystemMode',
'sandbox-UnzerPublicKey',
'production-UnzerPublicKey',
'sandbox-UnzerPrivateKey',
'production-UnzerPrivateKey',
'webhookConfiguration',
// Sandbox Invoice keys
'sandbox-UnzerPayLaterInvoiceB2BEURPublicKey',
'sandbox-UnzerPayLaterInvoiceB2CEURPrivateKey',
'sandbox-UnzerPayLaterInvoiceB2CCHFPublicKey',
'sandbox-UnzerPayLaterInvoiceB2CEURPublicKey',
'sandbox-UnzerPayLaterInvoiceB2BEURPrivateKey',
'sandbox-UnzerPayLaterInvoiceB2CCHFPrivateKey',
'sandbox-UnzerPayLaterInvoiceB2BCHFPrivateKey',
'sandbox-UnzerPayLaterInvoiceB2BCHFPublicKey',
// Sandbox Installment keys
'sandbox-UnzerPayLaterInstallmentB2CEURPrivateKey',
'sandbox-UnzerPayLaterInstallmentB2CEURPublicKey',
'sandbox-UnzerPayLaterInstallmentB2CCHFPrivateKey',
'sandbox-UnzerPayLaterInstallmentB2CCHFPublicKey',
// Production Invoice keys
'production-UnzerPayLaterInvoiceB2BEURPublicKey',
'production-UnzerPayLaterInvoiceB2CEURPrivateKey',
'production-UnzerPayLaterInvoiceB2CCHFPublicKey',
'production-UnzerPayLaterInvoiceB2CEURPublicKey',
'production-UnzerPayLaterInvoiceB2BEURPrivateKey',
'production-UnzerPayLaterInvoiceB2CCHFPrivateKey',
'production-UnzerPayLaterInvoiceB2BCHFPrivateKey',
'production-UnzerPayLaterInvoiceB2BCHFPublicKey',
// Production Installment keys
'production-UnzerPayLaterInstallmentB2CEURPrivateKey',
'production-UnzerPayLaterInstallmentB2CEURPublicKey',
'production-UnzerPayLaterInstallmentB2CCHFPrivateKey',
'production-UnzerPayLaterInstallmentB2CCHFPublicKey'
];

/**
* @dataProvider getSettingsDataProvider
*/
public function testSettings($values, $settingMethod, $settingValue): void
{
$mockModuleSettingService = $this->createMock(ModuleSettingServiceInterface::class);
$session = $this->createConfiguredMock(Session::class, []);
$config = $this->createConfiguredMock(Config::class, []);


foreach ($values as [$name, $moduleId, $value]) {
$this->configureModuleSettingServiceMock($mockModuleSettingService, $name, $moduleId, $value);
}

$sut = new ModuleSettings(
$this->getBridgeStub($values),
$mockModuleSettingService,
$this->getMockBuilder(ModuleConfigurationDaoBridgeInterface::class)
->disableOriginalConstructor()
->getMock(),
$session,
$config
);

$this->assertSame($settingValue, $sut->$settingMethod());
}

Expand Down Expand Up @@ -126,13 +177,140 @@ public function getSettingsDataProvider(): array
];
}

private function getBridgeStub($valueMap): ModuleSettingBridgeInterface
protected function setUp(): void
{
parent::setUp();

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

protected function tearDown(): void
{
$this->restoreSettings();
parent::tearDown();
}

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;
}
}
}
}

private function getSettingWithType(string $settingName): array
{
if (in_array($settingName, ['UnzerDebug', 'UnzerjQuery'])) {
return [
'value' => $this->moduleSettingService->getBoolean($settingName, Module::MODULE_ID),
'type' => 'boolean'
];
}

if ($settingName === 'UnzerSystemMode') {
return [
'value' => $this->moduleSettingService->getInteger($settingName, Module::MODULE_ID),
'type' => 'integer'
];
}

if ($settingName === 'webhookConfiguration') {
return [
'value' => $this->moduleSettingService->getCollection($settingName, Module::MODULE_ID),
'type' => 'collection'
];
}

return [
'value' => $this->moduleSettingService->getString($settingName, Module::MODULE_ID)->toString(),
'type' => 'string'
];
}

private function restoreSettings(): void
{
foreach ($this->originalSettings as $settingName => $data) {
try {
$this->restoreSetting($settingName, $data);
} catch (\Exception $e) {
continue;
}
}
}

private function restoreSetting(string $settingName, array $data): void
{
$bridgeStub = $this->getMockBuilder(ModuleSettingBridgeInterface::class)
->onlyMethods(['save', 'get'])
->getMock();
$bridgeStub->method('get')->willReturnMap($valueMap);
switch ($data['type']) {
case 'boolean':
$this->moduleSettingService->saveBoolean($settingName, $data['value'], Module::MODULE_ID);
break;
case 'integer':
$this->moduleSettingService->saveInteger($settingName, $data['value'], Module::MODULE_ID);
break;
case 'string':
$this->moduleSettingService->saveString($settingName, $data['value'], Module::MODULE_ID);
break;
case 'collection':
$this->moduleSettingService->saveCollection($settingName, $data['value'], Module::MODULE_ID);
break;
}
}

private function configureModuleSettingServiceMock(
ModuleSettingServiceInterface $mock,
string $name,
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)
->willReturn($value);
} elseif (
strpos($name, 'UnzerPrivateKey') !== false || strpos($name, 'UnzerPublicKey') !== false
|| $name === 'UnzerPublicKey' || $name === 'UnzerPrivateKey'
) {
static $keyMap = [];
$keyMap[$moduleId][$name] = $value;

$mock->method('getString')
->willReturnCallback(function ($callName, $callModuleId) use ($keyMap, $mock) {
if (isset($keyMap[$callModuleId][$callName])) {
return new UnicodeString($keyMap[$callModuleId][$callName]);
}

if ($callName === 'UnzerPublicKey' || $callName === 'UnzerPrivateKey') {
$systemMode = $mock->getInteger('UnzerSystemMode', $callModuleId);
$prefix = $systemMode === 1 ? 'production-' : 'sandbox-';
$fullKey = $prefix . $callName;

if (isset($keyMap[$callModuleId][$fullKey])) {
return new UnicodeString($keyMap[$callModuleId][$fullKey]);
}
}

return $bridgeStub;
return new UnicodeString('');
});
} elseif (in_array($name, ['UnzerDebug', 'UnzerjQuery'])) {
$mock->method('getBoolean')
->with($name, $moduleId)
->willReturn($value);
} elseif ($name === 'webhookConfiguration' || strpos($name, 'applepay_') === 0) {
$mock->method('getCollection')
->with($name, $moduleId)
->willReturn($value);
}
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Service/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected function getTranslatorMock()
{
$translatorMock = $this->createPartialMock(Translator::class, ['translateCode']);
$translatorMock->method('translateCode')
->willReturnCallback(function($message, $key) {
->willReturnCallback(function ($message, $key) {
if ($key === "clientMessage") {
return "specialTranslation";
}
Expand Down

0 comments on commit 1efb560

Please sign in to comment.