diff --git a/src/Setting/DataType/SettingType.php b/src/Setting/DataType/SettingType.php index d64d690..9e46697 100644 --- a/src/Setting/DataType/SettingType.php +++ b/src/Setting/DataType/SettingType.php @@ -13,31 +13,20 @@ use TheCodingMachine\GraphQLite\Annotations\Field; use TheCodingMachine\GraphQLite\Annotations\Type; use TheCodingMachine\GraphQLite\Types\ID; -use UnexpectedValueException; /** - * @Type() + * @Type */ final class SettingType { - private string $type; - public function __construct( private ID $name, - String $type + private string $type ) { - $valid = FieldType::validateFieldType($type); - if (!$valid) { - throw new UnexpectedValueException('The value "'.$type.'" is not a valid field type. -Please use one of the following types: "'.implode('", "', FieldType::getEnums()).'".'); - } - - $this->type = $type; - } /** - * @Field() + * @Field */ public function getName(): ID { @@ -45,10 +34,18 @@ public function getName(): ID } /** - * @Field() + * @Field */ public function getType(): string { return $this->type; } + + /** + * @Field + */ + public function isSupported(): bool + { + return FieldType::validateFieldType($this->getType()); + } } diff --git a/tests/Codeception/Acceptance/ModuleSettingCest.php b/tests/Codeception/Acceptance/ModuleSettingCest.php index c8ea5cb..828c85f 100644 --- a/tests/Codeception/Acceptance/ModuleSettingCest.php +++ b/tests/Codeception/Acceptance/ModuleSettingCest.php @@ -473,12 +473,13 @@ public function testGetModuleSettingsListNotAuthorized(AcceptanceTester $I): voi $I->login($this->getAgentUsername(), $this->getAgentPassword()); $I->sendGQLQuery( - 'query{ - moduleSettingsList(moduleId: "'.$this->getTestModuleName().'") { + 'query getSettings($moduleId: String!){ + moduleSettingsList(moduleId: $moduleId) { name type } - }' + }', + ['moduleId' => $this->getTestModuleName()] ); $I->seeResponseIsJson(); @@ -493,12 +494,14 @@ public function testGetModuleSettingsListAuthorized(AcceptanceTester $I): void $I->login($this->getAdminUsername(), $this->getAdminPassword()); $I->sendGQLQuery( - 'query{ - moduleSettingsList(moduleId: "'.$this->getTestModuleName().'") { + 'query getSettings($moduleId: String!){ + moduleSettingsList(moduleId: $moduleId) { name type + supported } - }' + }', + ['moduleId' => $this->getTestModuleName()] ); $I->seeResponseIsJson(); @@ -508,11 +511,26 @@ public function testGetModuleSettingsListAuthorized(AcceptanceTester $I): void $settingsList = $result['data']['moduleSettingsList']; $I->assertCount(5, $settingsList); - $I->assertContains(['name'=>'intSetting', 'type'=>FieldType::NUMBER], $settingsList); - $I->assertContains(['name'=>'floatSetting', 'type'=>FieldType::NUMBER], $settingsList); - $I->assertContains(['name'=>'boolSetting', 'type'=>FieldType::BOOLEAN], $settingsList); - $I->assertContains(['name'=>'stringSetting', 'type'=>FieldType::STRING], $settingsList); - $I->assertContains(['name'=>'arraySetting', 'type'=>FieldType::ARRAY], $settingsList); + $I->assertContains( + ['name' => 'intSetting', 'type' => FieldType::NUMBER, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'floatSetting', 'type' => FieldType::NUMBER, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'boolSetting', 'type' => FieldType::BOOLEAN, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'stringSetting', 'type' => FieldType::STRING, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'arraySetting', 'type' => FieldType::ARRAY, 'supported' => true], + $settingsList + ); } private function prepareConfiguration(): void diff --git a/tests/Codeception/Acceptance/ShopSettingCest.php b/tests/Codeception/Acceptance/ShopSettingCest.php index 448e65f..3ad2e01 100644 --- a/tests/Codeception/Acceptance/ShopSettingCest.php +++ b/tests/Codeception/Acceptance/ShopSettingCest.php @@ -330,6 +330,7 @@ public function testGetShopSettingsListNotAuthorized(AcceptanceTester $I): void shopSettingsList { name type + supported } }' ); @@ -350,6 +351,7 @@ public function testGetShopSettingsListAuthorized(AcceptanceTester $I): void shopSettingsList { name type + supported } }' ); @@ -360,13 +362,33 @@ public function testGetShopSettingsListAuthorized(AcceptanceTester $I): void $I->assertArrayNotHasKey('errors', $result); $settingsList = $result['data']['shopSettingsList']; - $I->assertCount(7, $settingsList); - $I->assertContains(['name'=>'intSetting', 'type' => FieldType::NUMBER], $settingsList); - $I->assertContains(['name'=>'floatSetting', 'type' => FieldType::NUMBER], $settingsList); - $I->assertContains(['name'=>'boolSetting', 'type' =>FieldType::BOOLEAN], $settingsList); - $I->assertContains(['name'=>'stringSetting', 'type' => FieldType::STRING], $settingsList); - $I->assertContains(['name'=>'selectSetting', 'type' => FieldType::SELECT], $settingsList); - $I->assertContains(['name'=>'arraySetting', 'type' => FieldType::ARRAY], $settingsList); - $I->assertContains(['name'=>'aarraySetting', 'type' => FieldType::ASSOCIATIVE_ARRAY], $settingsList); + $I->assertContains( + ['name' => 'intSetting', 'type' => FieldType::NUMBER, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'floatSetting', 'type' => FieldType::NUMBER, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'boolSetting', 'type' => FieldType::BOOLEAN, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'stringSetting', 'type' => FieldType::STRING, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'selectSetting', 'type' => FieldType::SELECT, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'arraySetting', 'type' => FieldType::ARRAY, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'aarraySetting', 'type' => FieldType::ASSOCIATIVE_ARRAY, 'supported' => true], + $settingsList + ); } } diff --git a/tests/Codeception/Acceptance/ThemeSettingCest.php b/tests/Codeception/Acceptance/ThemeSettingCest.php index 3ad8be3..c0a73a0 100644 --- a/tests/Codeception/Acceptance/ThemeSettingCest.php +++ b/tests/Codeception/Acceptance/ThemeSettingCest.php @@ -326,12 +326,13 @@ public function testGetThemeSettingsListNotAuthorized(AcceptanceTester $I): void $I->login($this->getAgentUsername(), $this->getAgentPassword()); $I->sendGQLQuery( - 'query{ - themeSettingsList(themeId: "'.$this->getTestThemeName().'") { + 'query getSettings($themeId: String!){ + themeSettingsList(themeId: $themeId) { name type } - }' + }', + ['themeId' => $this->getTestThemeName()] ); $I->seeResponseIsJson(); @@ -346,12 +347,14 @@ public function testGetThemeSettingsListAuthorized(AcceptanceTester $I): void $I->login($this->getAdminUsername(), $this->getAdminPassword()); $I->sendGQLQuery( - 'query{ - themeSettingsList(themeId: "'.$this->getTestThemeName().'") { + 'query getSettings($themeId: String!){ + themeSettingsList(themeId: $themeId) { name type + supported } - }' + }', + ['themeId' => $this->getTestThemeName()] ); $I->seeResponseIsJson(); @@ -361,12 +364,33 @@ public function testGetThemeSettingsListAuthorized(AcceptanceTester $I): void $settingsList = $result['data']['themeSettingsList']; $I->assertCount(7, $settingsList); - $I->assertContains(['name'=>'intSetting', 'type' => FieldType::NUMBER], $settingsList); - $I->assertContains(['name'=>'floatSetting', 'type' => FieldType::NUMBER], $settingsList); - $I->assertContains(['name'=>'boolSetting', 'type' =>FieldType::BOOLEAN], $settingsList); - $I->assertContains(['name'=>'stringSetting', 'type' => FieldType::STRING], $settingsList); - $I->assertContains(['name'=>'selectSetting', 'type' => FieldType::SELECT], $settingsList); - $I->assertContains(['name'=>'arraySetting', 'type' => FieldType::ARRAY], $settingsList); - $I->assertContains(['name'=>'aarraySetting', 'type' => FieldType::ASSOCIATIVE_ARRAY], $settingsList); + $I->assertContains( + ['name' => 'intSetting', 'type' => FieldType::NUMBER, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'floatSetting', 'type' => FieldType::NUMBER, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'boolSetting', 'type' => FieldType::BOOLEAN, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'stringSetting', 'type' => FieldType::STRING, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'selectSetting', 'type' => FieldType::SELECT, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'arraySetting', 'type' => FieldType::ARRAY, 'supported' => true], + $settingsList + ); + $I->assertContains( + ['name' => 'aarraySetting', 'type' => FieldType::ASSOCIATIVE_ARRAY, 'supported' => true], + $settingsList + ); } } diff --git a/tests/Unit/DataType/SettingTest.php b/tests/Unit/DataType/SettingTest.php index d971d5e..5d6455b 100644 --- a/tests/Unit/DataType/SettingTest.php +++ b/tests/Unit/DataType/SettingTest.php @@ -49,22 +49,4 @@ public function testStringSettingAsCollection(): void $this->assertEquals(new ID('arraySetting'), $setting->getName()); $this->assertSame(json_encode(['nice', 'values']), $setting->getValue()); } - - public function testSettingType(): void - { - $settingType = $this->getSettingType(); - - $this->assertEquals(new ID('settingType'), $settingType->getName()); - $this->assertSame( FieldType::BOOLEAN, $settingType->getType() - ); - } - - public function testInvalidSettingType(): void - { - $this->expectException(UnexpectedValueException::class); - $this->expectExceptionMessage('The value "invalidType" is not a valid field type. -Please use one of the following types: "'.implode('", "', FieldType::getEnums()).'".'); - - new SettingType(new ID('coolSettingType'), 'invalidType'); - } } diff --git a/tests/Unit/DataType/SettingTypeTest.php b/tests/Unit/DataType/SettingTypeTest.php new file mode 100644 index 0000000..add68d1 --- /dev/null +++ b/tests/Unit/DataType/SettingTypeTest.php @@ -0,0 +1,45 @@ +assertEquals($settingIdentifier, $sut->getName()); + $this->assertEquals($settingType, $sut->getType()); + } + + /** @dataProvider isSupportedDataProvider */ + public function testIsSupported(string $settingType, bool $expectation): void + { + $sut = new SettingType( + new ID('someSettingName'), + $settingType + ); + + $this->assertSame($expectation, $sut->isSupported()); + } + + public function isSupportedDataProvider(): \Generator + { + yield 'not supported case' => ['settingType' => 'someRandomSettingId', 'expectation' => false]; + yield 'supported case' => ['settingType' => FieldType::ARRAY, 'expectation' => true]; + } +}