diff --git a/src/Setting/Infrastructure/ModuleSettingRepository.php b/src/Setting/Infrastructure/ModuleSettingRepository.php index 20f3f67..c032d88 100644 --- a/src/Setting/Infrastructure/ModuleSettingRepository.php +++ b/src/Setting/Infrastructure/ModuleSettingRepository.php @@ -49,29 +49,29 @@ public function getCollectionSetting(string $name, string $moduleId): array return $this->moduleSettingService->getCollection($name, $moduleId); } - public function saveIntegerSetting(ID $name, int $value, string $moduleId): void + public function saveIntegerSetting(string $name, int $value, string $moduleId): void { - $this->moduleSettingService->saveInteger($name->val(), $value, $moduleId); + $this->moduleSettingService->saveInteger($name, $value, $moduleId); } - public function saveFloatSetting(ID $name, float $value, string $moduleId): void + public function saveFloatSetting(string $name, float $value, string $moduleId): void { - $this->moduleSettingService->saveFloat($name->val(), $value, $moduleId); + $this->moduleSettingService->saveFloat($name, $value, $moduleId); } - public function saveBooleanSetting(ID $name, bool $value, string $moduleId): void + public function saveBooleanSetting(string $name, bool $value, string $moduleId): void { - $this->moduleSettingService->saveBoolean($name->val(), $value, $moduleId); + $this->moduleSettingService->saveBoolean($name, $value, $moduleId); } - public function saveStringSetting(ID $name, string $value, string $moduleId): void + public function saveStringSetting(string $name, string $value, string $moduleId): void { - $this->moduleSettingService->saveString($name->val(), $value, $moduleId); + $this->moduleSettingService->saveString($name, $value, $moduleId); } - public function saveCollectionSetting(ID $name, array $value, string $moduleId): void + public function saveCollectionSetting(string $name, array $value, string $moduleId): void { - $this->moduleSettingService->saveCollection($name->val(), $value, $moduleId); + $this->moduleSettingService->saveCollection($name, $value, $moduleId); } /** diff --git a/src/Setting/Infrastructure/ModuleSettingRepositoryInterface.php b/src/Setting/Infrastructure/ModuleSettingRepositoryInterface.php index 2e00e19..63b72db 100644 --- a/src/Setting/Infrastructure/ModuleSettingRepositoryInterface.php +++ b/src/Setting/Infrastructure/ModuleSettingRepositoryInterface.php @@ -17,15 +17,15 @@ public function getStringSetting(string $name, string $moduleId): string; public function getCollectionSetting(string $name, string $moduleId): array; - public function saveIntegerSetting(ID $name, int $value, string $moduleId): void; + public function saveIntegerSetting(string $name, int $value, string $moduleId): void; - public function saveFloatSetting(ID $name, float $value, string $moduleId): void; + public function saveFloatSetting(string $name, float $value, string $moduleId): void; - public function saveBooleanSetting(ID $name, bool $value, string $moduleId): void; + public function saveBooleanSetting(string $name, bool $value, string $moduleId): void; - public function saveStringSetting(ID $name, string $value, string $moduleId): void; + public function saveStringSetting(string $name, string $value, string $moduleId): void; - public function saveCollectionSetting(ID $name, array $value, string $moduleId): void; + public function saveCollectionSetting(string $name, array $value, string $moduleId): void; /** * @return Setting[] diff --git a/src/Setting/Service/ModuleSettingService.php b/src/Setting/Service/ModuleSettingService.php index a507a81..4c07f18 100644 --- a/src/Setting/Service/ModuleSettingService.php +++ b/src/Setting/Service/ModuleSettingService.php @@ -71,30 +71,30 @@ public function getCollectionSetting(ID $name, string $moduleId): StringSetting public function changeIntegerSetting(ID $name, int $value, string $moduleId): IntegerSetting { - $this->moduleSettingRepository->saveIntegerSetting($name, $value, $moduleId); + $this->moduleSettingRepository->saveIntegerSetting((string)$name, $value, $moduleId); - return new IntegerSetting($name, $value); + return $this->getIntegerSetting($name, $moduleId); } public function changeFloatSetting(ID $name, float $value, string $moduleId): FloatSetting { - $this->moduleSettingRepository->saveFloatSetting($name, $value, $moduleId); + $this->moduleSettingRepository->saveFloatSetting((string)$name, $value, $moduleId); - return new FloatSetting($name, $value); + return $this->getFloatSetting($name, $moduleId); } public function changeBooleanSetting(ID $name, bool $value, string $moduleId): BooleanSetting { - $this->moduleSettingRepository->saveBooleanSetting($name, $value, $moduleId); + $this->moduleSettingRepository->saveBooleanSetting((string)$name, $value, $moduleId); - return new BooleanSetting($name, $value); + return $this->getBooleanSetting($name, $moduleId); } public function changeStringSetting(ID $name, string $value, string $moduleId): StringSetting { - $this->moduleSettingRepository->saveStringSetting($name, $value, $moduleId); + $this->moduleSettingRepository->saveStringSetting((string)$name, $value, $moduleId); - return new StringSetting($name, $value); + return $this->getStringSetting($name, $moduleId); } public function changeCollectionSetting(ID $name, string $value, string $moduleId): StringSetting @@ -105,9 +105,9 @@ public function changeCollectionSetting(ID $name, string $value, string $moduleI throw new InvalidCollection($value); } - $this->moduleSettingRepository->saveCollectionSetting($name, $arrayValue, $moduleId); + $this->moduleSettingRepository->saveCollectionSetting((string)$name, $arrayValue, $moduleId); - return new StringSetting($name, $value); + return $this->getCollectionSetting($name, $moduleId); } /** diff --git a/tests/Unit/Infrastructure/ModuleSettingRepositoryTest.php b/tests/Unit/Infrastructure/ModuleSettingRepositoryTest.php index 2040a67..ef9518f 100644 --- a/tests/Unit/Infrastructure/ModuleSettingRepositoryTest.php +++ b/tests/Unit/Infrastructure/ModuleSettingRepositoryTest.php @@ -23,7 +23,7 @@ public function testGetModuleSettingInteger(): void $shopServiceReturn = 123; $moduleRepository = $this->getSut( - moduleSettingService: $this->getShopModuleSettingServiceMock( + moduleSettingService: $this->getShopModuleSettingServiceMethodStub( 'getInteger', $name, $moduleId, @@ -44,7 +44,7 @@ public function testGetModuleSettingFloat(): void $moduleId = 'awesomeModule'; $moduleRepository = $this->getSut( - moduleSettingService: $this->getShopModuleSettingServiceMock( + moduleSettingService: $this->getShopModuleSettingServiceMethodStub( 'getFloat', $name, $moduleId, @@ -65,7 +65,7 @@ public function testGetModuleSettingBoolean(): void $moduleId = 'awesomeModule'; $moduleRepository = $this->getSut( - moduleSettingService: $this->getShopModuleSettingServiceMock( + moduleSettingService: $this->getShopModuleSettingServiceMethodStub( 'getBoolean', $name, $moduleId, @@ -86,7 +86,7 @@ public function testGetModuleSettingString(): void $repositoryReturn = 'default'; $moduleRepository = $this->getSut( - moduleSettingService: $this->getShopModuleSettingServiceMock( + moduleSettingService: $this->getShopModuleSettingServiceMethodStub( 'getString', $name, $moduleId, @@ -107,7 +107,7 @@ public function testGetModuleSettingCollection(): void $repositoryReturn = ['nice', 'values']; $moduleRepository = $this->getSut( - moduleSettingService: $this->getShopModuleSettingServiceMock( + moduleSettingService: $this->getShopModuleSettingServiceMethodStub( 'getCollection', $name, $moduleId, @@ -121,92 +121,94 @@ public function testGetModuleSettingCollection(): void ); } - public function testChangeModuleSettingInteger(): void + public function testSaveIntegerSettingProxyingDataToShopService(): void { - $name = new ID('intSetting'); + $name = 'intSetting'; + $moduleId = 'awesomeModule'; + $callValue = 123; $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); $moduleSettingService->expects($this->once()) ->method('saveInteger') - ->with($name->val(), 123, 'awesomeModule'); + ->with($name, $callValue, $moduleId); $moduleRepository = $this->getSut( - $moduleSettingService, - $this->createMock(ModuleConfigurationDaoInterface::class) + moduleSettingService: $moduleSettingService ); - $moduleRepository->saveIntegerSetting($name, 123, 'awesomeModule'); + $moduleRepository->saveIntegerSetting($name, $callValue, $moduleId); } - public function testChangeModuleSettingFloat(): void + public function testSaveFloatSettingProxyingDataToShopService(): void { - $name = new ID('floatSetting'); + $name = 'floatSetting'; + $moduleId = 'awesomeModule'; + $callValue = 1.23; $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); $moduleSettingService->expects($this->once()) ->method('saveFloat') - ->with($name->val(), 1.23, 'awesomeModule'); + ->with($name, $callValue, $moduleId); $moduleRepository = $this->getSut( - $moduleSettingService, - $this->createMock(ModuleConfigurationDaoInterface::class) + moduleSettingService: $moduleSettingService ); - $moduleRepository->saveFloatSetting($name, 1.23, 'awesomeModule'); + $moduleRepository->saveFloatSetting($name, $callValue, $moduleId); } - public function testChangeModuleSettingBoolean(): void + public function testSaveBooleanSettingProxyingDataToShopService(): void { - $name = new ID('boolSetting'); - $value = false; + $name = 'boolSetting'; + $moduleId = 'awesomeModule'; + $callValue = false; $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); $moduleSettingService->expects($this->once()) ->method('saveBoolean') - ->with($name->val(), $value, 'awesomeModule'); + ->with($name, $callValue, $moduleId); $moduleRepository = $this->getSut( - $moduleSettingService, - $this->createMock(ModuleConfigurationDaoInterface::class) + moduleSettingService: $moduleSettingService ); - $moduleRepository->saveBooleanSetting($name, $value, 'awesomeModule'); + $moduleRepository->saveBooleanSetting($name, $callValue, $moduleId); } - public function testChangeModuleSettingString(): void + public function testSaveStringSettingProxyingDataToShopService(): void { - $name = new ID('stringSetting'); - $value = 'default'; + $name = 'stringSetting'; + $moduleId = 'awesomeModule'; + $callValue = 'default'; $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); $moduleSettingService->expects($this->once()) ->method('saveString') - ->with($name->val(), $value, 'awesomeModule'); + ->with($name, $callValue, $moduleId); $moduleRepository = $this->getSut( - $moduleSettingService, - $this->createMock(ModuleConfigurationDaoInterface::class) + moduleSettingService: $moduleSettingService ); - $moduleRepository->saveStringSetting($name, $value, 'awesomeModule'); + $moduleRepository->saveStringSetting($name, $callValue, $moduleId); } - public function testChangeModuleSettingCollection(): void + public function testSaveCollectionSettingProxyingDataToShopService(): void { - $name = new ID('boolSetting'); - $value = [3, 'interesting', 'values']; + $name = 'collectionSetting'; + $moduleId = 'awesomeModule'; + $callValue = [3, 'interesting', 'values']; $moduleSettingService = $this->createMock(ModuleSettingServiceInterface::class); $moduleSettingService->expects($this->once()) ->method('saveCollection') - ->with($name->val(), $value, 'awesomeModule'); + ->with($name, $callValue, $moduleId); $moduleRepository = $this->getSut( - $moduleSettingService, - $this->createMock(ModuleConfigurationDaoInterface::class) + moduleSettingService: $moduleSettingService ); - $moduleRepository->saveCollectionSetting($name, $value, 'awesomeModule'); + $moduleRepository->saveCollectionSetting($name, $callValue, $moduleId); } public function testGetSettingsList(): void @@ -246,7 +248,7 @@ public function getSut( ); } - private function getShopModuleSettingServiceMock( + private function getShopModuleSettingServiceMethodStub( string $methodName, string $name, string $moduleId, diff --git a/tests/Unit/Service/ModuleSettingServiceTest.php b/tests/Unit/Service/ModuleSettingServiceTest.php index 5c6136f..63d25f4 100644 --- a/tests/Unit/Service/ModuleSettingServiceTest.php +++ b/tests/Unit/Service/ModuleSettingServiceTest.php @@ -133,56 +133,114 @@ public function testGetModuleSettingCollection(): void public function testChangeModuleSettingInteger(): void { + $name = 'intSetting'; + $moduleId = 'awesomeModule'; + + $callValue = 123; + $repositoryValue = 321; + $repository = $this->createMock(ModuleSettingRepositoryInterface::class); + $repository->expects($this->once()) + ->method('saveIntegerSetting') + ->with($name, $callValue, $moduleId); + $repository->expects($this->once()) + ->method('getIntegerSetting') + ->with($name, $moduleId) + ->willReturn($repositoryValue); - $settingService = $this->getSut($repository); + $sut = $this->getSut( + repository: $repository + ); - $nameID = new ID('intSetting'); - $integerSetting = $settingService->changeIntegerSetting($nameID, 123, 'awesomeModule'); + $nameID = new ID($name); + $setting = $sut->changeIntegerSetting($nameID, $callValue, $moduleId); - $this->assertSame($nameID, $integerSetting->getName()); - $this->assertSame(123, $integerSetting->getValue()); + $this->assertSame($nameID, $setting->getName()); + $this->assertSame($repositoryValue, $setting->getValue()); } public function testChangeModuleSettingFloat(): void { + $name = 'floatSetting'; + $moduleId = 'awesomeModule'; + + $callValue = 1.23; + $repositoryValue = 3.21; + $repository = $this->createMock(ModuleSettingRepositoryInterface::class); + $repository->expects($this->once()) + ->method('saveFloatSetting') + ->with($name, $callValue, $moduleId); + $repository->expects($this->once()) + ->method('getFloatSetting') + ->with($name, $moduleId) + ->willReturn($repositoryValue); - $settingService = $this->getSut($repository); + $sut = $this->getSut( + repository: $repository + ); - $nameID = new ID('floatSetting'); - $floatSetting = $settingService->changeFloatSetting($nameID, 1.23, 'awesomeModule'); + $nameID = new ID($name); + $setting = $sut->changeFloatSetting($nameID, $callValue, $moduleId); - $this->assertSame($nameID, $floatSetting->getName()); - $this->assertSame(1.23, $floatSetting->getValue()); + $this->assertSame($nameID, $setting->getName()); + $this->assertSame($repositoryValue, $setting->getValue()); } public function testChangeModuleSettingBoolean(): void { + $name = 'boolSetting'; + $moduleId = 'awesomeModule'; + + $callValue = true; + $repositoryValue = false; + $repository = $this->createMock(ModuleSettingRepositoryInterface::class); + $repository->expects($this->once()) + ->method('saveBooleanSetting') + ->with($name, $callValue, $moduleId); + $repository->expects($this->once()) + ->method('getBooleanSetting') + ->with($name, $moduleId) + ->willReturn($repositoryValue); - $settingService = $this->getSut($repository); + $sut = $this->getSut( + repository: $repository + ); - $nameID = new ID('boolSetting'); - $value = false; - $booleanSetting = $settingService->changeBooleanSetting($nameID, $value, 'awesomeModule'); + $nameID = new ID($name); + $setting = $sut->changeBooleanSetting($nameID, $callValue, $moduleId); - $this->assertSame($nameID, $booleanSetting->getName()); - $this->assertSame($value, $booleanSetting->getValue()); + $this->assertSame($nameID, $setting->getName()); + $this->assertSame($repositoryValue, $setting->getValue()); } public function testChangeModuleSettingString(): void { + $name = 'stringSetting'; + $moduleId = 'awesomeModule'; + + $callValue = 'someNewValue'; + $repositoryValue = 'realDatabaseValue'; + $repository = $this->createMock(ModuleSettingRepositoryInterface::class); + $repository->expects($this->once()) + ->method('saveStringSetting') + ->with($name, $callValue, $moduleId); + $repository->expects($this->once()) + ->method('getStringSetting') + ->with($name, $moduleId) + ->willReturn($repositoryValue); - $settingService = $this->getSut($repository); + $sut = $this->getSut( + repository: $repository + ); - $nameID = new ID('stringSetting'); - $value = 'default'; - $stringSetting = $settingService->changeStringSetting($nameID, $value, 'awesomeModule'); + $nameID = new ID($name); + $setting = $sut->changeStringSetting($nameID, $callValue, $moduleId); - $this->assertSame($nameID, $stringSetting->getName()); - $this->assertSame($value, $stringSetting->getValue()); + $this->assertSame($nameID, $setting->getName()); + $this->assertSame($repositoryValue, $setting->getValue()); } /** @@ -216,16 +274,36 @@ public function invalidCollectionDataProvider(): array public function testChangeModuleSettingCollection(): void { + $name = 'collectionSetting'; + $moduleId = 'awesomeModule'; + + $callValue = '[2, "values"]'; + $repositoryValue = ['realDatabaseValue']; + $repository = $this->createMock(ModuleSettingRepositoryInterface::class); + $repository->expects($this->once()) + ->method('saveCollectionSetting') + ->with($name, json_decode($callValue), $moduleId); + $repository->method('getCollectionSetting') + ->with($name, $moduleId) + ->willReturn($repositoryValue); - $settingService = $this->getSut($repository); + $encoderResponse = 'encoderResponse'; + $encoder = $this->createMock(JsonServiceInterface::class); + $encoder->method('jsonEncodeArray') + ->with($repositoryValue) + ->willReturn($encoderResponse); - $nameID = new ID('collectionSetting'); - $value = '[2, "values"]'; - $collectionSetting = $settingService->changeCollectionSetting($nameID, $value, 'awesomeModule'); + $sut = $this->getSut( + repository: $repository, + jsonService: $encoder, + ); + + $nameID = new ID($name); + $setting = $sut->changeCollectionSetting($nameID, $callValue, $moduleId); - $this->assertSame($nameID, $collectionSetting->getName()); - $this->assertSame($value, $collectionSetting->getValue()); + $this->assertSame($nameID, $setting->getName()); + $this->assertSame($encoderResponse, $setting->getValue()); } public function testListModuleSettings(): void