Skip to content

Commit

Permalink
OXDEV-7456: Add mutation to change theme float setting
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelOxid committed Nov 17, 2023
1 parent 0f820f9 commit 0a58c80
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 64 deletions.
8 changes: 7 additions & 1 deletion src/Setting/Infrastructure/ThemeSettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ public function saveIntegerSetting(ID $name, int $value, string $themeId): void

public function saveFloatSetting(ID $name, float $value, string $themeId): void
{
// TODO: Implement saveFloatSetting() method.
$value = $this->shopSettingEncoder->encode(FieldType::NUMBER, $value);

try {
$this->saveSettingValue($name, $themeId, (string)$value);
} catch (NotFound $e) {
$this->throwSetterNotFoundException('float', $name->val());
}
}

public function saveBooleanSetting(ID $name, bool $value, string $themeId): void
Expand Down
137 changes: 103 additions & 34 deletions tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,100 @@ class ThemeSettingRepositoryTest extends IntegrationTestCase
{
public function testSaveAndGetIntegerSetting(): void
{
$configurationChangedEvent = new ThemeSettingChangedEvent(
"coolIntSetting",
1,
'awesomeTheme'
$name = 'coolIntSetting';
$eventDispatcher = $this->getEventDispatcherMock($name);
/** @var QueryBuilderFactoryInterface $queryBuilderFactory */
$queryBuilderFactory = $this->get(QueryBuilderFactoryInterface::class);

$repository = $this->getSut(
eventDispatcher: $eventDispatcher,
queryBuilderFactory: $queryBuilderFactory
);

$this->createThemeSetting(
$queryBuilderFactory,
$name,
FieldType::NUMBER,
123
);

$repository->saveIntegerSetting(new ID($name), 124, 'awesomeTheme');
$integerResult = $repository->getInteger(new ID($name), 'awesomeTheme');

$this->assertSame(124, $integerResult);
}

public function testSaveNotExistingIntegerSetting(): void
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects($this->once())
->method('dispatch')
->with($configurationChangedEvent);
$basicContext = $this->get(BasicContextInterface::class);
$eventDispatcher->expects($this->never())
->method('dispatch');
$repository = $this->getSut(eventDispatcher: $eventDispatcher);

$this->expectException(NotFound::class);
$this->expectExceptionMessage('The integer setting "notExistingSetting" doesn\'t exist');
$repository->saveIntegerSetting(new ID('notExistingSetting'), 1234, 'awesomeTheme');
}

public function testSaveAndGetFloatSetting(): void
{
$name = "coolFloatSetting";
$eventDispatcher = $this->getEventDispatcherMock("coolFloatSetting");
/** @var QueryBuilderFactoryInterface $queryBuilderFactory */
$queryBuilderFactory = $this->get(QueryBuilderFactoryInterface::class);
$shopSettingEncoder = $this->get(ShopSettingEncoderInterface::class);

$repository = new ThemeSettingRepository(
$basicContext,
$eventDispatcher,
$repository = $this->getSut(
eventDispatcher: $eventDispatcher,
queryBuilderFactory: $queryBuilderFactory
);

$this->createThemeSetting(
$queryBuilderFactory,
$shopSettingEncoder
$name,
FieldType::NUMBER,
1.23
);

$repository->saveFloatSetting(new ID($name), 1.24, 'awesomeTheme');
$floatResult = $repository->getFloat(new ID($name), 'awesomeTheme');

$this->assertSame(1.24, $floatResult);
}

public function testSaveNotExistingFloatSetting(): void
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects($this->never())
->method('dispatch');
$repository = $this->getSut(eventDispatcher: $eventDispatcher);

$this->expectException(NotFound::class);
$this->expectExceptionMessage('The integer setting "notExistingSetting" doesn\'t exist');
$repository->saveIntegerSetting(new ID('notExistingSetting'), 1234, 'awesomeTheme');
}

private function getSut(
?BasicContextInterface $basicContext = null,
?EventDispatcherInterface $eventDispatcher = null,
?QueryBuilderFactoryInterface $queryBuilderFactory = null,
?ShopSettingEncoderInterface $shopSettingEncoder = null
): ThemeSettingRepository
{
return new ThemeSettingRepository(
$basicContext ?? $this->get(BasicContextInterface::class),
$eventDispatcher ?? $this->get(EventDispatcherInterface::class),
$queryBuilderFactory ?? $this->get(QueryBuilderFactoryInterface::class),
$shopSettingEncoder ?? $this->get(ShopSettingEncoderInterface::class)
);
}

private function createThemeSetting(
QueryBuilderFactoryInterface $queryBuilderFactory,
string $name,
string $fieldType,
mixed $value
): void
{
$uniqueId = uniqid();
$queryBuilder = $queryBuilderFactory->create();
$queryBuilder
Expand All @@ -59,32 +132,28 @@ public function testSaveAndGetIntegerSetting(): void
'oxid' => $uniqueId,
'oxshopid' => 1,
'oxmodule' => 'theme:awesomeTheme',
'oxvarname' => 'coolIntSetting',
'oxvartype' => FieldType::NUMBER,
'oxvarvalue' => 123
'oxvarname' => $name,
'oxvartype' => $fieldType,
'oxvarvalue' => $value
]);
$queryBuilder->execute();

$repository->saveIntegerSetting(new ID('coolIntSetting'), 124, 'awesomeTheme');
$integerResult = $repository->getInteger(new ID('coolIntSetting'), 'awesomeTheme');

$this->assertSame(124, $integerResult);
}

