diff --git a/src/Setting/Infrastructure/ShopSettingRepository.php b/src/Setting/Infrastructure/ShopSettingRepository.php index 4c9fff5..d651959 100644 --- a/src/Setting/Infrastructure/ShopSettingRepository.php +++ b/src/Setting/Infrastructure/ShopSettingRepository.php @@ -77,20 +77,17 @@ private function matchesFloatDigits(string $value): bool return (bool)preg_match("/^\d+(\.\d+)?$/", $value); } - protected function getShopSetting(string $name): ShopConfigurationSetting + public function getBoolean(string $name): bool { - return $this->configurationSettingDao->get($name, $this->basicContext->getCurrentShopId()); + $setting = $this->getShopSetting($name); + $this->checkSettingType($setting, FieldType::BOOLEAN); + + return (bool)$setting->getValue(); } - public function getBoolean(ID $name): bool + protected function getShopSetting(string $name): ShopConfigurationSetting { - try { - $value = $this->getSettingValue($name, FieldType::BOOLEAN); - } catch (NotFound $e) { - $this->throwGetterNotFoundException('boolean'); - } - - return (bool)$value; + return $this->configurationSettingDao->get($name, $this->basicContext->getCurrentShopId()); } public function getString(ID $name): string diff --git a/src/Setting/Infrastructure/ShopSettingRepositoryInterface.php b/src/Setting/Infrastructure/ShopSettingRepositoryInterface.php index b876aca..9aabe2c 100644 --- a/src/Setting/Infrastructure/ShopSettingRepositoryInterface.php +++ b/src/Setting/Infrastructure/ShopSettingRepositoryInterface.php @@ -11,7 +11,7 @@ public function getInteger(string $name): int; public function getFloat(string $name): float; - public function getBoolean(ID $name): bool; + public function getBoolean(string $name): bool; public function getString(ID $name): string; diff --git a/src/Setting/Service/ShopSettingService.php b/src/Setting/Service/ShopSettingService.php index b83e080..fc636c6 100644 --- a/src/Setting/Service/ShopSettingService.php +++ b/src/Setting/Service/ShopSettingService.php @@ -37,10 +37,10 @@ public function getFloatSetting(string $name): FloatSetting return new FloatSetting(new ID($name), $float); } - public function getBooleanSetting(ID $name): BooleanSetting + public function getBooleanSetting(string $name): BooleanSetting { $bool = $this->shopSettingRepository->getBoolean($name); - return new BooleanSetting($name, $bool); + return new BooleanSetting(new ID($name), $bool); } public function getStringSetting(ID $name): StringSetting diff --git a/src/Setting/Service/ShopSettingServiceInterface.php b/src/Setting/Service/ShopSettingServiceInterface.php index d8d1ed1..b899ac8 100644 --- a/src/Setting/Service/ShopSettingServiceInterface.php +++ b/src/Setting/Service/ShopSettingServiceInterface.php @@ -15,7 +15,7 @@ public function getIntegerSetting(string $name): IntegerSetting; public function getFloatSetting(string $name): FloatSetting; - public function getBooleanSetting(ID $name): BooleanSetting; + public function getBooleanSetting(string $name): BooleanSetting; public function getStringSetting(ID $name): StringSetting; diff --git a/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php b/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php index 38cc382..d99d197 100644 --- a/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php +++ b/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php @@ -11,6 +11,7 @@ use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface; use OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface; use OxidEsales\GraphQL\Base\Exception\NotFound; +use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType; use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\WrongSettingTypeException; use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\WrongSettingValueException; use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ShopSettingRepository; @@ -28,15 +29,13 @@ public function testGetShopSettingInteger($possibleValue, $expectedResult): void $settingName = 'settingName'; $shopId = 3; - $shopSettingType = 'num'; - $shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class); $shopSettingDaoStub->method('get') ->with($settingName, $shopId) ->willReturn( $this->createConfiguredMock(ShopConfigurationSetting::class, [ 'getName' => $settingName, - 'getType' => $shopSettingType, + 'getType' => FieldType::NUMBER, 'getValue' => $possibleValue ]) ); @@ -70,7 +69,7 @@ public function testGetShopSettingFloat($possibleValue, $expectedResult): void ->willReturn( $this->createConfiguredMock(ShopConfigurationSetting::class, [ 'getName' => $settingName, - 'getType' => 'num', + 'getType' => FieldType::NUMBER, 'getValue' => $possibleValue ]) ); @@ -166,41 +165,53 @@ public function wrongSettingsDataProvider(): \Generator 'value' => 'any', 'expectedException' => WrongSettingValueException::class ]; - } - - public function testGetShopSettingBooleanNegativ(): void - { - $nameID = new ID('booleanSetting'); - - $repository = $this->getFetchOneShopSettingRepoInstance(''); - - $boolean = $repository->getBoolean($nameID); - - $this->assertEquals(false, $boolean); + yield [ + 'method' => 'getBoolean', + 'type' => 'wrong', + 'value' => 'any', + 'expectedException' => WrongSettingTypeException::class + ]; } - public function testGetShopSettingBooleanPositiv(): void + /** @dataProvider possibleBoolValuesDataProvider */ + public function testGetShopSettingBoolean($possibleValue, $expectedResult): void { - $nameID = new ID('booleanSetting'); + $settingName = 'settingName'; + $shopId = 3; + $shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class); + $shopSettingDaoStub->method('get') + ->with($settingName, $shopId) + ->willReturn( + $this->createConfiguredMock(ShopConfigurationSetting::class, [ + 'getName' => $settingName, + 'getType' => FieldType::BOOLEAN, + 'getValue' => $possibleValue + ]) + ); - $repository = $this->getFetchOneShopSettingRepoInstance('1'); + $basicContext = $this->createStub(BasicContextInterface::class); + $basicContext->method('getCurrentShopId')->willReturn($shopId); - $boolean = $repository->getBoolean($nameID); + $sut = $this->getSut( + basicContext: $basicContext, + shopSettingDao: $shopSettingDaoStub + ); - $this->assertEquals(true, $boolean); + $this->assertSame($expectedResult, $sut->getBoolean($settingName)); } - public function testGetNoShopSettingBoolean(): void + public function possibleBoolValuesDataProvider(): \Generator { - $nameID = new ID('NotExistingSetting'); - - $repository = $this->getFetchOneShopSettingRepoInstance(false); - - $this->expectException(NotFound::class); - $this->expectExceptionMessage('The queried name couldn\'t be found as a boolean configuration'); - $repository->getBoolean($nameID); + yield ['possibleValue' => 1, 'expectedResult' => true]; + yield ['possibleValue' => '1', 'expectedResult' => true]; + yield ['possibleValue' => true, 'expectedResult' => true]; + yield ['possibleValue' => 'anything', 'expectedResult' => true]; + yield ['possibleValue' => null, 'expectedResult' => false]; + yield ['possibleValue' => 0, 'expectedResult' => false]; + yield ['possibleValue' => '0', 'expectedResult' => false]; + yield ['possibleValue' => false, 'expectedResult' => false]; } public function testGetShopSettingString(): void diff --git a/tests/Unit/Service/ShopSettingServiceTest.php b/tests/Unit/Service/ShopSettingServiceTest.php index c64ed7c..8995023 100644 --- a/tests/Unit/Service/ShopSettingServiceTest.php +++ b/tests/Unit/Service/ShopSettingServiceTest.php @@ -68,7 +68,7 @@ public function testGetShopSettingBoolean(): void $this->assertEquals( new BooleanSetting($nameID, $repositoryResult), - $settingService->getBooleanSetting($nameID) + $settingService->getBooleanSetting((string)$nameID) ); }