Skip to content

Commit

Permalink
OXDEV-7573 Add possible float and integer cases
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 6d6b159 commit adf8745
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 34 deletions.
14 changes: 14 additions & 0 deletions src/Setting/Exception/WrongSettingValueException.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 WrongSettingValueException extends \Exception
{
}
33 changes: 30 additions & 3 deletions src/Setting/Infrastructure/ShopSettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
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 Psr\EventDispatcher\EventDispatcherInterface;
use TheCodingMachine\GraphQLite\Types\ID;
use UnexpectedValueException;

final class ShopSettingRepository implements ShopSettingRepositoryInterface
{
Expand All @@ -39,15 +39,42 @@ public function getInteger(string $name): int
$setting = $this->getShopSetting($name);
$this->checkSettingType($setting, FieldType::NUMBER);

return (int)$setting->getValue();
$value = $setting->getValue();
if (
!is_int($value)
&& !(is_string($value) && $this->matchesOnlyDigits($value))
) {
throw new WrongSettingValueException();
}

return (int)$value;
}

private function matchesOnlyDigits(string $value): bool
{
return (bool)preg_match("/^\d+$/", $value);
}

public function getFloat(string $name): float
{
$setting = $this->getShopSetting($name);
$this->checkSettingType($setting, FieldType::NUMBER);

return (float)$setting->getValue();
$value = $setting->getValue();
if (
!is_int($value)
&& !is_float($value)
&& !(is_string($value) && $this->matchesFloatDigits($value))
) {
throw new WrongSettingValueException();
}

return (float)$value;
}

private function matchesFloatDigits(string $value): bool
{
return (bool)preg_match("/^\d+(\.\d+)?$/", $value);
}

protected function getShopSetting(string $name): ShopConfigurationSetting
Expand Down
91 changes: 60 additions & 31 deletions tests/Unit/Infrastructure/ShopSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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\Exception\WrongSettingValueException;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ShopSettingRepository;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ShopSettingRepositoryInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase;
Expand All @@ -21,24 +22,22 @@

class ShopSettingRepositoryTest extends UnitTestCase
{
public function testGetShopSettingInteger(): void
/** @dataProvider possibleIntegerValuesDataProvider */
public function testGetShopSettingInteger($possibleValue, $expectedResult): void
{
$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
'getValue' => $possibleValue
])
);

Expand All @@ -50,27 +49,29 @@ public function testGetShopSettingInteger(): void
shopSettingDao: $shopSettingDaoStub
);

$this->assertSame($expectedValue, $sut->getInteger($settingName));
$this->assertSame($expectedResult, $sut->getInteger($settingName));
}

public function testGetShopSettingFloat(): void
public function possibleIntegerValuesDataProvider(): \Generator
{
yield ['possibleValue' => 123, 'expectedResult' => 123];
yield ['possibleValue' => '123', 'expectedResult' => 123];
}

/** @dataProvider possibleFloatValuesDataProvider */
public function testGetShopSettingFloat($possibleValue, $expectedResult): void
{
$settingName = 'settingName';
$shopId = 3;

$shopSettingValue = 1.23;
$shopSettingType = 'num';

$expectedValue = 1.23;

$shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class);
$shopSettingDaoStub->method('get')
->with($settingName, $shopId)
->willReturn(
$this->createConfiguredMock(ShopConfigurationSetting::class, [
'getName' => $settingName,
'getType' => $shopSettingType,
'getValue' => $shopSettingValue
'getType' => 'num',
'getValue' => $possibleValue
])
);

Expand All @@ -82,18 +83,23 @@ public function testGetShopSettingFloat(): void
shopSettingDao: $shopSettingDaoStub
);

$this->assertSame($expectedValue, $sut->getFloat($settingName));
$this->assertSame($expectedResult, $sut->getFloat($settingName));
}

public function possibleFloatValuesDataProvider(): \Generator
{
yield ['possibleValue' => 123.2, 'expectedResult' => 123.2];
yield ['possibleValue' => 123, 'expectedResult' => 123.0];
yield ['possibleValue' => '123', 'expectedResult' => 123.0];
}

/** @dataProvider wrongSettingsDataProvider */
public function testGetShopSettingIntegerWrongData(
public function testGetShopSettingWrongData(
string $method,
string $type,
$value,
string $expectedException
): void {
$settingName = 'settingName';

$shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class);
$shopSettingDaoStub->method('get')->willReturn(
$this->createConfiguredMock(ShopConfigurationSetting::class, [
Expand All @@ -107,7 +113,7 @@ public function testGetShopSettingIntegerWrongData(
);

$this->expectException($expectedException);
$sut->$method($settingName);
$sut->$method('settingName');
}

public function wrongSettingsDataProvider(): \Generator
Expand All @@ -119,25 +125,48 @@ public function wrongSettingsDataProvider(): \Generator
'expectedException' => WrongSettingTypeException::class
];

yield [
'method' => 'getInteger',
'type' => 'num',
'value' => 'any',
'expectedException' => WrongSettingValueException::class
];

yield [
'method' => 'getInteger',
'type' => 'num',
'value' => null,
'expectedException' => WrongSettingValueException::class
];

yield [
'method' => 'getInteger',
'type' => 'num',
'value' => 1.123,
'expectedException' => WrongSettingValueException::class
];

yield [
'method' => 'getInteger',
'type' => 'num',
'value' => '1.123',
'expectedException' => WrongSettingValueException::class
];

yield [
'method' => 'getFloat',
'type' => 'wrong',
'value' => 'any',
'expectedException' => WrongSettingTypeException::class
];
}

//
// public function testGetShopSettingInvalidFloat(): void
// {
// $nameID = new ID('intSetting');
//
// $repository = $this->getFetchOneShopSettingRepoInstance('123');
//
// $this->expectException(UnexpectedValueException::class);
// $this->expectExceptionMessage('The queried configuration was found as an integer, not a float');
// $repository->getFloat($nameID);
// }
yield [
'method' => 'getFloat',
'type' => 'num',
'value' => 'any',
'expectedException' => WrongSettingValueException::class
];
}

public function testGetShopSettingBooleanNegativ(): void
{
Expand Down

0 comments on commit adf8745

Please sign in to comment.