public function testSaveNotExistingSetting(): void
/**
* @param string $name
* @return \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface|(EventDispatcherInterface&\PHPUnit\Framework\MockObject\MockObject)
*/
public function getEventDispatcherMock(string $name): \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects($this->never())
->method('dispatch');
$repository = new ThemeSettingRepository(
$this->get(BasicContextInterface::class),
$eventDispatcher,
$this->get(QueryBuilderFactoryInterface::class),
$this->get(ShopSettingEncoderInterface::class)
$configurationChangedEvent = new ThemeSettingChangedEvent(
$name,
1,
'awesomeTheme'
);

$this->expectException(NotFound::class);
$this->expectExceptionMessage('The integer setting "notExistingSetting" doesn\'t exist');
$repository->saveIntegerSetting(new ID('notExistingSetting'), 1234, 'awesomeTheme');
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects($this->once())
->method('dispatch')
->with($configurationChangedEvent);
return $eventDispatcher;
}
}
113 changes: 84 additions & 29 deletions tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ThemeSettingRepository;
use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use TheCodingMachine\GraphQLite\Types\ID;

Expand All @@ -17,21 +18,13 @@ class ChangeThemeSettingRepositoryTest extends UnitTestCase
public function testChangeThemeSettingInteger(): void
{
$nameID = new ID('integerSetting');
$settingEncoder = $this->createMock(ShopSettingEncoderInterface::class);
$settingEncoder->expects($this->once())
->method('encode')
->with(FieldType::NUMBER, 123)
->willReturn(123);

$repository = $this->getMockBuilder(ThemeSettingRepository::class)
->setConstructorArgs([
$this->createMock(BasicContextInterface::class),
$this->createMock(EventDispatcherInterface::class),
$this->createMock(QueryBuilderFactoryInterface::class),
$settingEncoder
])
->onlyMethods(['saveSettingValue'])
->getMock();
$settingEncoder = $this->getShopSettingEncoderMock(
FieldType::NUMBER,
123,
123
);
$repository = $this->getSut(settingEncoder: $settingEncoder);
$repository->expects($this->once())
->method('saveSettingValue')
->with($nameID, 'awesomeTheme', '123');
Expand All @@ -42,28 +35,90 @@ public function testChangeThemeSettingInteger(): void
public function testChangeNoThemeSettingInteger(): void
{
$nameID = new ID('NotExistingSetting');
$settingEncoder = $this->createMock(ShopSettingEncoderInterface::class);
$settingEncoder->expects($this->once())
->method('encode')
->with(FieldType::NUMBER, 123)
->willReturn(123);
$repository = $this->getMockBuilder(ThemeSettingRepository::class)
->setConstructorArgs([
$this->createMock(BasicContextInterface::class),
$this->createMock(EventDispatcherInterface::class),
$this->createMock(QueryBuilderFactoryInterface::class),
$settingEncoder
])
->onlyMethods(['saveSettingValue'])
->getMock();

$settingEncoder = $this->getShopSettingEncoderMock(
FieldType::NUMBER,
123,
123
);
$repository = $this->getSut(settingEncoder: $settingEncoder);
$repository->expects($this->once())
->method('saveSettingValue')
->with($nameID, 'awesomeTheme', '123')
->willThrowException(new NotFound());

$this->expectException(NotFound::class);
$this->expectExceptionMessage('The integer setting "' . $nameID->val() . '" doesn\'t exist');

$repository->saveIntegerSetting($nameID, 123, 'awesomeTheme');
}

public function testChangeThemeSettingFloat(): void
{
$nameID = new ID('floatSetting');

$settingEncoder = $this->getShopSettingEncoderMock(
FieldType::NUMBER,
1.23,
1.23
);
$repository = $this->getSut(settingEncoder: $settingEncoder);
$repository->expects($this->once())
->method('saveSettingValue')
->with($nameID, 'awesomeTheme', '1.23');

$repository->saveFloatSetting($nameID, 1.23, 'awesomeTheme');
}

public function testChangeNoThemeSettingFloat(): void
{
$nameID = new ID('NotExistingSetting');

$settingEncoder = $this->getShopSettingEncoderMock(
FieldType::NUMBER,
1.23,
1.23
);
$repository = $this->getSut(settingEncoder: $settingEncoder);
$repository->expects($this->once())
->method('saveSettingValue')
->with($nameID, 'awesomeTheme', '1.23')
->willThrowException(new NotFound());

$this->expectException(NotFound::class);
$this->expectExceptionMessage('The float setting "' . $nameID->val() . '" doesn\'t exist');

$repository->saveFloatSetting($nameID, 1.23, 'awesomeTheme');
}

public function getShopSettingEncoderMock(
string $fieldType,
mixed $decodedValue,
mixed $encodedValue
): ShopSettingEncoderInterface|MockObject
{
$settingEncoder = $this->createMock(ShopSettingEncoderInterface::class);
$settingEncoder->expects($this->once())
->method('encode')
->with($fieldType, $decodedValue)
->willReturn($encodedValue);
return $settingEncoder;
}

public function getSut(
?BasicContextInterface $basicContext = null,
?EventDispatcherInterface $eventDispatcher = null,
?QueryBuilderFactoryInterface $queryBuilderFactory = null,
?ShopSettingEncoderInterface $settingEncoder = null
): MockObject|ThemeSettingRepository {
$repository = $this->getMockBuilder(ThemeSettingRepository::class)
->setConstructorArgs([
$basicContext ?? $this->createMock(BasicContextInterface::class),
$eventDispatcher ?? $this->createMock(EventDispatcherInterface::class),
$queryBuilderFactory ?? $this->createMock(QueryBuilderFactoryInterface::class),
$settingEncoder ?? $this->createMock(ShopSettingEncoderInterface::class)
])
->onlyMethods(['saveSettingValue'])
->getMock();
return $repository;
}
}

0 comments on commit 0a58c80

Please sign in to comment.