Skip to content

Commit

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

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

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

public function saveStringSetting(ID $name, string $value, string $themeId): void
Expand Down
52 changes: 44 additions & 8 deletions tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OxidEsales\GraphQL\Base\Exception\NotFound;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ThemeSettingRepository;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use TheCodingMachine\GraphQLite\Types\ID;

Expand Down Expand Up @@ -92,17 +93,53 @@ public function testSaveNotExistingFloatSetting(): void
$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');
$this->expectExceptionMessage('The float setting "notExistingSetting" doesn\'t exist');
$repository->saveFloatSetting(new ID('notExistingSetting'), 1234, 'awesomeTheme');
}

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

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

$this->createThemeSetting(
$queryBuilderFactory,
$name,
FieldType::BOOLEAN,
''
);

$repository->saveBooleanSetting(new ID($name), true, 'awesomeTheme');
$floatResult = $repository->getBoolean(new ID($name), 'awesomeTheme');

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

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

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

private function getSut(
?BasicContextInterface $basicContext = null,
?EventDispatcherInterface $eventDispatcher = null,
?QueryBuilderFactoryInterface $queryBuilderFactory = null,
?ShopSettingEncoderInterface $shopSettingEncoder = null
): ThemeSettingRepository
{
): ThemeSettingRepository {
return new ThemeSettingRepository(
$basicContext ?? $this->get(BasicContextInterface::class),
$eventDispatcher ?? $this->get(EventDispatcherInterface::class),
Expand All @@ -116,8 +153,7 @@ private function createThemeSetting(
string $name,
string $fieldType,
mixed $value
): void
{
): void {
$uniqueId = uniqid();
$queryBuilder = $queryBuilderFactory->create();
$queryBuilder
Expand All @@ -141,9 +177,9 @@ private function createThemeSetting(

/**
* @param string $name
* @return \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface|(EventDispatcherInterface&\PHPUnit\Framework\MockObject\MockObject)
* @return MockObject|EventDispatcherInterface|(EventDispatcherInterface&MockObject)
*/
public function getEventDispatcherMock(string $name): \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface
public function getEventDispatcherMock(string $name): MockObject|EventDispatcherInterface
{
$configurationChangedEvent = new ThemeSettingChangedEvent(
$name,
Expand Down
42 changes: 38 additions & 4 deletions tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function testChangeNoThemeSettingInteger(): void
$repository = $this->getSut(settingEncoder: $settingEncoder);
$repository->expects($this->once())
->method('saveSettingValue')
->with($nameID, 'awesomeTheme', '123')
->willThrowException(new NotFound());

$this->expectException(NotFound::class);
Expand Down Expand Up @@ -81,7 +80,6 @@ public function testChangeNoThemeSettingFloat(): void
$repository = $this->getSut(settingEncoder: $settingEncoder);
$repository->expects($this->once())
->method('saveSettingValue')
->with($nameID, 'awesomeTheme', '1.23')
->willThrowException(new NotFound());

$this->expectException(NotFound::class);
Expand All @@ -90,12 +88,48 @@ public function testChangeNoThemeSettingFloat(): void
$repository->saveFloatSetting($nameID, 1.23, 'awesomeTheme');
}

public function testChangeThemeSettingBoolean(): void
{
$nameID = new ID('booleanSetting');

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

$repository->saveBooleanSetting($nameID, false, 'awesomeTheme');
}

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

$settingEncoder = $this->getShopSettingEncoderMock(
FieldType::BOOLEAN,
false,
''
);
$repository = $this->getSut(settingEncoder: $settingEncoder);
$repository->expects($this->once())
->method('saveSettingValue')
->willThrowException(new NotFound());

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

$repository->saveBooleanSetting($nameID, false, 'awesomeTheme');
}

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

0 comments on commit 1fe5d64

Please sign in to comment.