From 353e1752dea61bef3478594c39d5b264574493db Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Thu, 16 Nov 2023 14:00:00 +0200 Subject: [PATCH] OXDEV-7573 Get integer value via ShopConfigurationSettingDao Signed-off-by: Anton Fedurtsya --- .../Exception/WrongSettingTypeException.php | 14 ++++ .../Infrastructure/ShopSettingRepository.php | 18 ++-- .../ShopSettingRepositoryInterface.php | 2 +- src/Setting/Service/ShopSettingService.php | 4 +- .../Service/ShopSettingServiceInterface.php | 2 +- .../ShopSettingRepositoryTest.php | 83 ++++++++++++++----- tests/Unit/Service/ShopSettingServiceTest.php | 2 +- 7 files changed, 88 insertions(+), 37 deletions(-) create mode 100644 src/Setting/Exception/WrongSettingTypeException.php diff --git a/src/Setting/Exception/WrongSettingTypeException.php b/src/Setting/Exception/WrongSettingTypeException.php new file mode 100644 index 0000000..12306fe --- /dev/null +++ b/src/Setting/Exception/WrongSettingTypeException.php @@ -0,0 +1,14 @@ +getSettingValue($name, FieldType::NUMBER); - } catch (NotFound $e) { - $this->throwGetterNotFoundException('integer'); - } + $value = $this->configurationSettingDao->get( + $name, + $this->basicContext->getCurrentShopId() + ); - if ($this->isFloatString($value)) { - throw new UnexpectedValueException('The queried configuration was found as a float, not an integer'); + if ($value->getType() !== FieldType::NUMBER) { + throw new WrongSettingTypeException(); } - return (int)$value; + return (int)$value->getValue(); } public function getFloat(ID $name): float diff --git a/src/Setting/Infrastructure/ShopSettingRepositoryInterface.php b/src/Setting/Infrastructure/ShopSettingRepositoryInterface.php index 90e9686..c6ec0fe 100644 --- a/src/Setting/Infrastructure/ShopSettingRepositoryInterface.php +++ b/src/Setting/Infrastructure/ShopSettingRepositoryInterface.php @@ -7,7 +7,7 @@ interface ShopSettingRepositoryInterface { - public function getInteger(ID $name): int; + public function getInteger(string $name): int; public function getFloat(ID $name): float; diff --git a/src/Setting/Service/ShopSettingService.php b/src/Setting/Service/ShopSettingService.php index 1bcf3ab..1280651 100644 --- a/src/Setting/Service/ShopSettingService.php +++ b/src/Setting/Service/ShopSettingService.php @@ -25,10 +25,10 @@ public function __construct( ) { } - public function getIntegerSetting(ID $name): IntegerSetting + public function getIntegerSetting(string $name): IntegerSetting { $integer = $this->shopSettingRepository->getInteger($name); - return new IntegerSetting($name, $integer); + return new IntegerSetting(new ID($name), $integer); } public function getFloatSetting(ID $name): FloatSetting diff --git a/src/Setting/Service/ShopSettingServiceInterface.php b/src/Setting/Service/ShopSettingServiceInterface.php index f093e40..56e3cef 100644 --- a/src/Setting/Service/ShopSettingServiceInterface.php +++ b/src/Setting/Service/ShopSettingServiceInterface.php @@ -11,7 +11,7 @@ interface ShopSettingServiceInterface { - public function getIntegerSetting(ID $name): IntegerSetting; + public function getIntegerSetting(string $name): IntegerSetting; public function getFloatSetting(ID $name): FloatSetting; diff --git a/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php b/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php index e0a3409..892c82a 100644 --- a/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php +++ b/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php @@ -5,9 +5,13 @@ use Doctrine\DBAL\ForwardCompatibility\Result; use Doctrine\DBAL\Query\QueryBuilder; use OxidEsales\EshopCommunity\Internal\Framework\Config\Dao\ShopConfigurationSettingDaoInterface; +use OxidEsales\EshopCommunity\Internal\Framework\Config\DataObject\ShopConfigurationSetting; use OxidEsales\EshopCommunity\Internal\Framework\Config\Utility\ShopSettingEncoder; +use OxidEsales\EshopCommunity\Internal\Framework\Config\Utility\ShopSettingEncoderInterface; 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\Exception\WrongSettingTypeException; use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ShopSettingRepository; use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ShopSettingRepositoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase; @@ -20,36 +24,53 @@ class ShopSettingRepositoryTest extends UnitTestCase { public function testGetShopSettingInteger(): void { - $nameID = new ID('integerSetting'); - - - $repository = $this->getFetchOneShopSettingRepoInstance('123'); - - $integer = $repository->getInteger($nameID); + $settingName = 'settingName'; + $shopId = 3; + + $shopSettingValue = 123; + $shopSettingType = 'num'; + + $expectedValue = 123; + + $shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class); + $shopSettingDaoStub->method('get') + ->with($settingName, $shopId) + ->willReturn( + $this->createConfiguredMock(ShopConfigurationSetting::class, [ + 'getName' => $settingName, + 'getType' => $shopSettingType, + 'getValue' => $shopSettingValue + ]) + ); + + $basicContext = $this->createStub(BasicContextInterface::class); + $basicContext->method('getCurrentShopId')->willReturn($shopId); + + $sut = $this->getSut( + basicContext: $basicContext, + shopSettingDao: $shopSettingDaoStub + ); - $this->assertEquals(123, $integer); + $this->assertSame($expectedValue, $sut->getInteger($settingName)); } - public function testGetNoShopSettingInteger(): void + public function testGetShopSettingIntegerWrongType(): void { - $nameID = new ID('NotExistingSetting'); - - $repository = $this->getFetchOneShopSettingRepoInstance(false); - - $this->expectException(NotFound::class); - $this->expectExceptionMessage('The queried name couldn\'t be found as an integer configuration'); - $repository->getInteger($nameID); - } + $settingName = 'settingName'; - public function testGetShopSettingInvalidInteger(): void - { - $nameID = new ID('floatSetting'); + $shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class); + $shopSettingDaoStub->method('get')->willReturn( + $this->createConfiguredMock(ShopConfigurationSetting::class, [ + 'getType' => 'wrong' + ]) + ); - $repository = $this->getFetchOneShopSettingRepoInstance('1.23'); + $sut = $this->getSut( + shopSettingDao: $shopSettingDaoStub + ); - $this->expectException(UnexpectedValueException::class); - $this->expectExceptionMessage('The queried configuration was found as a float, not an integer'); - $repository->getInteger($nameID); + $this->expectException(WrongSettingTypeException::class); + $sut->getInteger($settingName); } public function testGetShopSettingFloat(): void @@ -272,4 +293,20 @@ private function getShopSettingRepository( $this->createStub(ShopConfigurationSettingDaoInterface::class) ); } + + private function getSut( + ?BasicContextInterface $basicContext = null, + ?EventDispatcherInterface $eventDispatcher = null, + ?QueryBuilderFactoryInterface $queryBuilderFactory = null, + ?ShopSettingEncoderInterface $shopSettingEncoder = null, + ?ShopConfigurationSettingDaoInterface $shopSettingDao = null, + ): ShopSettingRepositoryInterface { + return new ShopSettingRepository( + basicContext: $basicContext ?? $this->createStub(BasicContextInterface::class), + eventDispatcher: $eventDispatcher ?? $this->createStub(EventDispatcherInterface::class), + queryBuilderFactory: $queryBuilderFactory ?? $this->createStub(QueryBuilderFactoryInterface::class), + shopSettingEncoder: $shopSettingEncoder ?? $this->createStub(ShopSettingEncoderInterface::class), + configurationSettingDao: $shopSettingDao ?? $this->createStub(ShopConfigurationSettingDaoInterface::class), + ); + } } diff --git a/tests/Unit/Service/ShopSettingServiceTest.php b/tests/Unit/Service/ShopSettingServiceTest.php index 4c53b0e..be782f7 100644 --- a/tests/Unit/Service/ShopSettingServiceTest.php +++ b/tests/Unit/Service/ShopSettingServiceTest.php @@ -30,7 +30,7 @@ public function testGetShopSettingInteger(): void $this->assertEquals( new IntegerSetting($nameID, $repositoryResult), - $sut->getIntegerSetting($nameID) + $sut->getIntegerSetting((string)$nameID) ); }