Skip to content

Commit

Permalink
OXDEV-7456: Add mutation to change theme select setting
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelOxid committed Nov 20, 2023
1 parent 856f272 commit fcbf1f1
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Setting/Infrastructure/ThemeSettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ public function saveStringSetting(ID $name, string $value, string $themeId): voi

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

$this->saveSettingValue($name, $themeId, (string)$value);
}

public function saveCollectionSetting(ID $name, array $value, string $themeId): void
Expand Down
1 change: 1 addition & 0 deletions tests/Codeception/Acceptance/NotAuthorizedAccessCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function themeMutationsDataProvider(): \Generator
yield ['queryType' => 'mutation', 'queryName' => 'changeThemeSettingFloat', 'field' => 'name'];
yield ['queryType' => 'mutation', 'queryName' => 'changeThemeSettingBoolean', 'field' => 'name'];
yield ['queryType' => 'mutation', 'queryName' => 'changeThemeSettingString', 'field' => 'name'];
yield ['queryType' => 'mutation', 'queryName' => 'changeThemeSettingSelect', 'field' => 'name'];
yield ['queryType' => 'mutation', 'queryName' => 'changeThemeSettingCollection', 'field' => 'name'];
yield ['queryType' => 'mutation', 'queryName' => 'changeThemeSettingAssocCollection', 'field' => 'name'];
}
Expand Down
30 changes: 29 additions & 1 deletion tests/Codeception/Acceptance/ThemeSettingMutationsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,34 @@ public function testChangeStringSettingAuthorized(AcceptanceTester $I): void
$I->assertSame('default', $setting['value']);
}

public function testChangeSelectSettingAuthorized(AcceptanceTester $I): void
{
$I->login($this->getAdminUsername(), $this->getAdminPassword());

$I->sendGQLQuery(
'mutation m($name: ID!, $value: String!, $themeId: String!){
changeThemeSettingSelect(name: $name, value: $value, themeId: $themeId) {
name
value
}
}',
[
'name' => 'selectSettingEditable',
'value' => 'new select',
'themeId' => self::TEST_THEME_ID
]
);

$I->seeResponseIsJson();

$result = $I->grabJsonResponseAsArray();
$I->assertArrayNotHasKey('errors', $result);

$setting = $result['data']['changeThemeSettingSelect'];
$I->assertSame('selectSettingEditable', $setting['name']);
$I->assertSame('new select', $setting['value']);
}

public function testChangeCollectionSettingAuthorized(AcceptanceTester $I): void
{
$I->login($this->getAdminUsername(), $this->getAdminPassword());
Expand All @@ -155,7 +183,7 @@ public function testChangeCollectionSettingAuthorized(AcceptanceTester $I): void

$setting = $result['data']['changeThemeSettingCollection'];
$I->assertSame('arraySettingEditable', $setting['name']);
$I->assertSame('[3, "interesting", "values"]', $setting['value']);
$I->assertSame('[3,"interesting","values"]', $setting['value']);
}

public function testChangeAssocCollectionSettingAuthorized(AcceptanceTester $I): void
Expand Down
41 changes: 39 additions & 2 deletions tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function testSaveNotExistingBooleanSetting(): void

public function testSaveAndGetStringSetting(): void
{
$name = 'coolBooleanString';
$name = 'coolStringSetting';
$eventDispatcher = $this->getEventDispatcherMock($name);
/** @var QueryBuilderFactoryInterface $queryBuilderFactory */
$queryBuilderFactory = $this->get(QueryBuilderFactoryInterface::class);
Expand Down Expand Up @@ -171,9 +171,46 @@ public function testSaveNotExistingStringSetting(): void
$repository->saveStringSetting(new ID('notExistingSetting'), 'new value', 'awesomeTheme');
}

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

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

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

$repository->saveSelectSetting(new ID($name), 'new select value', 'awesomeTheme');
$stringResult = $repository->getSelect(new ID($name), 'awesomeTheme');

$this->assertSame('new select value', $stringResult);
}

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

$this->expectException(NotFound::class);
$this->expectExceptionMessage('Configuration "notExistingSetting" was not found for awesomeTheme');
$repository->saveSelectSetting(new ID('notExistingSetting'), 'new value', 'awesomeTheme');
}

public function testSaveAndGetCollectionSetting(): void
{
$name = 'coolArrayString';
$name = 'coolArraySetting';
$eventDispatcher = $this->getEventDispatcherMock($name);
/** @var QueryBuilderFactoryInterface $queryBuilderFactory */
$queryBuilderFactory = $this->get(QueryBuilderFactoryInterface::class);
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ public function testChangeThemeSettingString(): void
$repository->saveStringSetting($nameID, 'default', 'awesomeTheme');
}

public function testChangeThemeSettingSelect(): void
{
$nameID = new ID('selectSetting');

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

$repository->saveSelectSetting($nameID, 'select', 'awesomeTheme');
}

public function testChangeThemeSettingCollection(): void
{
$nameID = new ID('arraySetting');
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Service/ThemeSettingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,30 @@ public function testChangeThemeSettingString(): void
$this->assertSame($getterValue, $stringSetting->getValue());
}

public function testChangeThemeSettingSelect(): void
{
$nameID = new ID('selectSetting');
$setterValue = 'select';
$getterValue = 'otherValue';
$themeID = 'awesomeTheme';
$repository = $this->getRepositorySettingGetterMock(
'getSelect',
$nameID,
$themeID,
$getterValue
);
$repository->expects($this->once())
->method('saveSelectSetting')
->with($nameID, $setterValue, $themeID);

$settingService = $this->getSut(themeSettingRepository: $repository);

$selectSetting = $settingService->changeSelectSetting($nameID, $setterValue, $themeID);

$this->assertSame($nameID, $selectSetting->getName());
$this->assertSame($getterValue, $selectSetting->getValue());
}

/**
* @dataProvider invalidCollectionDataProvider
*/
Expand Down

0 comments on commit fcbf1f1

Please sign in to comment.