Skip to content

Commit

Permalink
OXDEV-7555 Change SettingType validation exception to isSupported field
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 13, 2023
1 parent 23dd45f commit ca80792
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 65 deletions.
27 changes: 12 additions & 15 deletions src/Setting/DataType/SettingType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,39 @@
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
{
return $this->name;
}

/**
* @Field()
* @Field
*/
public function getType(): string
{
return $this->type;
}

/**
* @Field
*/
public function isSupported(): bool
{
return FieldType::validateFieldType($this->getType());
}
}
40 changes: 29 additions & 11 deletions tests/Codeception/Acceptance/ModuleSettingCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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
Expand Down
38 changes: 30 additions & 8 deletions tests/Codeception/Acceptance/ShopSettingCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ public function testGetShopSettingsListNotAuthorized(AcceptanceTester $I): void
shopSettingsList {
name
type
supported
}
}'
);
Expand All @@ -350,6 +351,7 @@ public function testGetShopSettingsListAuthorized(AcceptanceTester $I): void
shopSettingsList {
name
type
supported
}
}'
);
Expand All @@ -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
);
}
}
50 changes: 37 additions & 13 deletions tests/Codeception/Acceptance/ThemeSettingCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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
);
}
}
18 changes: 0 additions & 18 deletions tests/Unit/DataType/SettingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
45 changes: 45 additions & 0 deletions tests/Unit/DataType/SettingTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\DataType;

use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\SettingType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use PHPUnit\Framework\TestCase;
use TheCodingMachine\GraphQLite\Types\ID;

/**
* @covers \OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\SettingType
*/
class SettingTypeTest extends TestCase
{
public function testGetters(): void
{
$settingIdentifier = new ID('someSettingName');
$settingType = 'someRandomType';

$sut = new SettingType(
$settingIdentifier,
$settingType
);

$this->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];
}
}

0 comments on commit ca80792

Please sign in to comment.