Skip to content

Commit

Permalink
OXDEV-7557 Simplify ModuleSettingController tests
Browse files Browse the repository at this point in the history
Converted everything to one test which checks if controller proxying the data to the service and transfers result to the controller return.

Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 16, 2023
1 parent 21dad4a commit 91ae8f1
Showing 1 changed file with 105 additions and 180 deletions.
285 changes: 105 additions & 180 deletions tests/Unit/Controller/ModuleSettingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,197 +3,122 @@
namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Controller;

use OxidEsales\GraphQL\ConfigurationAccess\Setting\Controller\ModuleSettingController;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\BooleanSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\FloatSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\IntegerSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\SettingType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\StringSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\ModuleSettingServiceInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase;
use TheCodingMachine\GraphQLite\Types\ID;

class ModuleSettingControllerTest extends UnitTestCase
{
public function testGetModuleSettingInteger(): void
{
$serviceIntegerSetting = $this->getIntegerSetting();

$settingService = $this->createMock(ModuleSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getIntegerSetting')
->willReturn($serviceIntegerSetting);

$settingController = new ModuleSettingController($settingService);

$nameID = $serviceIntegerSetting->getName();
$integerSetting = $settingController->getModuleSettingInteger($nameID, 'awesomeModule');

$this->assertSame($serviceIntegerSetting, $integerSetting);
}

public function testGetModuleSettingFloat(): void
{
$serviceFloatSetting = $this->getFloatSetting();

$settingService = $this->createMock(ModuleSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getFloatSetting')
->willReturn($serviceFloatSetting);

$settingController = new ModuleSettingController($settingService);

$nameID = $serviceFloatSetting->getName();
$floatSetting = $settingController->getModuleSettingFloat($nameID, 'awesomeModule');

$this->assertSame($serviceFloatSetting, $floatSetting);
}

public function testGetModuleSettingBoolean(): void
{
$serviceBooleanSetting = $this->getNegativeBooleanSetting();

$settingService = $this->createMock(ModuleSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getBooleanSetting')
->willReturn($serviceBooleanSetting);

$settingController = new ModuleSettingController($settingService);

$nameID = $serviceBooleanSetting->getName();
$booleanSetting = $settingController->getModuleSettingBoolean($nameID, 'awesomeModule');

$this->assertSame($serviceBooleanSetting, $booleanSetting);
}

public function testGetModuleSettingString(): void
{
$serviceStringSetting = $this->getStringSetting();

$settingService = $this->createMock(ModuleSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getStringSetting')
->willReturn($serviceStringSetting);

$settingController = new ModuleSettingController($settingService);

$nameID = $serviceStringSetting->getName();
$stringSetting = $settingController->getModuleSettingString($nameID, 'awesomeModule');

$this->assertSame($serviceStringSetting, $stringSetting);
}

public function testGetModuleSettingCollection(): void
{
$serviceCollectionSetting = $this->getCollectionSetting();

$settingService = $this->createMock(ModuleSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getCollectionSetting')
->willReturn($serviceCollectionSetting);

$settingController = new ModuleSettingController($settingService);

$nameID = $serviceCollectionSetting->getName();
$collectionSetting = $settingController->getModuleSettingCollection($nameID, 'awesomeModule');

$this->assertSame($serviceCollectionSetting, $collectionSetting);
}

public function testChangeModuleSettingInteger(): void
{
$serviceIntegerSetting = $this->getIntegerSetting();

$settingService = $this->createMock(ModuleSettingServiceInterface::class);
$settingService->expects($this->once())
->method('changeIntegerSetting')
->willReturn($serviceIntegerSetting);

$settingController = new ModuleSettingController($settingService);

$nameID = $serviceIntegerSetting->getName();
$value = $serviceIntegerSetting->getValue();
$integerSetting = $settingController->changeModuleSettingInteger($nameID, $value, 'awesomeModule');

$this->assertSame($serviceIntegerSetting, $integerSetting);
}

public function testChangeModuleSettingFloat(): void
{
$serviceFloatSetting = $this->getFloatSetting();

/** @dataProvider proxyTestDataProvider */
public function testGetModuleSettingProxyToService(
string $controllerMethod,
string $serviceMethod,
array $params,
$expectedValue
): void {
$settingService = $this->createMock(ModuleSettingServiceInterface::class);
$settingService->expects($this->once())
->method('changeFloatSetting')
->willReturn($serviceFloatSetting);
$settingService->expects($this->once())->method($serviceMethod)->with(...$params)->willReturn($expectedValue);

$settingController = new ModuleSettingController($settingService);

$nameID = $serviceFloatSetting->getName();
$value = $serviceFloatSetting->getValue();
$floatSetting = $settingController->changeModuleSettingFloat($nameID, $value, 'awesomeModule');

$this->assertSame($serviceFloatSetting, $floatSetting);
$this->assertSame($expectedValue, $settingController->$controllerMethod(...$params));
}

public function testChangeModuleSettingBoolean(): void
public function proxyTestDataProvider(): \Generator
{
$serviceBooleanSetting = $this->getNegativeBooleanSetting();

$settingService = $this->createMock(ModuleSettingServiceInterface::class);
$settingService->expects($this->once())
->method('changeBooleanSetting')
->willReturn($serviceBooleanSetting);

$settingController = new ModuleSettingController($settingService);

$nameID = $serviceBooleanSetting->getName();
$value = $serviceBooleanSetting->getValue();
$booleanSetting = $settingController->changeModuleSettingBoolean($nameID, $value, 'awesomeModule');

$this->assertSame($serviceBooleanSetting, $booleanSetting);
}

public function testChangeModuleSettingString(): void
{
$serviceStringSetting = $this->getStringSetting();

$settingService = $this->createMock(ModuleSettingServiceInterface::class);
$settingService->expects($this->once())
->method('changeStringSetting')
->willReturn($serviceStringSetting);

$settingController = new ModuleSettingController($settingService);

$nameID = $serviceStringSetting->getName();
$value = $serviceStringSetting->getValue();
$stringSetting = $settingController->changeModuleSettingString($nameID, $value, 'awesomeModule');

$this->assertSame($serviceStringSetting, $stringSetting);
}

public function testChangeModuleSettingCollection(): void
{
$serviceCollectionSetting = $this->getCollectionSetting();

$settingService = $this->createMock(ModuleSettingServiceInterface::class);
$settingService->expects($this->once())
->method('changeCollectionSetting')
->willReturn($serviceCollectionSetting);

$settingController = new ModuleSettingController($settingService);

$nameID = $serviceCollectionSetting->getName();
$value = $serviceCollectionSetting->getValue();
$collectionSetting = $settingController->changeModuleSettingCollection($nameID, $value, 'awesomeModule');

$this->assertSame($collectionSetting, $serviceCollectionSetting);
}

public function testListModuleSettings(): void
{
$moduleId = 'awesomeModule';
$serviceSettingsList = $this->getSettingTypeList();
$settingService = $this->createMock(ModuleSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getSettingsList')
->with($moduleId)
->willReturn($serviceSettingsList);

$sut = new ModuleSettingController($settingService);
$this->assertSame($serviceSettingsList, $sut->getModuleSettingsList($moduleId));
$settingNameID = new ID('settingName');

yield 'getter integer' => [
'controllerMethod' => 'getModuleSettingInteger',
'serviceMethod' => 'getIntegerSetting',
'params' => [$settingNameID, 'awesomeModule'],
'expectedValue' => new IntegerSetting($settingNameID, 123)
];

yield 'getter float' => [
'controllerMethod' => 'getModuleSettingFloat',
'serviceMethod' => 'getFloatSetting',
'params' => [$settingNameID, 'awesomeModule'],
'expectedValue' => new FloatSetting($settingNameID, 1.23)
];

yield 'getter bool' => [
'controllerMethod' => 'getModuleSettingBoolean',
'serviceMethod' => 'getBooleanSetting',
'params' => [$settingNameID, 'awesomeModule'],
'expectedValue' => new BooleanSetting($settingNameID, false)
];

yield 'getter string' => [
'controllerMethod' => 'getModuleSettingString',
'serviceMethod' => 'getStringSetting',
'params' => [$settingNameID, 'awesomeModule'],
'expectedValue' => new StringSetting($settingNameID, 'default')
];

yield 'getter collection' => [
'controllerMethod' => 'getModuleSettingCollection',
'serviceMethod' => 'getCollectionSetting',
'params' => [$settingNameID, 'awesomeModule'],
'expectedValue' => new StringSetting($settingNameID, 'someCollectionStringExample')
];

yield 'setter integer' => [
'controllerMethod' => 'changeModuleSettingInteger',
'serviceMethod' => 'changeIntegerSetting',
'params' => [$settingNameID, 123, 'awesomeModule'],
'expectedValue' => new IntegerSetting($settingNameID, 123)
];

yield 'setter float' => [
'controllerMethod' => 'changeModuleSettingFloat',
'serviceMethod' => 'changeFloatSetting',
'params' => [$settingNameID, 1.23, 'awesomeModule'],
'expectedValue' => new FloatSetting($settingNameID, 1.23)
];

yield 'setter float with integer value' => [
'controllerMethod' => 'changeModuleSettingFloat',
'serviceMethod' => 'changeFloatSetting',
'params' => [$settingNameID, 123, 'awesomeModule'],
'expectedValue' => new FloatSetting($settingNameID, 123)
];

yield 'setter boolean' => [
'controllerMethod' => 'changeModuleSettingBoolean',
'serviceMethod' => 'changeBooleanSetting',
'params' => [$settingNameID, false, 'awesomeModule'],
'expectedValue' => new BooleanSetting($settingNameID, false)
];

yield 'setter string' => [
'controllerMethod' => 'changeModuleSettingString',
'serviceMethod' => 'changeStringSetting',
'params' => [$settingNameID, 'some string', 'awesomeModule'],
'expectedValue' => new StringSetting($settingNameID, 'some string')
];

yield 'setter collection' => [
'controllerMethod' => 'changeModuleSettingCollection',
'serviceMethod' => 'changeCollectionSetting',
'params' => [$settingNameID, 'some collection string', 'awesomeModule'],
'expectedValue' => new StringSetting($settingNameID, 'some collection string')
];

yield 'list query' => [
'controllerMethod' => 'getModuleSettingsList',
'serviceMethod' => 'getSettingsList',
'params' => ['awesomeModule'],
'expectedValue' => [
new SettingType($settingNameID, FieldType::NUMBER),
new SettingType($settingNameID, FieldType::NUMBER),
]
];
}
}

0 comments on commit 91ae8f1

Please sign in to comment.