Skip to content

Commit

Permalink
OXDEV-7573 Get integer value via ShopConfigurationSettingDao
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 16, 2023
1 parent 8cf49da commit 353e175
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 37 deletions.
14 changes: 14 additions & 0 deletions src/Setting/Exception/WrongSettingTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception;

final class WrongSettingTypeException extends \Exception
{
}
18 changes: 9 additions & 9 deletions src/Setting/Infrastructure/ShopSettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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 Psr\EventDispatcher\EventDispatcherInterface;
use TheCodingMachine\GraphQLite\Types\ID;
use UnexpectedValueException;
Expand All @@ -32,19 +33,18 @@ public function __construct(
) {
}

public function getInteger(ID $name): int
public function getInteger(string $name): int
{
try {
$value = $this->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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

interface ShopSettingRepositoryInterface
{
public function getInteger(ID $name): int;
public function getInteger(string $name): int;

public function getFloat(ID $name): float;

Expand Down
4 changes: 2 additions & 2 deletions src/Setting/Service/ShopSettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Setting/Service/ShopSettingServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

interface ShopSettingServiceInterface
{
public function getIntegerSetting(ID $name): IntegerSetting;
public function getIntegerSetting(string $name): IntegerSetting;

public function getFloatSetting(ID $name): FloatSetting;

Expand Down
83 changes: 60 additions & 23 deletions tests/Unit/Infrastructure/ShopSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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),
);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Service/ShopSettingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testGetShopSettingInteger(): void

$this->assertEquals(
new IntegerSetting($nameID, $repositoryResult),
$sut->getIntegerSetting($nameID)
$sut->getIntegerSetting((string)$nameID)
);
}

Expand Down

0 comments on commit 353e175

Please sign in to comment.