diff --git a/src/Setting/Infrastructure/ThemeSettingRepository.php b/src/Setting/Infrastructure/ThemeSettingRepository.php index 372a24a..85e4f20 100644 --- a/src/Setting/Infrastructure/ThemeSettingRepository.php +++ b/src/Setting/Infrastructure/ThemeSettingRepository.php @@ -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 diff --git a/tests/Codeception/Acceptance/NotAuthorizedAccessCest.php b/tests/Codeception/Acceptance/NotAuthorizedAccessCest.php index bd479eb..bc9f78a 100644 --- a/tests/Codeception/Acceptance/NotAuthorizedAccessCest.php +++ b/tests/Codeception/Acceptance/NotAuthorizedAccessCest.php @@ -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']; } diff --git a/tests/Codeception/Acceptance/ThemeSettingMutationsCest.php b/tests/Codeception/Acceptance/ThemeSettingMutationsCest.php index d4497c4..75ad1e6 100644 --- a/tests/Codeception/Acceptance/ThemeSettingMutationsCest.php +++ b/tests/Codeception/Acceptance/ThemeSettingMutationsCest.php @@ -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()); @@ -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 diff --git a/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php b/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php index fb149ea..c29e12d 100644 --- a/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php +++ b/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php @@ -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); @@ -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); diff --git a/tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php b/tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php index b9c14ec..35e2827 100644 --- a/tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php +++ b/tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php @@ -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'); diff --git a/tests/Unit/Service/ThemeSettingServiceTest.php b/tests/Unit/Service/ThemeSettingServiceTest.php index e9aa46c..dd2e738 100644 --- a/tests/Unit/Service/ThemeSettingServiceTest.php +++ b/tests/Unit/Service/ThemeSettingServiceTest.php @@ -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 */