From 019c7ca8194124a160254c3e37dbb018d51404d9 Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Tue, 5 Dec 2023 14:09:28 +0200 Subject: [PATCH] OXDEV-7521 Remove module setting repository Signed-off-by: Anton Fedurtsya --- .../ModuleSettingRepository.php | 84 ------ .../ModuleSettingRepositoryInterface.php | 38 --- src/Setting/Service/ModuleSettingService.php | 36 ++- src/Setting/services.yaml | 3 - .../ModuleSettingRepositoryTest.php | 279 ------------------ .../Unit/Service/ModuleSettingServiceTest.php | 216 ++++++++------ 6 files changed, 147 insertions(+), 509 deletions(-) delete mode 100644 src/Setting/Infrastructure/ModuleSettingRepository.php delete mode 100644 src/Setting/Infrastructure/ModuleSettingRepositoryInterface.php delete mode 100644 tests/Unit/Infrastructure/ModuleSettingRepositoryTest.php diff --git a/src/Setting/Infrastructure/ModuleSettingRepository.php b/src/Setting/Infrastructure/ModuleSettingRepository.php deleted file mode 100644 index d18b50f..0000000 --- a/src/Setting/Infrastructure/ModuleSettingRepository.php +++ /dev/null @@ -1,84 +0,0 @@ -moduleSettingService->getInteger($name, $moduleId); - } - - public function getFloatSetting(string $name, string $moduleId): float - { - return $this->moduleSettingService->getFloat($name, $moduleId); - } - - public function getBooleanSetting(string $name, string $moduleId): bool - { - return $this->moduleSettingService->getBoolean($name, $moduleId); - } - - public function getStringSetting(string $name, string $moduleId): string - { - return (string)$this->moduleSettingService->getString($name, $moduleId); - } - - public function getCollectionSetting(string $name, string $moduleId): array - { - return $this->moduleSettingService->getCollection($name, $moduleId); - } - - public function saveIntegerSetting(string $name, int $value, string $moduleId): void - { - $this->moduleSettingService->saveInteger($name, $value, $moduleId); - } - - public function saveFloatSetting(string $name, float $value, string $moduleId): void - { - $this->moduleSettingService->saveFloat($name, $value, $moduleId); - } - - public function saveBooleanSetting(string $name, bool $value, string $moduleId): void - { - $this->moduleSettingService->saveBoolean($name, $value, $moduleId); - } - - public function saveStringSetting(string $name, string $value, string $moduleId): void - { - $this->moduleSettingService->saveString($name, $value, $moduleId); - } - - public function saveCollectionSetting(string $name, array $value, string $moduleId): void - { - $this->moduleSettingService->saveCollection($name, $value, $moduleId); - } - - /** - * @return Setting[] - */ - public function getSettingsList(string $moduleId): array - { - $moduleConfiguration = $this->moduleConfigurationDao->get($moduleId, $this->basicContext->getCurrentShopId()); - return $moduleConfiguration->getModuleSettings(); - } -} diff --git a/src/Setting/Infrastructure/ModuleSettingRepositoryInterface.php b/src/Setting/Infrastructure/ModuleSettingRepositoryInterface.php deleted file mode 100644 index ecb38a8..0000000 --- a/src/Setting/Infrastructure/ModuleSettingRepositoryInterface.php +++ /dev/null @@ -1,38 +0,0 @@ -moduleSettingRepository->getIntegerSetting($name, $moduleId) + $this->moduleSettingService->getInteger($name, $moduleId) ); } @@ -37,7 +43,7 @@ public function getFloatSetting(string $name, string $moduleId): FloatSetting { return new FloatSetting( $name, - $this->moduleSettingRepository->getFloatSetting($name, $moduleId) + $this->moduleSettingService->getFloat($name, $moduleId) ); } @@ -45,7 +51,7 @@ public function getBooleanSetting(string $name, string $moduleId): BooleanSettin { return new BooleanSetting( $name, - $this->moduleSettingRepository->getBooleanSetting($name, $moduleId) + $this->moduleSettingService->getBoolean($name, $moduleId) ); } @@ -53,13 +59,13 @@ public function getStringSetting(string $name, string $moduleId): StringSetting { return new StringSetting( $name, - $this->moduleSettingRepository->getStringSetting($name, $moduleId) + (string)$this->moduleSettingService->getString($name, $moduleId) ); } public function getCollectionSetting(string $name, string $moduleId): StringSetting { - $collection = $this->moduleSettingRepository->getCollectionSetting($name, $moduleId); + $collection = $this->moduleSettingService->getCollection($name, $moduleId); return new StringSetting( $name, @@ -69,35 +75,35 @@ public function getCollectionSetting(string $name, string $moduleId): StringSett public function changeIntegerSetting(string $name, int $value, string $moduleId): IntegerSetting { - $this->moduleSettingRepository->saveIntegerSetting($name, $value, $moduleId); + $this->moduleSettingService->saveInteger($name, $value, $moduleId); return $this->getIntegerSetting($name, $moduleId); } public function changeFloatSetting(string $name, float $value, string $moduleId): FloatSetting { - $this->moduleSettingRepository->saveFloatSetting($name, $value, $moduleId); + $this->moduleSettingService->saveFloat($name, $value, $moduleId); return $this->getFloatSetting($name, $moduleId); } public function changeBooleanSetting(string $name, bool $value, string $moduleId): BooleanSetting { - $this->moduleSettingRepository->saveBooleanSetting($name, $value, $moduleId); + $this->moduleSettingService->saveBoolean($name, $value, $moduleId); return $this->getBooleanSetting($name, $moduleId); } public function changeStringSetting(string $name, string $value, string $moduleId): StringSetting { - $this->moduleSettingRepository->saveStringSetting($name, $value, $moduleId); + $this->moduleSettingService->saveString($name, $value, $moduleId); return $this->getStringSetting($name, $moduleId); } public function changeCollectionSetting(string $name, string $value, string $moduleId): StringSetting { - $this->moduleSettingRepository->saveCollectionSetting( + $this->moduleSettingService->saveCollection( $name, $this->jsonService->jsonDecodeCollection($value), $moduleId @@ -111,7 +117,9 @@ public function changeCollectionSetting(string $name, string $value, string $mod */ public function getSettingsList(string $moduleId): array { - $settingsList = $this->moduleSettingRepository->getSettingsList($moduleId); + $moduleConfiguration = $this->moduleConfigurationDao->get($moduleId, $this->basicContext->getCurrentShopId()); + $settingsList = $moduleConfiguration->getModuleSettings(); + $settingTypes = []; /** @var Setting $setting */ foreach ($settingsList as $setting) { diff --git a/src/Setting/services.yaml b/src/Setting/services.yaml index ea4fe38..efcba98 100644 --- a/src/Setting/services.yaml +++ b/src/Setting/services.yaml @@ -13,9 +13,6 @@ services: OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\ThemeSettingServiceInterface: class: OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\ThemeSettingService - OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ModuleSettingRepositoryInterface: - class: OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ModuleSettingRepository - OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ThemeSettingRepositoryInterface: class: OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ThemeSettingRepository diff --git a/tests/Unit/Infrastructure/ModuleSettingRepositoryTest.php b/tests/Unit/Infrastructure/ModuleSettingRepositoryTest.php deleted file mode 100644 index 1d010b3..0000000 --- a/tests/Unit/Infrastructure/ModuleSettingRepositoryTest.php +++ /dev/null @@ -1,279 +0,0 @@ -getSut( - moduleSettingService: $this->getShopModuleSettingServiceMethodStub( - 'getInteger', - $name, - $moduleId, - $shopServiceReturn - ) - ); - - $this->assertEquals( - $shopServiceReturn, - $moduleRepository->getIntegerSetting($name, $moduleId) - ); - } - - public function testGetModuleSettingFloat(): void - { - $name = 'floatSetting'; - $shopServiceReturn = 1.23; - $moduleId = 'awesomeModule'; - - $moduleRepository = $this->getSut( - moduleSettingService: $this->getShopModuleSettingServiceMethodStub( - 'getFloat', - $name, - $moduleId, - $shopServiceReturn - ), - ); - - $this->assertEquals( - $shopServiceReturn, - $moduleRepository->getFloatSetting($name, $moduleId) - ); - } - - public function testGetModuleSettingBoolean(): void - { - $name = 'booleanSetting'; - $shopServiceReturn = false; - $moduleId = 'awesomeModule'; - - $moduleRepository = $this->getSut( - moduleSettingService: $this->getShopModuleSettingServiceMethodStub( - 'getBoolean', - $name, - $moduleId, - $shopServiceReturn - ), - ); - - $this->assertEquals( - $shopServiceReturn, - $moduleRepository->getBooleanSetting($name, $moduleId) - ); - } - - public function testGetModuleSettingString(): void - { - $name = 'stringSetting'; - $moduleId = 'awesomeModule'; - $repositoryReturn = 'default'; - - $moduleRepository = $this->getSut( - moduleSettingService: $this->getShopModuleSettingServiceMethodStub( - 'getString', - $name, - $moduleId, - new UnicodeString($repositoryReturn) - ), - ); - - $this->assertEquals( - $repositoryReturn, - $moduleRepository->getStringSetting($name, $moduleId) - ); - } - - public function testGetModuleSettingCollection(): void - { - $name = 'arraySetting'; - $moduleId = 'awesomeModule'; - $repositoryReturn = ['nice', 'values']; - - $moduleRepository = $this->getSut( - moduleSettingService: $this->getShopModuleSettingServiceMethodStub( - 'getCollection', - $name, - $moduleId, - $repositoryReturn - ), - ); - - $this->assertEquals( - $repositoryReturn, - $moduleRepository->getCollectionSetting($name, $moduleId) - ); - } - - public function testSaveIntegerSettingProxyingDataToShopService(): void - { - $name = 'intSetting'; - $moduleId = 'awesomeModule'; - - $callValue = 123; - $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); - $moduleSettingService->expects($this->once()) - ->method('saveInteger') - ->with($name, $callValue, $moduleId); - - $moduleRepository = $this->getSut( - moduleSettingService: $moduleSettingService - ); - - $moduleRepository->saveIntegerSetting($name, $callValue, $moduleId); - } - - public function testSaveFloatSettingProxyingDataToShopService(): void - { - $name = 'floatSetting'; - $moduleId = 'awesomeModule'; - - $callValue = 1.23; - $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); - $moduleSettingService->expects($this->once()) - ->method('saveFloat') - ->with($name, $callValue, $moduleId); - - $moduleRepository = $this->getSut( - moduleSettingService: $moduleSettingService - ); - - $moduleRepository->saveFloatSetting($name, $callValue, $moduleId); - } - - public function testSaveBooleanSettingProxyingDataToShopService(): void - { - $name = 'boolSetting'; - $moduleId = 'awesomeModule'; - - $callValue = false; - $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); - $moduleSettingService->expects($this->once()) - ->method('saveBoolean') - ->with($name, $callValue, $moduleId); - - $moduleRepository = $this->getSut( - moduleSettingService: $moduleSettingService - ); - - $moduleRepository->saveBooleanSetting($name, $callValue, $moduleId); - } - - public function testSaveStringSettingProxyingDataToShopService(): void - { - $name = 'stringSetting'; - $moduleId = 'awesomeModule'; - - $callValue = 'default'; - $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); - $moduleSettingService->expects($this->once()) - ->method('saveString') - ->with($name, $callValue, $moduleId); - - $moduleRepository = $this->getSut( - moduleSettingService: $moduleSettingService - ); - - $moduleRepository->saveStringSetting($name, $callValue, $moduleId); - } - - public function testSaveCollectionSettingProxyingDataToShopService(): void - { - $name = 'collectionSetting'; - $moduleId = 'awesomeModule'; - - $callValue = [3, 'interesting', 'values']; - $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); - $moduleSettingService->expects($this->once()) - ->method('saveCollection') - ->with($name, $callValue, $moduleId); - - $moduleRepository = $this->getSut( - moduleSettingService: $moduleSettingService - ); - - $moduleRepository->saveCollectionSetting($name, $callValue, $moduleId); - } - - public function testGetSettingsList(): void - { - $shopId = 3; - $moduleId = 'awesomeModule'; - - $response = [ - $this->createStub(Setting::class), - $this->createStub(Setting::class), - ]; - - $moduleConfigurationDao = $this->createMock(ModuleConfigurationDaoInterface::class); - $moduleConfigurationDao->expects($this->once()) - ->method('get') - ->with($moduleId, $shopId) - ->willReturn( - $this->createConfiguredMock(ModuleConfiguration::class, [ - 'getModuleSettings' => $response - ]) - ); - - $moduleRepository = $this->getSut( - moduleConfigurationDao: $moduleConfigurationDao, - basicContext: $this->getBasicContextMock($shopId), - ); - - $this->assertSame( - $response, - $moduleRepository->getSettingsList($moduleId) - ); - } - - public function getSut( - ?ModuleSettingServiceInterface $moduleSettingService = null, - ?ModuleConfigurationDaoInterface $moduleConfigurationDao = null, - ?BasicContextInterface $basicContext = null, - ): ModuleSettingRepository { - $moduleConfigurationDao = $moduleConfigurationDao ?? $this->createStub(ModuleConfigurationDaoInterface::class); - - return new ModuleSettingRepository( - moduleSettingService: $moduleSettingService ?? $this->createStub(ModuleSettingServiceInterface::class), - moduleConfigurationDao: $moduleConfigurationDao, - basicContext: $basicContext ?? $this->createStub(BasicContextInterface::class) - ); - } - - private function getShopModuleSettingServiceMethodStub( - string $methodName, - string $name, - string $moduleId, - $shopServiceReturn - ): ModuleSettingServiceInterface { - $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); - $moduleSettingService->expects($this->once()) - ->method($methodName) - ->with($name, $moduleId) - ->willReturn($shopServiceReturn); - return $moduleSettingService; - } -} diff --git a/tests/Unit/Service/ModuleSettingServiceTest.php b/tests/Unit/Service/ModuleSettingServiceTest.php index 2a61202..0be0214 100644 --- a/tests/Unit/Service/ModuleSettingServiceTest.php +++ b/tests/Unit/Service/ModuleSettingServiceTest.php @@ -9,7 +9,11 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Service; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Dao\ModuleConfigurationDaoInterface; use OxidEsales\EshopCommunity\Internal\Framework\Module\Setting\Setting; +use OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface; use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\BooleanSetting; use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\FloatSetting; use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\IntegerSetting; @@ -19,6 +23,7 @@ use OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\JsonServiceInterface; use OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\ModuleSettingService; use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase; +use Symfony\Component\String\UnicodeString; /** * @covers \OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\ModuleSettingService @@ -27,20 +32,21 @@ class ModuleSettingServiceTest extends UnitTestCase { public function testGetModuleSettingInteger(): void { - $name = 'integerSetting'; + $name = 'someSetting'; $moduleId = 'awesomeModule'; - $repositoryResponse = 123; - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - $repository->expects($this->once()) - ->method('getIntegerSetting') - ->with($name, $moduleId) - ->willReturn($repositoryResponse); - - $sut = $this->getSut($repository); + $shopServiceReturn = 123; + $sut = $this->getSut( + moduleSettingService: $this->getShopModuleSettingServiceMethodStub( + 'getInteger', + $name, + $moduleId, + $shopServiceReturn + ) + ); $this->assertEquals( - new IntegerSetting($name, $repositoryResponse), + new IntegerSetting($name, $shopServiceReturn), $sut->getIntegerSetting($name, $moduleId) ); } @@ -50,17 +56,18 @@ public function testGetModuleSettingFloat(): void $name = 'floatSetting'; $moduleId = 'awesomeModule'; - $repositoryResponse = 1.23; - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - $repository->expects($this->once()) - ->method('getFloatSetting') - ->with($name, $moduleId) - ->willReturn($repositoryResponse); - - $sut = $this->getSut($repository); + $shopServiceReturn = 1.23; + $sut = $this->getSut( + moduleSettingService: $this->getShopModuleSettingServiceMethodStub( + 'getFloat', + $name, + $moduleId, + $shopServiceReturn + ), + ); $this->assertEquals( - new FloatSetting($name, $repositoryResponse), + new FloatSetting($name, $shopServiceReturn), $sut->getFloatSetting($name, $moduleId) ); } @@ -70,17 +77,18 @@ public function testGetModuleSettingBoolean(): void $name = 'booleanSetting'; $moduleId = 'awesomeModule'; - $repositoryResponse = false; - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - $repository->expects($this->once()) - ->method('getBooleanSetting') - ->with($name, $moduleId) - ->willReturn($repositoryResponse); - - $sut = $this->getSut($repository); + $shopServiceReturn = false; + $sut = $this->getSut( + moduleSettingService: $this->getShopModuleSettingServiceMethodStub( + 'getBoolean', + $name, + $moduleId, + $shopServiceReturn + ), + ); $this->assertEquals( - new BooleanSetting($name, $repositoryResponse), + new BooleanSetting($name, $shopServiceReturn), $sut->getBooleanSetting($name, $moduleId) ); } @@ -90,17 +98,18 @@ public function testGetModuleSettingString(): void $name = 'stringSetting'; $moduleId = 'awesomeModule'; - $repositoryResponse = 'default'; - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - $repository->expects($this->once()) - ->method('getStringSetting') - ->with($name, $moduleId) - ->willReturn($repositoryResponse); - - $sut = $this->getSut($repository); + $shopServiceReturn = 'default'; + $sut = $this->getSut( + moduleSettingService: $this->getShopModuleSettingServiceMethodStub( + 'getString', + $name, + $moduleId, + new UnicodeString($shopServiceReturn) + ), + ); $this->assertEquals( - new StringSetting($name, $repositoryResponse), + new StringSetting($name, $shopServiceReturn), $sut->getStringSetting($name, $moduleId) ); } @@ -109,23 +118,22 @@ public function testGetModuleSettingCollection(): void { $name = 'arraySetting'; $moduleId = 'awesomeModule'; - - $repositoryResponse = ['nice', 'values']; - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - $repository->expects($this->once()) - ->method('getCollectionSetting') - ->with($name, $moduleId) - ->willReturn($repositoryResponse); + $shopServiceReturn = ['nice', 'values']; $encoderResponse = 'encoderResponse'; $encoder = $this->createMock(JsonServiceInterface::class); $encoder->method('jsonEncodeArray') - ->with($repositoryResponse) + ->with($shopServiceReturn) ->willReturn($encoderResponse); $sut = $this->getSut( - repository: $repository, - jsonService: $encoder + jsonService: $encoder, + moduleSettingService: $this->getShopModuleSettingServiceMethodStub( + 'getCollection', + $name, + $moduleId, + $shopServiceReturn + ) ); $this->assertEquals( @@ -142,17 +150,16 @@ public function testChangeModuleSettingInteger(): void $callValue = 123; $repositoryValue = 321; - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - $repository->expects($this->once()) - ->method('saveIntegerSetting') + $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); + $moduleSettingService->expects($this->once()) + ->method('saveInteger') ->with($name, $callValue, $moduleId); - $repository->expects($this->once()) - ->method('getIntegerSetting') + $moduleSettingService->method('getInteger') ->with($name, $moduleId) ->willReturn($repositoryValue); $sut = $this->getSut( - repository: $repository + moduleSettingService: $moduleSettingService ); $setting = $sut->changeIntegerSetting($name, $callValue, $moduleId); @@ -169,17 +176,16 @@ public function testChangeModuleSettingFloat(): void $callValue = 1.23; $repositoryValue = 3.21; - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - $repository->expects($this->once()) - ->method('saveFloatSetting') + $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); + $moduleSettingService->expects($this->once()) + ->method('saveFloat') ->with($name, $callValue, $moduleId); - $repository->expects($this->once()) - ->method('getFloatSetting') + $moduleSettingService->method('getFloat') ->with($name, $moduleId) ->willReturn($repositoryValue); $sut = $this->getSut( - repository: $repository + moduleSettingService: $moduleSettingService ); $setting = $sut->changeFloatSetting($name, $callValue, $moduleId); @@ -196,17 +202,16 @@ public function testChangeModuleSettingBoolean(): void $callValue = true; $repositoryValue = false; - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - $repository->expects($this->once()) - ->method('saveBooleanSetting') + $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); + $moduleSettingService->expects($this->once()) + ->method('saveBoolean') ->with($name, $callValue, $moduleId); - $repository->expects($this->once()) - ->method('getBooleanSetting') + $moduleSettingService->method('getBoolean') ->with($name, $moduleId) ->willReturn($repositoryValue); $sut = $this->getSut( - repository: $repository + moduleSettingService: $moduleSettingService ); $setting = $sut->changeBooleanSetting($name, $callValue, $moduleId); @@ -223,17 +228,16 @@ public function testChangeModuleSettingString(): void $callValue = 'someNewValue'; $repositoryValue = 'realDatabaseValue'; - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - $repository->expects($this->once()) - ->method('saveStringSetting') + $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); + $moduleSettingService->expects($this->once()) + ->method('saveString') ->with($name, $callValue, $moduleId); - $repository->expects($this->once()) - ->method('getStringSetting') + $moduleSettingService->method('getString') ->with($name, $moduleId) - ->willReturn($repositoryValue); + ->willReturn(new UnicodeString($repositoryValue)); $sut = $this->getSut( - repository: $repository + moduleSettingService: $moduleSettingService ); $setting = $sut->changeStringSetting($name, $callValue, $moduleId); @@ -251,13 +255,6 @@ public function testChangeModuleSettingCollection(): void $repositoryValue = ['realDatabaseValue']; $decodedValue = ['decodedCollectionValue']; - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - $repository->expects($this->once()) - ->method('saveCollectionSetting') - ->with($name, $decodedValue, $moduleId); - $repository->method('getCollectionSetting') - ->with($name, $moduleId) - ->willReturn($repositoryValue); $encoderResponse = 'encoderResponse'; $encoder = $this->createMock(JsonServiceInterface::class); @@ -268,9 +265,17 @@ public function testChangeModuleSettingCollection(): void ->with($callValue) ->willReturn($decodedValue); + $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); + $moduleSettingService->expects($this->once()) + ->method('saveCollection') + ->with($name, $decodedValue, $moduleId); + $moduleSettingService->method('getCollection') + ->with($name, $moduleId) + ->willReturn($repositoryValue); + $sut = $this->getSut( - repository: $repository, jsonService: $encoder, + moduleSettingService: $moduleSettingService ); $setting = $sut->changeCollectionSetting($name, $callValue, $moduleId); @@ -281,29 +286,58 @@ public function testChangeModuleSettingCollection(): void public function testListModuleSettings(): void { + $shopId = 3; $moduleId = 'awesomeModule'; - $intSetting = (new Setting())->setName('intSetting')->setType(FieldType::NUMBER); - $stringSetting = (new Setting())->setName('stringSetting')->setType(FieldType::STRING); - $arraySetting = (new Setting())->setName('arraySetting')->setType(FieldType::ARRAY); + $response = [ + (new Setting())->setName('intSetting')->setType(FieldType::NUMBER), + (new Setting())->setName('stringSetting')->setType(FieldType::STRING), + (new Setting())->setName('arraySetting')->setType(FieldType::ARRAY) + ]; + + $moduleConfigurationDao = $this->createMock(ModuleConfigurationDaoInterface::class); + $moduleConfigurationDao->expects($this->once()) + ->method('get') + ->with($moduleId, $shopId) + ->willReturn( + $this->createConfiguredMock(ModuleConfiguration::class, [ + 'getModuleSettings' => $response + ]) + ); - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - $repository->expects($this->once()) - ->method('getSettingsList') - ->with($moduleId) - ->willReturn([$intSetting, $stringSetting, $arraySetting]); + $sut = $this->getSut( + moduleConfigDao: $moduleConfigurationDao, + basicContext: $this->getBasicContextMock(3) + ); - $sut = $this->getSut($repository); $this->assertEquals($this->getSettingTypeList(), $sut->getSettingsList($moduleId)); } private function getSut( - ?ModuleSettingRepositoryInterface $repository = null, ?JsonServiceInterface $jsonService = null, + ?ModuleSettingServiceInterface $moduleSettingService = null, + ?ModuleConfigurationDaoInterface $moduleConfigDao = null, + ?BasicContextInterface $basicContext = null, ): ModuleSettingService { return new ModuleSettingService( - moduleSettingRepository: $repository ?? $this->createStub(ModuleSettingRepositoryInterface::class), jsonService: $jsonService ?? $this->createStub(JsonServiceInterface::class), + moduleSettingService: $moduleSettingService ?? $this->createStub(ModuleSettingServiceInterface::class), + moduleConfigurationDao: $moduleConfigDao ?? $this->createStub(ModuleConfigurationDaoInterface::class), + basicContext: $basicContext ?? $this->createStub(BasicContextInterface::class), ); } + + private function getShopModuleSettingServiceMethodStub( + string $methodName, + string $name, + string $moduleId, + mixed $shopServiceReturn + ): ModuleSettingServiceInterface { + $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); + $moduleSettingService->expects($this->once()) + ->method($methodName) + ->with($name, $moduleId) + ->willReturn($shopServiceReturn); + return $moduleSettingService; + } }